-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_test.go
281 lines (257 loc) · 6.73 KB
/
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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package modmake
import (
"context"
"fmt"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func ExampleBuild_Graph() {
b := NewBuild()
b.Tools().DependsOn(b.AddNewStep("print-tools", "", Task(func(ctx context.Context) error {
fmt.Println("Running in tools")
return nil
})))
b.Package().DependsOn(b.AddNewStep("print-pkg", "", Task(func(ctx context.Context) error {
fmt.Println("Running in package")
return nil
})))
b.Graph(true)
// Output:
// Printing build graph
//
// tools - Installs external tools that will be needed later
// -> print-tools - No description
// generate - Generates code, possibly using external tools
// -> tools *
// test - Runs unit tests on the code base
// -> generate *
// benchmark (skip step) - Runs benchmarking on the code base
// -> test *
// build - Builds the code base and outputs an artifact
// -> benchmark (skip step) *
// package - Bundles one or more built artifacts into one or more distributable packages
// -> build *
// -> print-pkg - No description
// print-pkg *
// print-tools *
//
// * - duplicate reference
}
func ExampleBuild_Execute() {
var (
ranTools bool
ranGenerate bool
)
b := NewBuild()
b.Tools().DependsOnRunner("print-tools", "", Task(func(ctx context.Context) error {
fmt.Println("Running in tools")
return nil
}))
b.Tools().Does(Task(func(ctx context.Context) error {
ranTools = true
return nil
}))
b.Generate().Does(Task(func(ctx context.Context) error {
ranGenerate = true
return nil
}))
// Skipping tests in tests
b.Test().Skip()
b.Package().DependsOnRunner("print-pkg", "", Task(func(ctx context.Context) error {
fmt.Println("Running in package")
return nil
}))
b.Execute("--skip", "tools", "--skip", "generate", "package", "print-tools")
fmt.Println("Ran tools:", ranTools)
fmt.Println("Ran generate:", ranGenerate)
// Output:
// Running in tools
// Running in package
// Ran tools: false
// Ran generate: false
}
func ExampleBuild_Steps() {
b := NewBuild()
b.Execute("-v", "steps")
// Output:
// benchmark - Runs benchmarking on the code base
// build - Builds the code base and outputs an artifact
// generate - Generates code, possibly using external tools
// package - Bundles one or more built artifacts into one or more distributable packages
// test - Runs unit tests on the code base
// tools - Installs external tools that will be needed later
}
func TestCyclesCheck(t *testing.T) {
tests := map[string]struct {
b func() *Build
noCycles bool
}{
"Self-dependence": {
b: func() *Build {
b := NewBuild()
b.Build().DependsOn(b.Build())
return b
},
},
"Direct cyclic": {
b: func() *Build {
b := NewBuild()
b.Benchmark().DependsOn(b.Build())
return b
},
},
"Large cycle": {
b: func() *Build {
steps := make([]*Step, 1_000)
for i := 0; i < 1_000; i++ {
steps[i] = NewStep(strconv.FormatInt(int64(i), 10), "").Does(NoOp())
if i > 0 {
steps[i].DependsOn(steps[i-1])
}
}
b := NewBuild()
b.Tools().DependsOn(steps[999])
steps[0].DependsOn(b.Package())
return b
},
},
"Dual dependency": {
b: func() *Build {
b := NewBuild()
b.Build().DependsOnRunner("echo", "Prints a message", Print("a message"))
b.Package().DependsOn(b.Step("echo"))
return b
},
noCycles: true,
},
}
for name, tc := range tests {
tc := tc
t.Run(name, func(t *testing.T) {
b := tc.b()
err := b.cyclesCheck()
if tc.noCycles {
assert.NoError(t, err)
} else {
t.Log(err)
assert.ErrorIs(t, err, ErrCycleDetected)
}
})
}
}
func TestCallBuild(t *testing.T) {
err := CallBuild("example/helloworld/build.go", "--only", "build").Run(context.TODO())
assert.NoError(t, err)
}
func TestSubmoduleCallBuild(t *testing.T) {
err := CallBuild("./example/submodule/modmake/build.go", "build").Run(context.TODO())
assert.NoError(t, err)
}
func ExampleCallBuild() {
callHelloWorldExample := Task(func(ctx context.Context) error {
return CallBuild("example/helloworld/build.go", "build").Run(ctx)
})
if err := callHelloWorldExample(context.TODO()); err != nil {
panic(err)
}
// Output:
}
func BenchmarkLargeCycle_1000(b *testing.B) {
build := func() *Build {
steps := make([]*Step, 1_000)
for i := 0; i < 1_000; i++ {
steps[i] = NewStep(strconv.FormatInt(int64(i), 10), "").Does(NoOp())
if i > 0 {
steps[i].DependsOn(steps[i-1])
}
}
b := NewBuild()
b.Tools().DependsOn(steps[999])
steps[0].DependsOn(b.Package())
return b
}()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = build.cyclesCheck()
}
}
func BenchmarkLargeCycle_10000(b *testing.B) {
build := func() *Build {
steps := make([]*Step, 10_000)
for i := 0; i < 10_000; i++ {
steps[i] = NewStep(strconv.FormatInt(int64(i), 10), "").Does(NoOp())
if i > 0 {
steps[i].DependsOn(steps[i-1])
}
}
b := NewBuild()
b.Tools().DependsOn(steps[9999])
steps[0].DependsOn(b.Package())
return b
}()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = build.cyclesCheck()
}
}
func TestBuild_Import(t *testing.T) {
b := NewBuild()
other := NewBuild()
assert.NotPanics(t, func() {
other.AddStep(NewStep("print", "Prints a message").Does(Print("Printing!")))
other.Build().DependsOn(other.Step("print"))
}, "New step creation should not panic")
b.Import("other", other)
_, ok := b.StepOk("other:print")
assert.True(t, ok, "Step 'other:print' should have been imported")
_, ok = b.StepOk("print")
assert.False(t, ok, "Other 'print' step should not have been imported")
}
func TestBuild_ImportAndLink(t *testing.T) {
outer := NewBuild()
middle := NewBuild()
inner := NewBuild()
middle.ImportAndLink("inner", inner)
middle.Step("inner:build").DependsOn(middle.Test())
outer.ImportAndLink("middle", middle)
steps := outer.Steps()
expected := []string{
"benchmark",
"build",
"generate",
"middle:benchmark",
"middle:build",
"middle:generate",
"middle:inner:benchmark",
"middle:inner:build",
"middle:inner:generate",
"middle:inner:package",
"middle:inner:test",
"middle:inner:tools",
"middle:package",
"middle:test",
"middle:tools",
"package",
"test",
"tools",
}
assert.Equal(t, expected, steps)
}
func TestCallRemote(t *testing.T) {
module := "github.com/saylorsolutions/[email protected]"
buildPath := Path("example/helloworld/build.go")
err := CallRemote(module, buildPath, "build").Run(context.TODO())
assert.NoError(t, err)
}
func ExampleCallRemote() {
callHelloWorldExample := Task(func(ctx context.Context) error {
module := "github.com/saylorsolutions/[email protected]"
buildPath := Path("example/helloworld/build.go")
return CallRemote(module, buildPath, "build").Run(context.TODO())
})
if err := callHelloWorldExample(context.TODO()); err != nil {
panic(err)
}
// Output:
}