-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
turbo_json_test.go
278 lines (242 loc) · 11 KB
/
turbo_json_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
package fs
import (
"os"
"reflect"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/util"
"gotest.tools/v3/assert/cmp"
)
func assertIsSorted(t *testing.T, arr []string, msg string) {
t.Helper()
if arr == nil {
return
}
copied := make([]string, len(arr))
copy(copied, arr)
sort.Strings(copied)
if !reflect.DeepEqual(arr, copied) {
t.Errorf("Expected sorted, got %v: %v", arr, msg)
}
}
func Test_ReadTurboConfig(t *testing.T) {
testDir := getTestDir(t, "correct")
turboJSON, turboJSONReadErr := readTurboConfig(testDir.UntypedJoin("turbo.json"))
if turboJSONReadErr != nil {
t.Fatalf("invalid parse: %#v", turboJSONReadErr)
}
assert.EqualValues(t, []string{"AWS_SECRET_KEY"}, turboJSON.GlobalPassthroughEnv)
pipelineExpected := map[string]BookkeepingTaskDefinition{
"build": {
definedFields: util.SetFromStrings([]string{"Outputs", "OutputMode", "DependsOn"}),
experimentalFields: util.SetFromStrings([]string{"PassthroughEnv"}),
experimental: taskDefinitionExperiments{
PassthroughEnv: []string{"GITHUB_TOKEN"},
},
TaskDefinition: taskDefinitionHashable{
Outputs: TaskOutputs{Inclusions: []string{".next/**", "dist/**"}, Exclusions: []string{"dist/assets/**"}},
TopologicalDependencies: []string{"build"},
EnvVarDependencies: []string{},
TaskDependencies: []string{},
ShouldCache: true,
OutputMode: util.NewTaskOutput,
},
},
"lint": {
definedFields: util.SetFromStrings([]string{"Outputs", "OutputMode", "ShouldCache", "DependsOn"}),
experimentalFields: util.SetFromStrings([]string{}),
experimental: taskDefinitionExperiments{
PassthroughEnv: []string{},
},
TaskDefinition: taskDefinitionHashable{
Outputs: TaskOutputs{},
TopologicalDependencies: []string{},
EnvVarDependencies: []string{"MY_VAR"},
TaskDependencies: []string{},
ShouldCache: true,
OutputMode: util.NewTaskOutput,
},
},
"dev": {
definedFields: util.SetFromStrings([]string{"OutputMode", "ShouldCache"}),
experimentalFields: util.SetFromStrings([]string{}),
experimental: taskDefinitionExperiments{
PassthroughEnv: []string{},
},
TaskDefinition: taskDefinitionHashable{
Outputs: TaskOutputs{},
TopologicalDependencies: []string{},
EnvVarDependencies: []string{},
TaskDependencies: []string{},
ShouldCache: false,
OutputMode: util.FullTaskOutput,
},
},
"publish": {
definedFields: util.SetFromStrings([]string{"Inputs", "Outputs", "DependsOn", "ShouldCache"}),
experimentalFields: util.SetFromStrings([]string{}),
experimental: taskDefinitionExperiments{
PassthroughEnv: []string{},
},
TaskDefinition: taskDefinitionHashable{
Outputs: TaskOutputs{Inclusions: []string{"dist/**"}},
TopologicalDependencies: []string{"build", "publish"},
EnvVarDependencies: []string{},
TaskDependencies: []string{"admin#lint", "build"},
ShouldCache: false,
Inputs: []string{"build/**/*"},
OutputMode: util.FullTaskOutput,
},
},
}
validateOutput(t, turboJSON, pipelineExpected)
remoteCacheOptionsExpected := RemoteCacheOptions{"team_id", true}
assert.EqualValues(t, remoteCacheOptionsExpected, turboJSON.RemoteCacheOptions)
}
func Test_LoadTurboConfig_Legacy(t *testing.T) {
testDir := getTestDir(t, "legacy-only")
packageJSONPath := testDir.UntypedJoin("package.json")
rootPackageJSON, pkgJSONReadErr := ReadPackageJSON(packageJSONPath)
if pkgJSONReadErr != nil {
t.Fatalf("invalid parse: %#v", pkgJSONReadErr)
}
_, turboJSONReadErr := LoadTurboConfig(testDir, rootPackageJSON, false)
expectedErrorMsg := "Could not find turbo.json. Follow directions at https://turbo.build/repo/docs to create one: file does not exist"
assert.EqualErrorf(t, turboJSONReadErr, expectedErrorMsg, "Error should be: %v, got: %v", expectedErrorMsg, turboJSONReadErr)
}
func Test_LoadTurboConfig_BothCorrectAndLegacy(t *testing.T) {
testDir := getTestDir(t, "both")
packageJSONPath := testDir.UntypedJoin("package.json")
rootPackageJSON, pkgJSONReadErr := ReadPackageJSON(packageJSONPath)
if pkgJSONReadErr != nil {
t.Fatalf("invalid parse: %#v", pkgJSONReadErr)
}
turboJSON, turboJSONReadErr := LoadTurboConfig(testDir, rootPackageJSON, false)
if turboJSONReadErr != nil {
t.Fatalf("invalid parse: %#v", turboJSONReadErr)
}
pipelineExpected := map[string]BookkeepingTaskDefinition{
"build": {
definedFields: util.SetFromStrings([]string{"Outputs", "OutputMode", "DependsOn"}),
experimentalFields: util.SetFromStrings([]string{}),
experimental: taskDefinitionExperiments{
PassthroughEnv: []string{},
},
TaskDefinition: taskDefinitionHashable{
Outputs: TaskOutputs{Inclusions: []string{".next/**", "dist/**"}, Exclusions: []string{"dist/assets/**"}},
TopologicalDependencies: []string{"build"},
EnvVarDependencies: []string{},
TaskDependencies: []string{},
ShouldCache: true,
OutputMode: util.NewTaskOutput,
},
},
}
validateOutput(t, turboJSON, pipelineExpected)
remoteCacheOptionsExpected := RemoteCacheOptions{"team_id", true}
assert.EqualValues(t, remoteCacheOptionsExpected, turboJSON.RemoteCacheOptions)
assert.Equal(t, rootPackageJSON.LegacyTurboConfig == nil, true)
}
func Test_ReadTurboConfig_InvalidEnvDeclarations1(t *testing.T) {
testDir := getTestDir(t, "invalid-env-1")
_, turboJSONReadErr := readTurboConfig(testDir.UntypedJoin("turbo.json"))
expectedErrorMsg := "turbo.json: You specified \"$A\" in the \"env\" key. You should not prefix your environment variables with \"$\""
assert.EqualErrorf(t, turboJSONReadErr, expectedErrorMsg, "Error should be: %v, got: %v", expectedErrorMsg, turboJSONReadErr)
}
func Test_ReadTurboConfig_InvalidEnvDeclarations2(t *testing.T) {
testDir := getTestDir(t, "invalid-env-2")
_, turboJSONReadErr := readTurboConfig(testDir.UntypedJoin("turbo.json"))
expectedErrorMsg := "turbo.json: You specified \"$A\" in the \"env\" key. You should not prefix your environment variables with \"$\""
assert.EqualErrorf(t, turboJSONReadErr, expectedErrorMsg, "Error should be: %v, got: %v", expectedErrorMsg, turboJSONReadErr)
}
func Test_ReadTurboConfig_InvalidGlobalEnvDeclarations(t *testing.T) {
testDir := getTestDir(t, "invalid-global-env")
_, turboJSONReadErr := readTurboConfig(testDir.UntypedJoin("turbo.json"))
expectedErrorMsg := "turbo.json: You specified \"$QUX\" in the \"globalEnv\" key. You should not prefix your environment variables with \"$\""
assert.EqualErrorf(t, turboJSONReadErr, expectedErrorMsg, "Error should be: %v, got: %v", expectedErrorMsg, turboJSONReadErr)
}
func Test_ReadTurboConfig_EnvDeclarations(t *testing.T) {
testDir := getTestDir(t, "legacy-env")
turboJSON, turboJSONReadErr := readTurboConfig(testDir.UntypedJoin("turbo.json"))
if turboJSONReadErr != nil {
t.Fatalf("invalid parse: %#v", turboJSONReadErr)
}
pipeline := turboJSON.Pipeline
assert.EqualValues(t, pipeline["task1"].TaskDefinition.EnvVarDependencies, sortedArray([]string{"A"}))
assert.EqualValues(t, pipeline["task2"].TaskDefinition.EnvVarDependencies, sortedArray([]string{"A"}))
assert.EqualValues(t, pipeline["task3"].TaskDefinition.EnvVarDependencies, sortedArray([]string{"A"}))
assert.EqualValues(t, pipeline["task4"].TaskDefinition.EnvVarDependencies, sortedArray([]string{"A", "B"}))
assert.EqualValues(t, pipeline["task6"].TaskDefinition.EnvVarDependencies, sortedArray([]string{"A", "B", "C", "D", "E", "F"}))
assert.EqualValues(t, pipeline["task7"].TaskDefinition.EnvVarDependencies, sortedArray([]string{"A", "B", "C"}))
assert.EqualValues(t, pipeline["task8"].TaskDefinition.EnvVarDependencies, sortedArray([]string{"A", "B", "C"}))
assert.EqualValues(t, pipeline["task9"].TaskDefinition.EnvVarDependencies, sortedArray([]string{"A"}))
assert.EqualValues(t, pipeline["task10"].TaskDefinition.EnvVarDependencies, sortedArray([]string{"A"}))
assert.EqualValues(t, pipeline["task11"].TaskDefinition.EnvVarDependencies, sortedArray([]string{"A", "B"}))
// check global env vars also
assert.EqualValues(t, sortedArray([]string{"FOO", "BAR", "BAZ", "QUX"}), sortedArray(turboJSON.GlobalEnv))
assert.EqualValues(t, sortedArray([]string{"somefile.txt"}), sortedArray(turboJSON.GlobalDeps))
}
func Test_TaskOutputsSort(t *testing.T) {
inclusions := []string{"foo/**", "bar"}
exclusions := []string{"special-file", ".hidden/**"}
taskOutputs := TaskOutputs{Inclusions: inclusions, Exclusions: exclusions}
taskOutputs.Sort()
assertIsSorted(t, taskOutputs.Inclusions, "Inclusions")
assertIsSorted(t, taskOutputs.Exclusions, "Exclusions")
assert.True(t, cmp.DeepEqual(taskOutputs, TaskOutputs{Inclusions: []string{"bar", "foo/**"}, Exclusions: []string{".hidden/**", "special-file"}})().Success())
}
// Helpers
func validateOutput(t *testing.T, turboJSON *TurboJSON, expectedPipeline Pipeline) {
t.Helper()
assertIsSorted(t, turboJSON.GlobalDeps, "Global Deps")
assertIsSorted(t, turboJSON.GlobalEnv, "Global Env")
validatePipeline(t, turboJSON.Pipeline, expectedPipeline)
}
func validatePipeline(t *testing.T, actual Pipeline, expected Pipeline) {
t.Helper()
// check top level keys
if len(actual) != len(expected) {
expectedKeys := []string{}
for k := range expected {
expectedKeys = append(expectedKeys, k)
}
actualKeys := []string{}
for k := range actual {
actualKeys = append(actualKeys, k)
}
t.Errorf("pipeline tasks mismatch. got %v, want %v", strings.Join(actualKeys, ","), strings.Join(expectedKeys, ","))
}
// check individual task definitions
for taskName, expectedTaskDefinition := range expected {
bookkeepingTaskDef, ok := actual[taskName]
if !ok {
t.Errorf("missing expected task: %v", taskName)
}
actualTaskDefinition := bookkeepingTaskDef.GetTaskDefinition()
assertIsSorted(t, actualTaskDefinition.Outputs.Inclusions, "Task output inclusions")
assertIsSorted(t, actualTaskDefinition.Outputs.Exclusions, "Task output exclusions")
assertIsSorted(t, actualTaskDefinition.EnvVarDependencies, "Task env vars")
assertIsSorted(t, actualTaskDefinition.PassthroughEnv, "Task env vars")
assertIsSorted(t, actualTaskDefinition.TopologicalDependencies, "Topo deps")
assertIsSorted(t, actualTaskDefinition.TaskDependencies, "Task deps")
assert.EqualValuesf(t, expectedTaskDefinition, bookkeepingTaskDef, "task definition mismatch for %v", taskName)
}
}
func getTestDir(t *testing.T, testName string) turbopath.AbsoluteSystemPath {
defaultCwd, err := os.Getwd()
if err != nil {
t.Errorf("failed to get cwd: %v", err)
}
cwd, err := CheckedToAbsoluteSystemPath(defaultCwd)
if err != nil {
t.Fatalf("cwd is not an absolute directory %v: %v", defaultCwd, err)
}
return cwd.UntypedJoin("testdata", testName)
}
func sortedArray(arr []string) []string {
sort.Strings(arr)
return arr
}