internal/deps/resolved_test.go

112 lines · 3684 bytes

3 symbols in this file
  1package deps
  2
  3import "testing"
  4
  5func TestResolvedReadsLockfiles(t *testing.T) {
  6	got := found(t, Resolved(tree(map[string]string{
  7		"go.mod": `module x
  8
  9require (
 10	github.com/a/direct v1.0.0
 11	github.com/a/indirect v0.2.0 // indirect
 12	github.com/a/replaced v1.0.0
 13)
 14
 15replace github.com/a/replaced => ../replaced
 16`,
 17		"package-lock.json": `{"packages":{
 18			"": {"name":"app"},
 19			"node_modules/left-pad": {"version":"1.3.0"},
 20			"node_modules/a/node_modules/nested": {"version":"2.0.0"},
 21			"node_modules/linked": {"link": true}
 22		}}`,
 23		"package.json": `{"dependencies":{"ignored-when-locked":"1.0.0"}}`,
 24		"Cargo.lock": `
 25[[package]]
 26name = "serde"
 27version = "1.0.197"
 28source = "registry+https://github.com/rust-lang/crates.io-index"
 29
 30[[package]]
 31name = "member"
 32version = "0.1.0"
 33
 34[[package]]
 35name = "from-git"
 36version = "0.3.0"
 37source = "git+https://example.com/x.git#abc"
 38`,
 39		"uv.lock": `
 40[[package]]
 41name = "Requests"
 42version = "2.31.0"
 43source = { registry = "https://pypi.org/simple" }
 44
 45[[package]]
 46name = "app"
 47version = "0.1.0"
 48source = { editable = "." }
 49`,
 50		"Package.resolved": `{"pins":[
 51			{"identity":"vapor","location":"https://github.com/vapor/vapor.git","state":{"version":"4.0.0"}},
 52			{"identity":"branchy","location":"https://github.com/x/branchy","state":{"branch":"main","revision":"abc"}}
 53		],"version":2}`,
 54		"App.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved": `{"object":{"pins":[
 55			{"package":"Sparkle","repositoryURL":"git@github.com:sparkle-project/Sparkle.git","state":{"version":"2.6.0"}}
 56		]},"version":1}`,
 57	}), []string{"go.mod", "App.xcodeproj", "Sources"}))
 58
 59	want := map[string]string{
 60		"go:github.com/a/direct":                   "v1.0.0",
 61		"go:github.com/a/indirect":                 "v0.2.0",
 62		"npm:left-pad":                             "1.3.0",
 63		"npm:nested":                               "2.0.0",
 64		"cargo:serde":                              "1.0.197",
 65		"pypi:requests":                            "2.31.0",
 66		"swift:github.com/vapor/vapor":             "4.0.0",
 67		"swift:github.com/sparkle-project/Sparkle": "2.6.0",
 68	}
 69	for k, v := range want {
 70		if got[k] != v {
 71			t.Errorf("%s = %q, want %q", k, got[k], v)
 72		}
 73	}
 74	if len(got) != len(want) {
 75		t.Errorf("resolved %v, want exactly %v", got, want)
 76	}
 77}
 78
 79// Without a lockfile only exact pins are sent: a range's floor is rarely
 80// what is installed, and would match advisories the build does not have.
 81func TestResolvedWithoutLockfilesKeepsExactPins(t *testing.T) {
 82	got := found(t, Resolved(tree(map[string]string{
 83		"package.json":     `{"dependencies":{"exact":"1.2.3","caret":"^1.2.3","tilde":"~1.2.3"}}`,
 84		"Cargo.toml":       "[dependencies]\npinned = \"=1.0.0\"\ncaret = \"1.0\"\n",
 85		"requirements.txt": "flask==3.0.0\nrequests>=2.31\nbare\n",
 86	}), nil))
 87	want := map[string]string{"npm:exact": "1.2.3", "cargo:pinned": "1.0.0", "pypi:flask": "3.0.0"}
 88	for k, v := range want {
 89		if got[k] != v {
 90			t.Errorf("%s = %q, want %q", k, got[k], v)
 91		}
 92	}
 93	if len(got) != len(want) {
 94		t.Errorf("resolved %v, want exactly %v", got, want)
 95	}
 96}
 97
 98func TestSwiftURLName(t *testing.T) {
 99	for in, want := range map[string]string{
100		"https://github.com/vapor/vapor.git": "github.com/vapor/vapor",
101		"https://github.com/vapor/vapor":     "github.com/vapor/vapor",
102		"git@github.com:vapor/vapor.git":     "github.com/vapor/vapor",
103		"ssh://git@gitbay.org/krz/org-swift": "gitbay.org/krz/org-swift",
104		"https://user@example.com/a/b.git/":  "example.com/a/b",
105		"/Users/me/local-package":            "",
106		"../sibling":                         "",
107	} {
108		if got := swiftURLName(in); got != want {
109			t.Errorf("swiftURLName(%q) = %q, want %q", in, got, want)
110		}
111	}
112}