|
| 1 | +package maven |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/bazelbuild/bazel-gazelle/label" |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | + "github.com/stackb/scala-gazelle/pkg/resolver" |
| 11 | +) |
| 12 | + |
| 13 | +func TestNewResolver(t *testing.T) { |
| 14 | + for name, tc := range map[string]struct { |
| 15 | + content string |
| 16 | + wantLabels []string |
| 17 | + }{ |
| 18 | + "v1 format": { |
| 19 | + content: `{ |
| 20 | + "dependency_tree": { |
| 21 | + "dependencies": [ |
| 22 | + { |
| 23 | + "coord": "xml-apis:xml-apis:1.4.01", |
| 24 | + "dependencies": [], |
| 25 | + "directDependencies": [], |
| 26 | + "file": "xml-apis-1.4.01.jar", |
| 27 | + "packages": ["javax.xml", "javax.xml.parsers"], |
| 28 | + "sha256": "abc123" |
| 29 | + } |
| 30 | + ], |
| 31 | + "version": "0.1.0" |
| 32 | + } |
| 33 | + }`, |
| 34 | + wantLabels: []string{ |
| 35 | + "@maven//:xml_apis_xml_apis", |
| 36 | + "@maven//:xml_apis_xml_apis", |
| 37 | + }, |
| 38 | + }, |
| 39 | + "v2 format": { |
| 40 | + content: `{ |
| 41 | + "__INPUT_ARTIFACTS_HASH": 123, |
| 42 | + "__RESOLVED_ARTIFACTS_HASH": 456, |
| 43 | + "version": "2", |
| 44 | + "artifacts": { |
| 45 | + "xml-apis:xml-apis": { |
| 46 | + "version": "1.4.01", |
| 47 | + "shasums": {"jar": "abc123"} |
| 48 | + } |
| 49 | + }, |
| 50 | + "packages": { |
| 51 | + "xml-apis:xml-apis": ["javax.xml", "javax.xml.parsers"] |
| 52 | + }, |
| 53 | + "dependencies": {}, |
| 54 | + "repositories": {}, |
| 55 | + "services": {}, |
| 56 | + "conflict_resolution": {}, |
| 57 | + "skipped": [] |
| 58 | + }`, |
| 59 | + wantLabels: []string{ |
| 60 | + "@maven//:xml_apis_xml_apis", |
| 61 | + "@maven//:xml_apis_xml_apis", |
| 62 | + }, |
| 63 | + }, |
| 64 | + } { |
| 65 | + t.Run(name, func(t *testing.T) { |
| 66 | + tmpDir := t.TempDir() |
| 67 | + installFile := filepath.Join(tmpDir, "maven_install.json") |
| 68 | + if err := os.WriteFile(installFile, []byte(tc.content), 0644); err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + |
| 72 | + var gotSymbols []*resolver.Symbol |
| 73 | + putSymbol := func(s *resolver.Symbol) error { |
| 74 | + gotSymbols = append(gotSymbols, s) |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + r, err := NewResolver(installFile, "maven", "scala", func(format string, args ...interface{}) {}, putSymbol) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("NewResolver: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + if r.Name() != "maven" { |
| 84 | + t.Errorf("Name() = %q, want %q", r.Name(), "maven") |
| 85 | + } |
| 86 | + |
| 87 | + var gotLabels []string |
| 88 | + for _, s := range gotSymbols { |
| 89 | + gotLabels = append(gotLabels, s.Label.String()) |
| 90 | + } |
| 91 | + |
| 92 | + if diff := cmp.Diff(tc.wantLabels, gotLabels); diff != "" { |
| 93 | + t.Errorf("labels (-want +got):\n%s", diff) |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestNewResolverV2Deterministic(t *testing.T) { |
| 100 | + // Create a v2 lockfile with multiple artifacts |
| 101 | + content := `{ |
| 102 | + "__INPUT_ARTIFACTS_HASH": 123, |
| 103 | + "__RESOLVED_ARTIFACTS_HASH": 456, |
| 104 | + "version": "2", |
| 105 | + "artifacts": {}, |
| 106 | + "packages": { |
| 107 | + "com.example:charlie": ["com.example.charlie"], |
| 108 | + "com.example:alpha": ["com.example.alpha"], |
| 109 | + "com.example:bravo": ["com.example.bravo"] |
| 110 | + }, |
| 111 | + "dependencies": {}, |
| 112 | + "repositories": {}, |
| 113 | + "services": {}, |
| 114 | + "conflict_resolution": {}, |
| 115 | + "skipped": [] |
| 116 | + }` |
| 117 | + |
| 118 | + tmpDir := t.TempDir() |
| 119 | + installFile := filepath.Join(tmpDir, "maven_install.json") |
| 120 | + if err := os.WriteFile(installFile, []byte(content), 0644); err != nil { |
| 121 | + t.Fatal(err) |
| 122 | + } |
| 123 | + |
| 124 | + // Run multiple times to verify deterministic ordering |
| 125 | + var firstOrder []string |
| 126 | + for i := 0; i < 10; i++ { |
| 127 | + var gotSymbols []*resolver.Symbol |
| 128 | + putSymbol := func(s *resolver.Symbol) error { |
| 129 | + gotSymbols = append(gotSymbols, s) |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + _, err := NewResolver(installFile, "maven", "scala", func(format string, args ...interface{}) {}, putSymbol) |
| 134 | + if err != nil { |
| 135 | + t.Fatalf("NewResolver: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + var order []string |
| 139 | + for _, s := range gotSymbols { |
| 140 | + order = append(order, s.Name) |
| 141 | + } |
| 142 | + |
| 143 | + if i == 0 { |
| 144 | + firstOrder = order |
| 145 | + // Verify alphabetical ordering |
| 146 | + want := []string{"com.example.alpha", "com.example.bravo", "com.example.charlie"} |
| 147 | + if diff := cmp.Diff(want, order); diff != "" { |
| 148 | + t.Errorf("expected alphabetical order (-want +got):\n%s", diff) |
| 149 | + } |
| 150 | + } else { |
| 151 | + if diff := cmp.Diff(firstOrder, order); diff != "" { |
| 152 | + t.Errorf("iteration %d: order is not deterministic (-first +current):\n%s", i, diff) |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestResolverResolve(t *testing.T) { |
| 159 | + content := `{ |
| 160 | + "__INPUT_ARTIFACTS_HASH": 123, |
| 161 | + "__RESOLVED_ARTIFACTS_HASH": 456, |
| 162 | + "version": "2", |
| 163 | + "artifacts": {}, |
| 164 | + "packages": { |
| 165 | + "com.example:foo": ["com.example.foo", "com.example.foo.bar"] |
| 166 | + }, |
| 167 | + "dependencies": {}, |
| 168 | + "repositories": {}, |
| 169 | + "services": {}, |
| 170 | + "conflict_resolution": {}, |
| 171 | + "skipped": [] |
| 172 | + }` |
| 173 | + |
| 174 | + tmpDir := t.TempDir() |
| 175 | + installFile := filepath.Join(tmpDir, "maven_install.json") |
| 176 | + if err := os.WriteFile(installFile, []byte(content), 0644); err != nil { |
| 177 | + t.Fatal(err) |
| 178 | + } |
| 179 | + |
| 180 | + r, err := NewResolver(installFile, "maven", "scala", func(format string, args ...interface{}) {}, func(s *resolver.Symbol) error { return nil }) |
| 181 | + if err != nil { |
| 182 | + t.Fatalf("NewResolver: %v", err) |
| 183 | + } |
| 184 | + |
| 185 | + for name, tc := range map[string]struct { |
| 186 | + pkg string |
| 187 | + wantLabel label.Label |
| 188 | + wantErr bool |
| 189 | + }{ |
| 190 | + "found package": { |
| 191 | + pkg: "com.example.foo", |
| 192 | + wantLabel: label.Label{Repo: "maven", Name: "com_example_foo"}, |
| 193 | + }, |
| 194 | + "found nested package": { |
| 195 | + pkg: "com.example.foo.bar", |
| 196 | + wantLabel: label.Label{Repo: "maven", Name: "com_example_foo"}, |
| 197 | + }, |
| 198 | + "not found": { |
| 199 | + pkg: "com.example.notfound", |
| 200 | + wantErr: true, |
| 201 | + }, |
| 202 | + } { |
| 203 | + t.Run(name, func(t *testing.T) { |
| 204 | + got, err := r.Resolve(tc.pkg) |
| 205 | + if tc.wantErr { |
| 206 | + if err == nil { |
| 207 | + t.Error("expected error, got nil") |
| 208 | + } |
| 209 | + return |
| 210 | + } |
| 211 | + if err != nil { |
| 212 | + t.Fatalf("Resolve: %v", err) |
| 213 | + } |
| 214 | + if diff := cmp.Diff(tc.wantLabel, got); diff != "" { |
| 215 | + t.Errorf("Resolve (-want +got):\n%s", diff) |
| 216 | + } |
| 217 | + }) |
| 218 | + } |
| 219 | +} |
0 commit comments