Skip to content

Commit c6da768

Browse files
committed
internal/mod/module: add CheckPathWithoutVersion
This enables us to check that module path is valid when it doesn't have a version suffix. Signed-off-by: Roger Peppe <[email protected]> Change-Id: Ia28e2fb305c1c3e63a7b06cf898be4c844201f1f Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1170841 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 63307bd commit c6da768

File tree

2 files changed

+61
-24
lines changed

2 files changed

+61
-24
lines changed

internal/mod/module/module_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ func TestCheck(t *testing.T) {
4343
}
4444
}
4545

46+
var checkPathWithoutVersionTests = []struct {
47+
path string
48+
wantErr string
49+
}{{
50+
path: "rsc io/quote",
51+
wantErr: `invalid char ' '`,
52+
}, {
53+
path: "foo.com@v0",
54+
wantErr: `module path inappropriately contains major version`,
55+
}, {
56+
path: "foo.com/bar/baz",
57+
}}
58+
59+
func TestCheckPathWithoutVersion(t *testing.T) {
60+
for _, test := range checkPathWithoutVersionTests {
61+
t.Run(test.path, func(t *testing.T) {
62+
err := CheckPathWithoutVersion(test.path)
63+
if test.wantErr != "" {
64+
qt.Assert(t, qt.ErrorMatches(err, test.wantErr))
65+
return
66+
}
67+
qt.Assert(t, qt.IsNil(err))
68+
})
69+
}
70+
}
71+
4672
var newVersionTests = []struct {
4773
path, vers string
4874
wantError string

internal/mod/module/path.go

+35-24
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,40 @@ func fileNameOK(r rune) bool {
104104
return unicode.IsLetter(r)
105105
}
106106

107+
// CheckPathWithoutVersion is like CheckPath except that
108+
// it expects a module path without a major version.
109+
func CheckPathWithoutVersion(basePath string) (err error) {
110+
if _, _, ok := SplitPathVersion(basePath); ok {
111+
return fmt.Errorf("module path inappropriately contains major version")
112+
}
113+
if err := checkPath(basePath, modulePath); err != nil {
114+
return err
115+
}
116+
i := strings.Index(basePath, "/")
117+
if i < 0 {
118+
i = len(basePath)
119+
}
120+
if i == 0 {
121+
return fmt.Errorf("leading slash")
122+
}
123+
if !strings.Contains(basePath[:i], ".") {
124+
return fmt.Errorf("missing dot in first path element")
125+
}
126+
if basePath[0] == '-' {
127+
return fmt.Errorf("leading dash in first path element")
128+
}
129+
for _, r := range basePath[:i] {
130+
if !firstPathOK(r) {
131+
return fmt.Errorf("invalid char %q in first path element", r)
132+
}
133+
}
134+
// Sanity check agreement with OCI specs.
135+
if !basePathPat.MatchString(basePath) {
136+
return fmt.Errorf("non-conforming path %q", basePath)
137+
}
138+
return nil
139+
}
140+
107141
// CheckPath checks that a module path is valid.
108142
// A valid module path is a valid import path, as checked by CheckImportPath,
109143
// with three additional constraints.
@@ -135,32 +169,9 @@ func CheckPath(mpath string) (err error) {
135169
if semver.Major(vers) != vers {
136170
return fmt.Errorf("path can contain major version only")
137171
}
138-
139-
if err := checkPath(basePath, modulePath); err != nil {
172+
if err := CheckPathWithoutVersion(basePath); err != nil {
140173
return err
141174
}
142-
i := strings.Index(basePath, "/")
143-
if i < 0 {
144-
i = len(basePath)
145-
}
146-
if i == 0 {
147-
return fmt.Errorf("leading slash")
148-
}
149-
if !strings.Contains(basePath[:i], ".") {
150-
return fmt.Errorf("missing dot in first path element")
151-
}
152-
if basePath[0] == '-' {
153-
return fmt.Errorf("leading dash in first path element")
154-
}
155-
for _, r := range basePath[:i] {
156-
if !firstPathOK(r) {
157-
return fmt.Errorf("invalid char %q in first path element", r)
158-
}
159-
}
160-
// Sanity check agreement with OCI specs.
161-
if !basePathPat.MatchString(basePath) {
162-
return fmt.Errorf("non-conforming path %q", basePath)
163-
}
164175
if !tagPat.MatchString(vers) {
165176
return fmt.Errorf("non-conforming version %q", vers)
166177
}

0 commit comments

Comments
 (0)