This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
/
project_build_test.go
87 lines (75 loc) · 1.61 KB
/
project_build_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//go:build e2e
package tests_test
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"testing"
"gotest.tools/v3/assert"
)
func TestProjectBuild(t *testing.T) {
if testing.Short() {
t.Skip("short tests only")
}
c := mkCmd("./example", "./mage", "clean", "build")
assertCommandStarted(t, c)
assertCommandSucceded(t, c)
c = mkCmd("./example/build/_output/bin",
fmt.Sprintf("./other-%s-%s", runtime.GOOS, runtime.GOARCH))
assertCommandStarted(t, c)
assertCommandSucceded(t, c)
}
func mkCmd(dir, name string, args ...string) *exec.Cmd {
c := exec.Command(name, args...)
c.Env = append(
env(filterOutByName{names: []string{"GOOS", "GOARCH", "GOARM"}}),
"GOTRACEBACK=all",
)
c.Dir = dir
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c
}
func assertCommandStarted(tb testing.TB, c *exec.Cmd) {
tb.Helper()
assert.NilError(tb, c.Start())
tb.Logf("Started `%q` with pid %d",
append([]string{c.Path}, c.Args...),
c.Process.Pid)
tb.Log("Process env:")
for _, e := range c.Env {
tb.Logf(" * %s", e)
}
}
func assertCommandSucceded(tb testing.TB, c *exec.Cmd) {
tb.Helper()
assert.NilError(tb, c.Wait())
}
func env(filter envFilter) []string {
ret := make([]string, 0, len(os.Environ()))
for _, e := range os.Environ() {
envPair := strings.SplitN(e, "=", 2)
key := envPair[0]
if !filter.include(key) {
continue
}
ret = append(ret, e)
}
return ret
}
type filterOutByName struct {
names []string
}
func (f filterOutByName) include(name string) bool {
for _, n := range f.names {
if name == n {
return false
}
}
return true
}
type envFilter interface {
include(name string) bool
}