|
| 1 | +package resolve_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/git-pkgs/resolve" |
| 11 | + "github.com/git-pkgs/resolve/parsers" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + apmSkillsCommit = "7f3c9a4d2e1b8c7f0a9e6d5c4b3a2918f7e6d5c4" |
| 16 | + apmCommit = "0123456789abcdef0123456789abcdef01234567" |
| 17 | +) |
| 18 | + |
| 19 | +func writeAPMLockfile(t *testing.T, name, content string) string { |
| 20 | + t.Helper() |
| 21 | + dir := t.TempDir() |
| 22 | + if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + return dir |
| 26 | +} |
| 27 | + |
| 28 | +func TestAPM(t *testing.T) { |
| 29 | + result, err := resolve.ParseDir("apm", filepath.Join("testdata", "apm")) |
| 30 | + if err != nil { |
| 31 | + t.Fatalf("unexpected error: %v", err) |
| 32 | + } |
| 33 | + if result.Manager != "apm" || result.Ecosystem != "apm" { |
| 34 | + t.Errorf("result = %s/%s, want apm/apm", result.Manager, result.Ecosystem) |
| 35 | + } |
| 36 | + |
| 37 | + type node struct{ name, version, purl string } |
| 38 | + var roots []node |
| 39 | + for _, dep := range result.Direct { |
| 40 | + roots = append(roots, node{dep.Name, dep.Version, dep.PURL}) |
| 41 | + } |
| 42 | + want := []node{ |
| 43 | + {"github.com/acme-corp/security-baseline", "a1b2c3d4e5f6789012345678901234567890abcd", |
| 44 | + "pkg:git/github.com/acme-corp/security-baseline@a1b2c3d4e5f6789012345678901234567890abcd"}, |
| 45 | + {"github.com/composiohq/awesome-claude-skills/brand-guidelines", apmSkillsCommit, |
| 46 | + "pkg:git/github.com/composiohq/awesome-claude-skills@" + apmSkillsCommit + "#brand-guidelines"}, |
| 47 | + {"github.com/composiohq/awesome-claude-skills/canvas-design", apmSkillsCommit, |
| 48 | + "pkg:git/github.com/composiohq/awesome-claude-skills@" + apmSkillsCommit + "#canvas-design"}, |
| 49 | + // Registry and local sources have no package coordinates. |
| 50 | + {"acme/toolkit", "2.3.1", ""}, |
| 51 | + {"./packages/shared-skills", "0.1.0", ""}, |
| 52 | + {"github.com/owner/test-helpers", apmCommit, "pkg:git/github.com/owner/test-helpers@" + apmCommit}, |
| 53 | + } |
| 54 | + if !reflect.DeepEqual(roots, want) { |
| 55 | + t.Fatalf("direct =\n%+v\nwant\n%+v", roots, want) |
| 56 | + } |
| 57 | + |
| 58 | + // Transitive chain resolved by repo_url, across hosts. |
| 59 | + baseline := findDep(result.Direct, "github.com/acme-corp/security-baseline") |
| 60 | + prompts := findDep(baseline.Deps, "github.com/acme-corp/common-prompts") |
| 61 | + if prompts == nil || findDep(baseline.Deps, "github.com/composiohq/design-tokens") == nil || len(baseline.Deps) != 2 { |
| 62 | + t.Fatalf("security-baseline deps = %+v", baseline.Deps) |
| 63 | + } |
| 64 | + style := findDep(prompts.Deps, "gitlab.com/team/style-guide") |
| 65 | + if style == nil || style.PURL != "pkg:git/gitlab.com/team/style-guide@0f1e2d3c4b5a69788796a5b4c3d2e1f0a9b8c7d6" { |
| 66 | + t.Errorf("style-guide = %+v", style) |
| 67 | + } |
| 68 | + |
| 69 | + for _, dep := range result.Direct { |
| 70 | + if dep.Deps == nil { |
| 71 | + t.Errorf("%s Deps is nil, want an empty slice for a tree manager", dep.Name) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestAPMLegacyLockfile(t *testing.T) { |
| 77 | + content, err := os.ReadFile(filepath.Join("testdata", "apm", "apm.lock.yaml")) |
| 78 | + if err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + current, err := resolve.ParseDir("apm", filepath.Join("testdata", "apm")) |
| 82 | + if err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + legacy, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock", string(content))) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("legacy apm.lock: %v", err) |
| 88 | + } |
| 89 | + if !reflect.DeepEqual(legacy.Direct, current.Direct) { |
| 90 | + t.Error("legacy apm.lock graph differs from apm.lock.yaml") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestAPMPrefersCurrentLockfile(t *testing.T) { |
| 95 | + dir := writeAPMLockfile(t, "apm.lock.yaml", "dependencies:\n - repo_url: owner/current\n resolved_commit: "+apmCommit+"\n") |
| 96 | + if err := os.WriteFile(filepath.Join(dir, "apm.lock"), []byte("dependencies:\n - repo_url: owner/legacy\n"), 0o644); err != nil { |
| 97 | + t.Fatal(err) |
| 98 | + } |
| 99 | + result, err := resolve.ParseDir("apm", dir) |
| 100 | + if err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + if len(result.Direct) != 1 || result.Direct[0].Name != "github.com/owner/current" { |
| 104 | + t.Errorf("direct = %+v, want only github.com/owner/current", result.Direct) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestAPMParentOnOtherHost(t *testing.T) { |
| 109 | + result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: |
| 110 | + - repo_url: team/rules |
| 111 | + host: gitlab.example.com |
| 112 | + resolved_commit: `+apmCommit+` |
| 113 | + - repo_url: team/shared |
| 114 | + host: gitlab.example.com |
| 115 | + resolved_commit: `+apmCommit+` |
| 116 | + depth: 2 |
| 117 | + resolved_by: team/rules |
| 118 | +`)) |
| 119 | + if err != nil { |
| 120 | + t.Fatal(err) |
| 121 | + } |
| 122 | + if len(result.Direct) != 1 || findDep(result.Direct[0].Deps, "gitlab.example.com/team/shared") == nil { |
| 123 | + t.Errorf("direct = %+v, want team/shared under team/rules", result.Direct) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// Full keys are what replacement lockfiles write; v1 and v2 use bare repo_urls. |
| 128 | +func TestAPMParentByVirtualKey(t *testing.T) { |
| 129 | + result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: |
| 130 | + - repo_url: acme/skills |
| 131 | + resolved_commit: `+apmCommit+` |
| 132 | + - repo_url: acme/skills |
| 133 | + virtual_path: review |
| 134 | + is_virtual: true |
| 135 | + resolved_commit: `+apmCommit+` |
| 136 | + - repo_url: acme/shared |
| 137 | + resolved_commit: `+apmCommit+` |
| 138 | + depth: 2 |
| 139 | + resolved_by: acme/skills/review |
| 140 | +`)) |
| 141 | + if err != nil { |
| 142 | + t.Fatal(err) |
| 143 | + } |
| 144 | + review := findDep(result.Direct, "github.com/acme/skills/review") |
| 145 | + whole := findDep(result.Direct, "github.com/acme/skills") |
| 146 | + if review == nil || findDep(review.Deps, "github.com/acme/shared") == nil || len(whole.Deps) != 0 { |
| 147 | + t.Errorf("direct = %+v, want acme/shared under the review subpackage only", result.Direct) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// Lockfiles written before microsoft/apm#3370 keep manifest casing in repo_url and resolved_by. |
| 152 | +func TestAPMMixedCaseLegacyParent(t *testing.T) { |
| 153 | + result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: |
| 154 | + - repo_url: ComposioHQ/awesome-claude-skills |
| 155 | + resolved_commit: `+apmCommit+` |
| 156 | + - repo_url: acme/shared |
| 157 | + resolved_commit: `+apmCommit+` |
| 158 | + depth: 2 |
| 159 | + resolved_by: ComposioHQ/awesome-claude-skills |
| 160 | +`)) |
| 161 | + if err != nil { |
| 162 | + t.Fatalf("unexpected error: %v", err) |
| 163 | + } |
| 164 | + if len(result.Direct) != 1 || findDep(result.Direct[0].Deps, "github.com/acme/shared") == nil { |
| 165 | + t.Errorf("direct = %+v, want acme/shared under awesome-claude-skills", result.Direct) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestAPMMixedCaseVirtualKey(t *testing.T) { |
| 170 | + lock := `dependencies: |
| 171 | + - repo_url: Acme/Skills |
| 172 | + virtual_path: Review |
| 173 | + is_virtual: true |
| 174 | + resolved_commit: ` + apmCommit + ` |
| 175 | + - repo_url: acme/shared |
| 176 | + resolved_commit: ` + apmCommit + ` |
| 177 | + depth: 2 |
| 178 | + resolved_by: ` |
| 179 | + if _, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", lock+"Acme/Skills/Review\n")); err != nil { |
| 180 | + t.Errorf("mixed-case repository: %v", err) |
| 181 | + } |
| 182 | + _, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", lock+"acme/skills/review\n")) |
| 183 | + if !errors.Is(err, parsers.ErrAPMMissingParent) { |
| 184 | + t.Errorf("lowercased virtual path: err = %v, want ErrAPMMissingParent", err) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +func TestAPMAnchoredLocalPaths(t *testing.T) { |
| 189 | + result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: |
| 190 | + - repo_url: _local/a |
| 191 | + source: local |
| 192 | + local_path: ./packages/a |
| 193 | + - repo_url: _local/b |
| 194 | + source: local |
| 195 | + local_path: ./tools/b |
| 196 | + - repo_url: _local/sibling |
| 197 | + source: local |
| 198 | + local_path: ../sibling |
| 199 | + anchored_local_path: packages/sibling |
| 200 | + depth: 2 |
| 201 | + resolved_by: _local/a |
| 202 | + - repo_url: _local/sibling |
| 203 | + source: local |
| 204 | + local_path: ../sibling |
| 205 | + anchored_local_path: tools/sibling |
| 206 | + depth: 2 |
| 207 | + resolved_by: _local/b |
| 208 | +`)) |
| 209 | + if err != nil { |
| 210 | + t.Fatalf("unexpected error: %v", err) |
| 211 | + } |
| 212 | + if len(result.Direct) != 2 { |
| 213 | + t.Fatalf("direct = %+v, want two local parents", result.Direct) |
| 214 | + } |
| 215 | + for _, parent := range result.Direct { |
| 216 | + if len(parent.Deps) != 1 || parent.Deps[0].Name != "../sibling" { |
| 217 | + t.Errorf("%s deps = %+v, want one ../sibling", parent.Name, parent.Deps) |
| 218 | + } |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +func TestAPMLocatorKeepsPathCase(t *testing.T) { |
| 223 | + result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: |
| 224 | + - repo_url: https://gitlab.com/Team/Rules |
| 225 | + resolved_commit: `+apmCommit+` |
| 226 | + - repo_url: https://gitlab.com/team/rules |
| 227 | + resolved_commit: `+apmCommit+` |
| 228 | +`)) |
| 229 | + if err != nil { |
| 230 | + t.Fatalf("unexpected error: %v", err) |
| 231 | + } |
| 232 | + if findDep(result.Direct, "gitlab.com/Team/Rules") == nil || findDep(result.Direct, "gitlab.com/team/rules") == nil { |
| 233 | + t.Errorf("direct = %+v, want both GitLab paths", result.Direct) |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +func TestAPMGraphErrors(t *testing.T) { |
| 238 | + commit := " resolved_commit: " + apmCommit + "\n" |
| 239 | + tests := []struct { |
| 240 | + name, content string |
| 241 | + want error |
| 242 | + }{ |
| 243 | + {"ambiguous parent", "dependencies:\n" + |
| 244 | + " - repo_url: acme/skills\n virtual_path: review\n is_virtual: true\n" + commit + |
| 245 | + " - repo_url: acme/skills\n virtual_path: deploy\n is_virtual: true\n" + commit + |
| 246 | + " - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n", |
| 247 | + parsers.ErrAPMAmbiguousParent}, |
| 248 | + {"repo_url shared by package and subpackage", "dependencies:\n" + |
| 249 | + " - repo_url: acme/skills\n" + commit + |
| 250 | + " - repo_url: acme/skills\n virtual_path: review\n is_virtual: true\n" + commit + |
| 251 | + " - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n", |
| 252 | + parsers.ErrAPMAmbiguousParent}, |
| 253 | + {"case-folded repo_url on two hosts", "dependencies:\n" + |
| 254 | + " - repo_url: ACME/Skills\n" + commit + |
| 255 | + " - repo_url: acme/skills\n host: gitlab.com\n" + commit + |
| 256 | + " - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n", |
| 257 | + parsers.ErrAPMAmbiguousParent}, |
| 258 | + {"missing parent", "dependencies:\n" + |
| 259 | + " - repo_url: acme/orphan\n" + commit + " depth: 2\n resolved_by: acme/gone\n", |
| 260 | + parsers.ErrAPMMissingParent}, |
| 261 | + {"parent at wrong depth", "dependencies:\n" + |
| 262 | + " - repo_url: acme/root\n" + commit + |
| 263 | + " - repo_url: acme/deep\n" + commit + " depth: 3\n resolved_by: acme/root\n", |
| 264 | + parsers.ErrAPMMissingParent}, |
| 265 | + {"cyclic parents", "dependencies:\n" + |
| 266 | + " - repo_url: acme/a\n" + commit + " depth: 2\n resolved_by: acme/b\n" + |
| 267 | + " - repo_url: acme/b\n" + commit + " depth: 2\n resolved_by: acme/a\n", |
| 268 | + parsers.ErrAPMMissingParent}, |
| 269 | + {"duplicate entry", "dependencies:\n" + |
| 270 | + " - repo_url: acme/twice\n" + commit + |
| 271 | + " - repo_url: acme/twice\n" + commit, |
| 272 | + parsers.ErrAPMDuplicateEntry}, |
| 273 | + } |
| 274 | + for _, tt := range tests { |
| 275 | + t.Run(tt.name, func(t *testing.T) { |
| 276 | + _, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", tt.content)) |
| 277 | + if !errors.Is(err, tt.want) { |
| 278 | + t.Errorf("err = %v, want %v", err, tt.want) |
| 279 | + } |
| 280 | + }) |
| 281 | + } |
| 282 | +} |
| 283 | + |
| 284 | +func TestAPMInvalidLockfile(t *testing.T) { |
| 285 | + for name, content := range map[string]string{ |
| 286 | + "malformed YAML": "dependencies: [unterminated\n", |
| 287 | + "missing repo_url": "dependencies:\n - resolved_commit: " + apmCommit + "\n", |
| 288 | + "unsupported version": "lockfile_version: \"3\"\nreplacements: []\ndependencies: []\n", |
| 289 | + } { |
| 290 | + t.Run(name, func(t *testing.T) { |
| 291 | + if _, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", content)); err == nil { |
| 292 | + t.Error("expected error") |
| 293 | + } |
| 294 | + }) |
| 295 | + } |
| 296 | +} |
| 297 | + |
| 298 | +func TestAPMMissingLockfile(t *testing.T) { |
| 299 | + _, err := resolve.ParseDir("apm", t.TempDir()) |
| 300 | + if !errors.Is(err, parsers.ErrAPMLockfileMissing) { |
| 301 | + t.Errorf("err = %v, want ErrAPMLockfileMissing", err) |
| 302 | + } |
| 303 | +} |
0 commit comments