|
| 1 | +package execute_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/microsoft/typescript-go/internal/compiler" |
| 10 | + "github.com/microsoft/typescript-go/internal/core" |
| 11 | + "github.com/microsoft/typescript-go/internal/execute" |
| 12 | + "github.com/microsoft/typescript-go/internal/tsoptions" |
| 13 | + "github.com/microsoft/typescript-go/internal/vfs/vfstest" |
| 14 | + "gotest.tools/v3/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func TestBuildOrderGenerator(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + testCases := []*buildOrderTestCase{ |
| 20 | + {"specify two roots", []string{"A", "G"}, []string{"D", "E", "C", "B", "A", "G"}, false}, |
| 21 | + {"multiple parts of the same graph in various orders", []string{"A"}, []string{"D", "E", "C", "B", "A"}, false}, |
| 22 | + {"multiple parts of the same graph in various orders", []string{"A", "C", "D"}, []string{"D", "E", "C", "B", "A"}, false}, |
| 23 | + {"multiple parts of the same graph in various orders", []string{"D", "C", "A"}, []string{"D", "E", "C", "B", "A"}, false}, |
| 24 | + {"other orderings", []string{"F"}, []string{"E", "F"}, false}, |
| 25 | + {"other orderings", []string{"E"}, []string{"E"}, false}, |
| 26 | + {"other orderings", []string{"F", "C", "A"}, []string{"E", "F", "D", "C", "B", "A"}, false}, |
| 27 | + {"returns circular order", []string{"H"}, []string{"E", "J", "I", "H"}, true}, |
| 28 | + {"returns circular order", []string{"A", "H"}, []string{"D", "E", "C", "B", "A", "J", "I", "H"}, true}, |
| 29 | + } |
| 30 | + for _, testcase := range testCases { |
| 31 | + testcase.run(t) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +type buildOrderTestCase struct { |
| 36 | + name string |
| 37 | + projects []string |
| 38 | + expected []string |
| 39 | + circular bool |
| 40 | +} |
| 41 | + |
| 42 | +func (b *buildOrderTestCase) configName(project string) string { |
| 43 | + return fmt.Sprintf("/home/src/workspaces/project/%s/tsconfig.json", project) |
| 44 | +} |
| 45 | + |
| 46 | +func (b *buildOrderTestCase) projectName(config string) string { |
| 47 | + str := strings.TrimPrefix(config, "/home/src/workspaces/project/") |
| 48 | + str = strings.TrimSuffix(str, "/tsconfig.json") |
| 49 | + return str |
| 50 | +} |
| 51 | + |
| 52 | +func (b *buildOrderTestCase) run(t *testing.T) { |
| 53 | + t.Helper() |
| 54 | + t.Run(b.name+" - "+strings.Join(b.projects, ","), func(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + files := make(map[string]string) |
| 57 | + deps := map[string][]string{ |
| 58 | + "A": {"B", "C"}, |
| 59 | + "B": {"C", "D"}, |
| 60 | + "C": {"D", "E"}, |
| 61 | + "F": {"E"}, |
| 62 | + "H": {"I"}, |
| 63 | + "I": {"J"}, |
| 64 | + "J": {"H", "E"}, |
| 65 | + } |
| 66 | + reverseDeps := map[string][]string{} |
| 67 | + for project, deps := range deps { |
| 68 | + for _, dep := range deps { |
| 69 | + reverseDeps[dep] = append(reverseDeps[dep], project) |
| 70 | + } |
| 71 | + } |
| 72 | + for _, project := range []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"} { |
| 73 | + files[fmt.Sprintf("/home/src/workspaces/project/%s/%s.ts", project, project)] = "export {}" |
| 74 | + referencesStr := "" |
| 75 | + if deps, ok := deps[project]; ok { |
| 76 | + referencesStr = fmt.Sprintf(`, "references": [%s]`, strings.Join(core.Map(deps, func(dep string) string { |
| 77 | + return fmt.Sprintf(`{ "path": "../%s" }`, dep) |
| 78 | + }), ",")) |
| 79 | + } |
| 80 | + files[b.configName(project)] = fmt.Sprintf(`{ |
| 81 | + "compilerOptions": { "composite": true }, |
| 82 | + "files": ["./%s.ts"], |
| 83 | + %s |
| 84 | + }`, project, referencesStr) |
| 85 | + } |
| 86 | + |
| 87 | + host := compiler.NewCompilerHost("/home/src/workspaces/project", vfstest.FromMap(files, true), "", nil) |
| 88 | + args := append([]string{"--build", "--dry"}, b.projects...) |
| 89 | + buildCommand := tsoptions.ParseBuildCommandLine(args, host) |
| 90 | + buildOrderGenerator := execute.NewBuildOrderGenerator(buildCommand, host, false) |
| 91 | + buildOrder := core.Map(buildOrderGenerator.Order(), b.projectName) |
| 92 | + assert.DeepEqual(t, buildOrder, b.expected) |
| 93 | + |
| 94 | + for index, project := range buildOrder { |
| 95 | + upstream := core.Map(buildOrderGenerator.Upstream(b.configName(project)), b.projectName) |
| 96 | + expectedUpstream := deps[project] |
| 97 | + assert.Assert(t, len(upstream) <= len(expectedUpstream), fmt.Sprintf("Expected upstream for %s to be at most %d, got %d", project, len(expectedUpstream), len(upstream))) |
| 98 | + for _, expected := range expectedUpstream { |
| 99 | + if slices.Contains(buildOrder[:index], expected) { |
| 100 | + assert.Assert(t, slices.Contains(upstream, expected), fmt.Sprintf("Expected upstream for %s to contain %s", project, expected)) |
| 101 | + } else { |
| 102 | + assert.Assert(t, !slices.Contains(upstream, expected), fmt.Sprintf("Expected upstream for %s to not contain %s", project, expected)) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + downstream := core.Map(buildOrderGenerator.Downstream(b.configName(project)), b.projectName) |
| 107 | + expectedDownstream := reverseDeps[project] |
| 108 | + assert.Assert(t, len(downstream) <= len(expectedDownstream), fmt.Sprintf("Expected downstream for %s to be at most %d, got %d", project, len(expectedDownstream), len(downstream))) |
| 109 | + for _, expected := range expectedDownstream { |
| 110 | + if slices.Contains(buildOrder[index+1:], expected) { |
| 111 | + assert.Assert(t, slices.Contains(downstream, expected), fmt.Sprintf("Expected downstream for %s to contain %s", project, expected)) |
| 112 | + } else { |
| 113 | + assert.Assert(t, !slices.Contains(downstream, expected), fmt.Sprintf("Expected downstream for %s to not contain %s", project, expected)) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if !b.circular { |
| 119 | + for project, projectDeps := range deps { |
| 120 | + child := b.configName(project) |
| 121 | + childIndex := slices.Index(buildOrder, child) |
| 122 | + if childIndex == -1 { |
| 123 | + continue |
| 124 | + } |
| 125 | + for _, dep := range projectDeps { |
| 126 | + parent := b.configName(dep) |
| 127 | + parentIndex := slices.Index(buildOrder, parent) |
| 128 | + |
| 129 | + assert.Assert(t, childIndex > parentIndex, fmt.Sprintf("Expecting child %s to be built after parent %s", project, dep)) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + }) |
| 134 | +} |
0 commit comments