-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mod/modfile: add ModuleForImportPath
This makes it straightforward to resolve modules and default major versions for packages without consulting a registry or invoking any of the code inside internal/mod. This is valid to do for packages that are part of the current dependency graph as long as the module is tidy, because the tidy algorithm guarantees that all dependency modules are present in the module.cue file. Signed-off-by: Roger Peppe <[email protected]> Change-Id: Ia0d86b335e2c1f2963e75f7901322fb55a842643 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1207985 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
- Loading branch information
Showing
2 changed files
with
109 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,13 +27,14 @@ import ( | |
) | ||
|
||
var parseTests = []struct { | ||
testName string | ||
parse func(modfile []byte, filename string) (*File, error) | ||
data string | ||
wantError string | ||
want *File | ||
wantVersions []module.Version | ||
wantDefaults map[string]string | ||
testName string | ||
parse func(modfile []byte, filename string) (*File, error) | ||
data string | ||
wantError string | ||
want *File | ||
wantVersions []module.Version | ||
wantDefaults map[string]string | ||
wantModVersionForPkg map[string]string | ||
}{{ | ||
testName: "NoDeps", | ||
parse: Parse, | ||
|
@@ -50,6 +51,15 @@ language: version: "v0.8.0-alpha.0" | |
wantDefaults: map[string]string{ | ||
"foo.com/bar": "v0", | ||
}, | ||
wantModVersionForPkg: map[string]string{ | ||
"foo.com/bar": "foo.com/bar@v0", | ||
"foo.com/bar@v0": "foo.com/bar@v0", | ||
"foo.com/bar/baz@v0": "foo.com/bar@v0", | ||
"foo.com/bar@v1": "", | ||
"foo.com/bar:hello": "foo.com/bar@v0", | ||
"foo.com/bar/baz:hello": "foo.com/bar@v0", | ||
"foo.com/bar/baz@v0:hello": "foo.com/bar@v0", | ||
}, | ||
}, { | ||
testName: "WithDeps", | ||
parse: Parse, | ||
|
@@ -60,6 +70,11 @@ deps: "example.com@v1": { | |
default: true | ||
v: "v1.2.3" | ||
} | ||
deps: "example.com/other@v1": v: "v1.9.10" | ||
deps: "example.com/other/more/nested@v2": { | ||
v: "v2.9.20" | ||
default: true | ||
} | ||
deps: "other.com/something@v0": v: "v0.2.3" | ||
`, | ||
want: &File{ | ||
|
@@ -75,12 +90,36 @@ deps: "other.com/something@v0": v: "v0.2.3" | |
"other.com/something@v0": { | ||
Version: "v0.2.3", | ||
}, | ||
"example.com/other@v1": { | ||
Version: "v1.9.10", | ||
}, | ||
"example.com/other/more/nested@v2": { | ||
Version: "v2.9.20", | ||
Default: true, | ||
}, | ||
}, | ||
}, | ||
wantVersions: parseVersions("[email protected]", "other.com/[email protected]"), | ||
wantVersions: parseVersions( | ||
"example.com/other/more/[email protected]", | ||
"example.com/[email protected]", | ||
"[email protected]", | ||
"other.com/[email protected]", | ||
), | ||
wantDefaults: map[string]string{ | ||
"foo.com/bar": "v0", | ||
"example.com": "v1", | ||
"example.com/other/more/nested": "v2", | ||
"foo.com/bar": "v0", | ||
"example.com": "v1", | ||
}, | ||
wantModVersionForPkg: map[string]string{ | ||
"example.com": "[email protected]", | ||
"example.com/x/y@v1": "[email protected]", | ||
"example.com/x/y@v1:x": "[email protected]", | ||
"example.com/other@v1": "example.com/[email protected]", | ||
"example.com/other/p@v1": "example.com/[email protected]", | ||
"example.com/other/more": "[email protected]", | ||
"example.com/other/more@v1": "example.com/[email protected]", | ||
"example.com/other/more/nested": "example.com/other/more/[email protected]", | ||
"example.com/other/more/nested/x:p": "example.com/other/more/[email protected]", | ||
}, | ||
}, { | ||
testName: "WithSource", | ||
|
@@ -270,6 +309,12 @@ deps: "example.com": v: "v1.2.3" | |
wantDefaults: map[string]string{ | ||
"foo.com/bar": "v0", | ||
}, | ||
wantModVersionForPkg: map[string]string{ | ||
"example.com": "", // No default major version. | ||
"example.com@v1": "[email protected]", | ||
"example.com/x/y@v1": "[email protected]", | ||
"example.com/x/y@v1:x": "[email protected]", | ||
}, | ||
}, { | ||
testName: "LegacyWithExtraFields", | ||
parse: ParseLegacy, | ||
|
@@ -445,6 +490,17 @@ func TestParse(t *testing.T) { | |
qt.Assert(t, qt.Equals(f.ModulePath(), f.Module)) | ||
qt.Assert(t, qt.Equals(f.MajorVersion(), "v0")) | ||
} | ||
for p, m := range test.wantModVersionForPkg { | ||
t.Run("package-"+p, func(t *testing.T) { | ||
mv, ok := f.ModuleForImportPath(p) | ||
if m == "" { | ||
qt.Assert(t, qt.IsFalse(ok), qt.Commentf("got version %v", mv)) | ||
return | ||
} | ||
qt.Check(t, qt.IsTrue(ok)) | ||
qt.Check(t, qt.Equals(mv.String(), m)) | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
|