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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
package scope
import (
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/pyr-sh/dag"
"github.com/vercel/turbo/cli/internal/context"
"github.com/vercel/turbo/cli/internal/fs"
"github.com/vercel/turbo/cli/internal/lockfile"
"github.com/vercel/turbo/cli/internal/packagemanager"
"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/ui"
"github.com/vercel/turbo/cli/internal/util"
"github.com/vercel/turbo/cli/internal/workspace"
)
type mockSCM struct {
changed []string
contents map[string][]byte
}
func (m *mockSCM) ChangedFiles(_fromCommit string, _toCommit string, _relativeTo string) ([]string, error) {
return m.changed, nil
}
func (m *mockSCM) PreviousContent(fromCommit string, filePath string) ([]byte, error) {
contents, ok := m.contents[filePath]
if !ok {
return nil, fmt.Errorf("No contents found")
}
return contents, nil
}
type mockLockfile struct {
globalChange bool
versions map[string]string
allDeps map[string]map[string]string
}
func (m *mockLockfile) ResolvePackage(workspacePath turbopath.AnchoredUnixPath, name string, version string) (lockfile.Package, error) {
resolvedVersion, ok := m.versions[name]
if ok {
key := fmt.Sprintf("%s%s", name, version)
return lockfile.Package{Key: key, Version: resolvedVersion, Found: true}, nil
}
return lockfile.Package{Found: false}, nil
}
func (m *mockLockfile) AllDependencies(key string) (map[string]string, bool) {
deps, ok := m.allDeps[key]
return deps, ok
}
func (m *mockLockfile) Encode(w io.Writer) error {
return nil
}
func (m *mockLockfile) GlobalChange(other lockfile.Lockfile) bool {
return m.globalChange || (other != nil && other.(*mockLockfile).globalChange)
}
func (m *mockLockfile) Patches() []turbopath.AnchoredUnixPath {
return nil
}
func (m *mockLockfile) Subgraph(workspaces []turbopath.AnchoredSystemPath, packages []string) (lockfile.Lockfile, error) {
return nil, nil
}
var _ (lockfile.Lockfile) = (*mockLockfile)(nil)
func TestResolvePackages(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("cwd: %v", err)
}
root, err := fs.GetCwd(cwd)
if err != nil {
t.Fatalf("cwd: %v", err)
}
tui := ui.Default()
logger := hclog.Default()
// Dependency graph:
//
// app0 -
// \
// app1 -> libA
// \
// > libB -> libD
// /
// app2 <
// \
// > libC
// /
// app2-a <
//
// Filesystem layout:
//
// app/
// app0
// app1
// app2
// app2-a
// libs/
// libA
// libB
// libC
// libD
graph := dag.AcyclicGraph{}
graph.Add("app0")
graph.Add("app1")
graph.Add("app2")
graph.Add("app2-a")
graph.Add("libA")
graph.Add("libB")
graph.Add("libC")
graph.Add("libD")
graph.Connect(dag.BasicEdge("libA", "libB"))
graph.Connect(dag.BasicEdge("libB", "libD"))
graph.Connect(dag.BasicEdge("app0", "libA"))
graph.Connect(dag.BasicEdge("app1", "libA"))
graph.Connect(dag.BasicEdge("app2", "libB"))
graph.Connect(dag.BasicEdge("app2", "libC"))
graph.Connect(dag.BasicEdge("app2-a", "libC"))
workspaceInfos := workspace.Catalog{
PackageJSONs: map[string]*fs.PackageJSON{
"//": {
Dir: turbopath.AnchoredSystemPath("").ToSystemPath(),
UnresolvedExternalDeps: map[string]string{"global": "2"},
TransitiveDeps: []lockfile.Package{{Key: "global2", Version: "2", Found: true}},
},
"app0": {
Dir: turbopath.AnchoredUnixPath("app/app0").ToSystemPath(),
Name: "app0",
UnresolvedExternalDeps: map[string]string{"app0-dep": "2"},
TransitiveDeps: []lockfile.Package{
{Key: "app0-dep2", Version: "2", Found: true},
{Key: "app0-util2", Version: "2", Found: true},
},
},
"app1": {
Dir: turbopath.AnchoredUnixPath("app/app1").ToSystemPath(),
Name: "app1",
},
"app2": {
Dir: turbopath.AnchoredUnixPath("app/app2").ToSystemPath(),
Name: "app2",
},
"app2-a": {
Dir: turbopath.AnchoredUnixPath("app/app2-a").ToSystemPath(),
Name: "app2-a",
},
"libA": {
Dir: turbopath.AnchoredUnixPath("libs/libA").ToSystemPath(),
Name: "libA",
},
"libB": {
Dir: turbopath.AnchoredUnixPath("libs/libB").ToSystemPath(),
Name: "libB",
UnresolvedExternalDeps: map[string]string{"external": "1"},
TransitiveDeps: []lockfile.Package{
{Key: "external-dep-a1", Version: "1", Found: true},
{Key: "external-dep-b1", Version: "1", Found: true},
{Key: "external1", Version: "1", Found: true},
},
},
"libC": {
Dir: turbopath.AnchoredUnixPath("libs/libC").ToSystemPath(),
Name: "libC",
},
"libD": {
Dir: turbopath.AnchoredUnixPath("libs/libD").ToSystemPath(),
Name: "libD",
},
},
}
packageNames := []string{}
for name := range workspaceInfos.PackageJSONs {
packageNames = append(packageNames, name)
}
// global -> globalDep
// app0-dep -> app0-dep :)
makeLockfile := func(f func(*mockLockfile)) *mockLockfile {
l := mockLockfile{
globalChange: false,
versions: map[string]string{
"global": "2",
"app0-dep": "2",
"app0-util": "2",
"external": "1",
"external-dep-a": "1",
"external-dep-b": "1",
},
allDeps: map[string]map[string]string{
"global2": map[string]string{},
"app0-dep2": map[string]string{
"app0-util": "2",
},
"app0-util2": map[string]string{},
"external1": map[string]string{
"external-dep-a": "1",
"external-dep-b": "1",
},
"external-dep-a1": map[string]string{},
"external-dep-b1": map[string]string{},
},
}
if f != nil {
f(&l)
}
return &l
}
testCases := []struct {
name string
changed []string
expected []string
expectAllPackages bool
scope []string
since string
ignore string
globalDeps []string
includeDependencies bool
includeDependents bool
lockfile string
currLockfile *mockLockfile
prevLockfile *mockLockfile
inferPkgPath string
}{
{
name: "Just scope and dependencies",
changed: []string{},
includeDependencies: true,
scope: []string{"app2"},
expected: []string{"app2", "libB", "libC", "libD"},
},
{
name: "Only turbo.json changed",
changed: []string{"turbo.json"},
expected: []string{"//", "app0", "app1", "app2", "app2-a", "libA", "libB", "libC", "libD"},
since: "dummy",
includeDependencies: true,
},
{
name: "Only root package.json changed",
changed: []string{"package.json"},
expected: []string{"//", "app0", "app1", "app2", "app2-a", "libA", "libB", "libC", "libD"},
since: "dummy",
includeDependencies: true,
},
{
name: "Only package-lock.json changed",
changed: []string{"package-lock.json"},
expected: []string{"//", "app0", "app1", "app2", "app2-a", "libA", "libB", "libC", "libD"},
since: "dummy",
includeDependencies: true,
lockfile: "package-lock.json",
},
{
name: "Only yarn.lock changed",
changed: []string{"yarn.lock"},
expected: []string{"//", "app0", "app1", "app2", "app2-a", "libA", "libB", "libC", "libD"},
since: "dummy",
includeDependencies: true,
lockfile: "yarn.lock",
},
{
name: "Only pnpm-lock.yaml changed",
changed: []string{"pnpm-lock.yaml"},
expected: []string{"//", "app0", "app1", "app2", "app2-a", "libA", "libB", "libC", "libD"},
since: "dummy",
includeDependencies: true,
lockfile: "pnpm-lock.yaml",
},
{
name: "One package changed",
changed: []string{"libs/libB/src/index.ts"},
expected: []string{"libB"},
since: "dummy",
},
{
name: "One package manifest changed",
changed: []string{"libs/libB/package.json"},
expected: []string{"libB"},
since: "dummy",
},
{
name: "An ignored package changed",
changed: []string{"libs/libB/src/index.ts"},
expected: []string{},
since: "dummy",
ignore: "libs/libB/**/*.ts",
},
{
// nothing in scope depends on the change
name: "unrelated library changed",
changed: []string{"libs/libC/src/index.ts"},
expected: []string{},
since: "dummy",
scope: []string{"app1"},
includeDependencies: true, // scope implies include-dependencies
},
{
// a dependent lib changed, scope implies include-dependencies,
// so all deps of app1 get built
name: "dependency of scope changed",
changed: []string{"libs/libA/src/index.ts"},
expected: []string{"libA", "libB", "libD", "app1"},
since: "dummy",
scope: []string{"app1"},
includeDependencies: true, // scope implies include-dependencies
},
{
// a dependent lib changed, user explicitly asked to not build dependencies.
// Since the package matching the scope had a changed dependency, we run it.
// We don't include its dependencies because the user asked for no dependencies.
// note: this is not yet supported by the CLI, as you cannot specify --include-dependencies=false
name: "dependency of scope changed, user asked to not include depedencies",
changed: []string{"libs/libA/src/index.ts"},
expected: []string{"app1"},
since: "dummy",
scope: []string{"app1"},
includeDependencies: false,
},
{
// a nested dependent lib changed, user explicitly asked to not build dependencies
// note: this is not yet supported by the CLI, as you cannot specify --include-dependencies=false
name: "nested dependency of scope changed, user asked to not include dependencies",
changed: []string{"libs/libB/src/index.ts"},
expected: []string{"app1"},
since: "dummy",
scope: []string{"app1"},
includeDependencies: false,
},
{
name: "global dependency changed, even though it was ignored, forcing a build of everything",
changed: []string{"libs/libB/src/index.ts"},
expected: []string{"//", "app0", "app1", "app2", "app2-a", "libA", "libB", "libC", "libD"},
since: "dummy",
ignore: "libs/libB/**/*.ts",
globalDeps: []string{"libs/**/*.ts"},
},
{
name: "an app changed, user asked for dependencies to build",
changed: []string{"app/app2/src/index.ts"},
since: "dummy",
includeDependencies: true,
expected: []string{"app2", "libB", "libC", "libD"},
},
{
name: "a library changed, user asked for dependents to be built",
changed: []string{"libs/libB"},
since: "dummy",
includeDependents: true,
expected: []string{"app0", "app1", "app2", "libA", "libB"},
},
{
// no changes, no base to compare against, defaults to everything
name: "no changes or scope specified, build everything",
since: "",
expected: []string{"//", "app0", "app1", "app2", "app2-a", "libA", "libB", "libC", "libD"},
expectAllPackages: true,
},
{
// a dependent library changed, no deps beyond the scope are build
// "libB" is still built because it is a dependent within the scope, but libB's dependents
// are skipped
name: "a dependent library changed, build up to scope",
changed: []string{"libs/libD/src/index.ts"},
since: "dummy",
scope: []string{"libB"},
expected: []string{"libB", "libD"},
includeDependencies: true, // scope implies include-dependencies
},
{
name: "library change, no scope",
changed: []string{"libs/libA/src/index.ts"},
expected: []string{"libA", "app0", "app1"},
includeDependents: true,
since: "dummy",
},
{
// make sure multiple apps with the same prefix are handled separately.
// prevents this issue: https://github.com/vercel/turbo/issues/1528
name: "Two apps with an overlapping prefix changed",
changed: []string{"app/app2/src/index.js", "app/app2-a/src/index.js"},
expected: []string{"app2", "app2-a"},
since: "dummy",
},
{
name: "Global lockfile change invalidates all packages",
changed: []string{"dummy.lock"},
expected: []string{"//", "app0", "app1", "app2", "app2-a", "libA", "libB", "libC", "libD"},
lockfile: "dummy.lock",
currLockfile: makeLockfile(nil),
prevLockfile: makeLockfile(func(ml *mockLockfile) {
ml.globalChange = true
}),
since: "dummy",
},
{
name: "Dependency of workspace root change invalidates all packages",
changed: []string{"dummy.lock"},
expected: []string{"//", "app0", "app1", "app2", "app2-a", "libA", "libB", "libC", "libD"},
lockfile: "dummy.lock",
currLockfile: makeLockfile(nil),
prevLockfile: makeLockfile(func(ml *mockLockfile) {
ml.versions["global"] = "3"
ml.allDeps["global3"] = map[string]string{}
}),
since: "dummy",
},
{
name: "Version change invalidates package",
changed: []string{"dummy.lock"},
expected: []string{"//", "app0"},
lockfile: "dummy.lock",
currLockfile: makeLockfile(nil),
prevLockfile: makeLockfile(func(ml *mockLockfile) {
ml.versions["app0-util"] = "3"
ml.allDeps["app0-dep2"] = map[string]string{"app0-util": "3"}
ml.allDeps["app0-util3"] = map[string]string{}
}),
since: "dummy",
},
{
name: "Transitive dep invalidates package",
changed: []string{"dummy.lock"},
expected: []string{"//", "libB"},
lockfile: "dummy.lock",
currLockfile: makeLockfile(nil),
prevLockfile: makeLockfile(func(ml *mockLockfile) {
ml.versions["external-dep-a"] = "2"
ml.allDeps["external1"] = map[string]string{"external-dep-a": "2", "external-dep-b": "1"}
ml.allDeps["external-dep-a2"] = map[string]string{}
}),
since: "dummy",
},
{
name: "Transitive dep invalidates package and dependents",
changed: []string{"dummy.lock"},
expected: []string{"//", "app0", "app1", "app2", "libA", "libB"},
lockfile: "dummy.lock",
includeDependents: true,
currLockfile: makeLockfile(nil),
prevLockfile: makeLockfile(func(ml *mockLockfile) {
ml.versions["external-dep-a"] = "2"
ml.allDeps["external1"] = map[string]string{"external-dep-a": "2", "external-dep-b": "1"}
ml.allDeps["external-dep-a2"] = map[string]string{}
}),
since: "dummy",
},
{
name: "Infer app2 from directory",
inferPkgPath: "app/app2",
expected: []string{"app2"},
},
{
name: "Infer app2 from a subdirectory",
inferPkgPath: "app/app2/src",
expected: []string{"app2"},
},
{
name: "Infer from a directory with no packages",
inferPkgPath: "wrong",
expected: []string{},
},
{
name: "Infer from a parent directory",
inferPkgPath: "app",
expected: []string{"app0", "app1", "app2", "app2-a"},
},
{
name: "library change, no scope, inferred libs",
changed: []string{"libs/libA/src/index.ts"},
expected: []string{"libA"},
since: "dummy",
inferPkgPath: "libs",
},
{
name: "library change, no scope, inferred app",
changed: []string{"libs/libA/src/index.ts"},
expected: []string{},
since: "dummy",
inferPkgPath: "app",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("test #%v %v", i, tc.name), func(t *testing.T) {
// Convert test data to system separators.
systemSeparatorChanged := make([]string, len(tc.changed))
for index, path := range tc.changed {
systemSeparatorChanged[index] = filepath.FromSlash(path)
}
scm := &mockSCM{
changed: systemSeparatorChanged,
contents: make(map[string][]byte, len(systemSeparatorChanged)),
}
for _, path := range systemSeparatorChanged {
scm.contents[path] = nil
}
readLockfile := func(_rootPackageJSON *fs.PackageJSON, content []byte) (lockfile.Lockfile, error) {
return tc.prevLockfile, nil
}
pkgInferenceRoot, err := resolvePackageInferencePath(tc.inferPkgPath)
if err != nil {
t.Errorf("bad inference path (%v): %v", tc.inferPkgPath, err)
}
pkgs, isAllPackages, err := ResolvePackages(&Opts{
LegacyFilter: LegacyFilter{
Entrypoints: tc.scope,
Since: tc.since,
IncludeDependencies: tc.includeDependencies,
SkipDependents: !tc.includeDependents,
},
IgnorePatterns: []string{tc.ignore},
GlobalDepPatterns: tc.globalDeps,
PackageInferenceRoot: pkgInferenceRoot,
}, root, scm, &context.Context{
WorkspaceInfos: workspaceInfos,
WorkspaceNames: packageNames,
PackageManager: &packagemanager.PackageManager{Lockfile: tc.lockfile, UnmarshalLockfile: readLockfile},
WorkspaceGraph: graph,
RootNode: "root",
Lockfile: tc.currLockfile,
}, tui, logger)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
expected := make(util.Set)
for _, pkg := range tc.expected {
expected.Add(pkg)
}
if !reflect.DeepEqual(pkgs, expected) {
t.Errorf("ResolvePackages got %v, want %v", pkgs, expected)
}
if isAllPackages != tc.expectAllPackages {
t.Errorf("isAllPackages got %v, want %v", isAllPackages, tc.expectAllPackages)
}
})
}
}
|