Skip to content

Commit

Permalink
releases/compiler: Support setting build tags in compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-woods committed Sep 20, 2023
1 parent 326d76a commit 7f2e7b2
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
# vendor/

# Ephemeral directories
/.idea
/bin
/test-reports
30 changes: 21 additions & 9 deletions releases/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Work struct {
Target string
Source string
WithCoverage bool
Tags string
Environment []string

Result *string
Expand All @@ -53,21 +54,32 @@ func (c *compiler) Compile(ctx context.Context, work Work) (string, error) {
goBin := goPath()
var cmd *exec.Cmd
if !work.WithCoverage {
// #nosec - this is fine
cmd = exec.CommandContext(ctx, goBin, "build",
"-ldflags="+c.ldFlags,
args := []string{
"build",
"-ldflags=" + c.ldFlags,
"-o", path,
work.Source,
)
} else {
}
if work.Tags != "" {
args = append(args, "-tags", work.Tags)
}
args = append(args, work.Source)
// #nosec - this is fine
cmd = exec.CommandContext(ctx, goBin, "test",
cmd = exec.CommandContext(ctx, goBin, args...)
} else {
args := []string{
"test",
"-coverpkg=./...",
"-c",
"-tags", "testrunmain",
work.Source,
"-o", path,
)
"-tags", "testrunmain",
}
if work.Tags != "" {
args[len(args)-1] += " " + work.Tags
}
args = append(args, work.Source)
// #nosec - this is fine
cmd = exec.CommandContext(ctx, goBin, args...)
}

cmd.Dir = cwd
Expand Down
2 changes: 1 addition & 1 deletion releases/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestCompiler_Compile(t *testing.T) {
binary, err = c.Compile(context.Background(), Work{
Name: "name",
Target: "../..",
Source: "./testing/compiler/internal/cmd",
Source: "./releases/compiler/internal/cmd",
Environment: []string{"FOO=foo", "BAR=bar"},
})
assert.Assert(t, err)
Expand Down
2 changes: 1 addition & 1 deletion releases/compiler/internal/cmd2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

func main() {
fmt.Printf("command 2: %v\n", os.Args[1:])
fmt.Printf("command 2: %v %s\n", os.Args[1:], importantString())
}
8 changes: 8 additions & 0 deletions releases/compiler/internal/cmd2/with_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build work
// +build work

package main

func importantString() string {
return "correct"
}
8 changes: 8 additions & 0 deletions releases/compiler/internal/cmd2/without_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !work
// +build !work

package main

func importantString() string {
return "wrong"
}
10 changes: 10 additions & 0 deletions releases/compiler/internal/cmd3/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"fmt"
"os"
)

func main() {
fmt.Printf("command 3: %v %s\n", os.Args[1:], importantString())
}
23 changes: 23 additions & 0 deletions releases/compiler/internal/cmd3/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build testrunmain
// +build testrunmain

package main

import (
"os"
"testing"
)

func TestRunMain(t *testing.T) {
stripTestArgs()
main()
}

func stripTestArgs() {
for i, arg := range os.Args {
if arg == "--" {
os.Args = append([]string{os.Args[0]}, os.Args[i+1:]...)
break
}
}
}
8 changes: 8 additions & 0 deletions releases/compiler/internal/cmd3/with_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build work
// +build work

package main

func importantString() string {
return "correct"
}
8 changes: 8 additions & 0 deletions releases/compiler/internal/cmd3/without_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !work
// +build !work

package main

func importantString() string {
return "wrong"
}
43 changes: 37 additions & 6 deletions releases/compiler/parallel_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package compiler

import (
"context"
"os"
"testing"

"gotest.tools/v3/assert"
"gotest.tools/v3/fs"
"gotest.tools/v3/golden"
"gotest.tools/v3/icmd"

"github.com/circleci/ex/testing/testcontext"
)

func TestParallel_Compile(t *testing.T) {
Expand All @@ -18,7 +20,7 @@ func TestParallel_Compile(t *testing.T) {
Parallelism: 2,
})

var binary1, binary2 string
var binary1, binary2, binary3 string
t.Cleanup(func() {
c.Cleanup()
})
Expand All @@ -28,23 +30,38 @@ func TestParallel_Compile(t *testing.T) {
Result: &binary1,
Name: "binary1",
Target: "../..",
Source: "./testing/compiler/internal/cmd",
Source: "./releases/compiler/internal/cmd",
Environment: []string{"FOO=foo1", "BAR=bar1"},
})
c.Add(Work{
Result: &binary2,
Name: "binary2",
Target: "../..",
Source: "./testing/compiler/internal/cmd2",
Source: "./releases/compiler/internal/cmd2",
Environment: []string{"FOO=foo2", "BAR=bar2"},
Tags: "work",
})
c.Add(Work{
Result: &binary3,
Name: "binary3",
Target: "../..",
Source: "./releases/compiler/internal/cmd3",
Environment: []string{"FOO=foo2", "BAR=bar2"},
Tags: "work",
WithCoverage: true,
})

err := c.Run(context.Background())
err := c.Run(testcontext.Background())
assert.Check(t, err)

_, err = os.Stat(binary1)
assert.Check(t, err)

_, err = os.Stat(binary2)
assert.Check(t, err)

_, err = os.Stat(binary3)
assert.Check(t, err)
}))

t.Run("Run binaries", func(t *testing.T) {
Expand All @@ -55,7 +72,21 @@ func TestParallel_Compile(t *testing.T) {

res = icmd.RunCommand(binary2, "arg1", "arg2", "arg3")
assert.Check(t, res.Equal(icmd.Expected{
Out: "command 2: [arg1 arg2 arg3]",
Out: "command 2: [arg1 arg2 arg3] correct",
}))
})

t.Run("Run binary with coverage", func(t *testing.T) {
// N.B. we use the txt extension here to not fall foul of the .out in .gitignore
// we would expect instrumented binary runs to use .o or .out for coverage report extensions.
file := fs.NewFile(t, "coverage.txt")
res := icmd.RunCommand(binary3, "-test.run", "^TestRunMain$",
"-test.coverprofile", file.Path(),
"--",
"arg1", "arg2", "arg3")
assert.Check(t, res.Equal(icmd.Expected{
Out: "command 3: [arg1 arg2 arg3] correct",
}))
assert.Check(t, golden.String(string(golden.Get(t, file.Path())), "coverage.txt"))
})
}
3 changes: 2 additions & 1 deletion releases/compiler/testdata/coverage.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mode: set
github.com/circleci/ex/testing/compiler/internal/cmd/main.go:8.13,10.2 1 1
github.com/circleci/ex/releases/compiler/internal/cmd3/main.go:8.13,10.2 1 1
github.com/circleci/ex/releases/compiler/internal/cmd3/with_tags.go:6.31,8.2 1 1

0 comments on commit 7f2e7b2

Please sign in to comment.