-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappbuild_test.go
162 lines (149 loc) · 4.94 KB
/
appbuild_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package modmake
import (
"context"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestNewAppBuild(t *testing.T) {
a := NewAppBuild("testapp", "cmd/modmake", "1.0.0")
assert.NotNil(t, a)
assert.Equal(t, "testapp", a.appName)
assert.Equal(t, Go().ToModulePath("cmd/modmake"), a.mainPath, "Module path should be prepended to a raw path")
assert.Equal(t, "1.0.0", a.version)
v := a.Variant("linux", "amd64").Build(func(gb *GoBuild) {
gb.OS("whatever").Arch("something")
})
assert.NotNil(t, v)
assert.Equal(t, "linux_amd64", v.variant)
assert.Equal(t, Path("build/testapp_linux_amd64/testapp"), v.buildOutput)
gb := a.goBuild(v)
var (
foundOS, foundArch bool
)
envs := gb.cmd.env
// Traverse in reverse order, since GOOS and GOARCH are added to the Cmd as environment overrides.
for i := len(envs) - 1; i >= 0; i-- {
env := envs[i]
kv := strings.SplitN(env, "=", 2)
assert.Len(t, kv, 2)
k, v := kv[0], kv[1]
if !foundOS && k == "GOOS" {
assert.Equal(t, "linux", v)
// Ignore previously set values.
foundOS = true
}
if !foundArch && k == "GOARCH" {
assert.Equal(t, "amd64", v)
// Ignore previously set values.
foundArch = true
}
}
assert.Equal(t, Path("build/testapp_linux_amd64/testapp"), gb.output)
assert.Equal(t, Path("dist/testapp"), v.distDir)
}
func TestNewAppBuild_Windows_adds_exe_suffix(t *testing.T) {
a := NewAppBuild("testapp", "cmd/modmake", "1.0.0")
v := a.Variant("windows", "amd64")
assert.Equal(t, Path("build/testapp_windows_amd64/testapp.exe"), v.buildOutput)
}
func TestAppVariant_NoPackage(t *testing.T) {
a := NewAppBuild("testapp", "cmd/modmake", "1.0.0")
v := a.Variant("darwin", "arm64").NoPackage()
task := a.pkgTask(v)
assert.Nil(t, task, "Calling NoPackage should result in a nil Task")
}
func TestAppVariant_Package(t *testing.T) {
var packageCalled bool
a := NewAppBuild("testapp", "cmd/modmake", "1.0.0")
v := a.Variant("darwin", "arm64").
NoPackage().
Package(func(binaryPath, destDir PathString, app, variant, version string) Task {
return Plain(func() {
packageCalled = true
})
})
task := a.pkgTask(v)
assert.NoError(t, task.Run(context.Background()))
assert.NotNil(t, task, "Calling Package should override the package Task")
assert.True(t, packageCalled, "The new package task should have been called")
}
func TestAppBuild_NoDupeVariants(t *testing.T) {
a := NewAppBuild("testapp", "cmd/modmake", "1.0.0")
assert.NotPanics(t, func() {
a.NamedVariant("dupe", "darwin", "arm64").NoPackage()
})
assert.Panics(t, func() {
a.NamedVariant("dupe", "linux", "amd64").NoPackage()
}, "Should panic when a duplicate variant name is added to an AppBuild since it's configuration time")
}
func TestBuild_ImportApp_Default(t *testing.T) {
b := NewBuild()
a := NewAppBuild("testapp", "cmd/modmake", "1.0.0")
b.ImportApp(a)
_, ok := b.StepOk("testapp:build-testapp_local")
assert.True(t, ok, "A default task should be created when no variants are added")
assert.Len(t, a.variants, 2) // Implied install variant.
}
func TestBuild_ImportApp(t *testing.T) {
b := NewBuild()
a := NewAppBuild("testapp", "cmd/modmake", "1.0.0")
win := a.Variant("windows", "amd64")
mac := a.Variant("darwin", "amd64")
lin := a.Variant("linux", "arm64")
b.ImportApp(a)
_, ok := b.StepOk("testapp:local")
assert.False(t, ok, "A default task should NOT be created when variants are added")
assert.Len(t, a.variants, 4) // An extra install variant is created.
shouldExist := []string{
"testapp:build-testapp_windows_amd64",
"testapp:package-testapp_windows_amd64",
"testapp:build-testapp_darwin_amd64",
"testapp:package-testapp_darwin_amd64",
"testapp:build-testapp_linux_arm64",
"testapp:package-testapp_linux_arm64",
"testapp:install",
// These should be equivalent to the list above
"testapp:" + a.buildName(win),
"testapp:" + a.packageName(win),
"testapp:" + a.buildName(mac),
"testapp:" + a.packageName(mac),
"testapp:" + a.buildName(lin),
"testapp:" + a.packageName(lin),
"testapp:install", // Not referencable using app, implied creation at the time of build generation.
}
for i := 7; i < len(shouldExist); i++ {
assert.Equal(t, shouldExist[i-7], shouldExist[i])
}
for _, step := range shouldExist {
_, ok := b.StepOk(step)
assert.Truef(t, ok, "Step %s should exist", step)
}
}
func TestAppBuildFunc_Then(t *testing.T) {
var calls []string
a := AppBuildFunc(func(gb *GoBuild) {
calls = append(calls, "a")
})
b := AppBuildFunc(func(gb *GoBuild) {
calls = append(calls, "b")
})
a.Then(b)(Go().Build(""))
assert.Len(t, calls, 2)
assert.Equal(t, "a", calls[0])
assert.Equal(t, "b", calls[1])
}
func TestAppPackageFunc_Then(t *testing.T) {
var calls []string
a := AppPackageFunc(func(_, _ PathString, _, _, _ string) Task {
return Plain(func() {
calls = append(calls, "a")
})
})
b := AppPackageFunc(func(_, _ PathString, _, _, _ string) Task {
return Plain(func() {
calls = append(calls, "b")
})
})
a.Then(b)(Path(""), Path(""), "", "", "")
}