]>
Commit | Line | Data |
---|---|---|
dd931d9b ILT |
1 | // Copyright 2018 The Go Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style | |
3 | // license that can be found in the LICENSE file. | |
4 | ||
5 | package modload | |
6 | ||
7 | import ( | |
8 | "internal/testenv" | |
9 | "io/ioutil" | |
10 | "log" | |
11 | "os" | |
12 | "path" | |
13 | "path/filepath" | |
14 | "strings" | |
15 | "testing" | |
16 | ||
aa8901e9 | 17 | "cmd/go/internal/cfg" |
dd931d9b ILT |
18 | "cmd/go/internal/modfetch" |
19 | "cmd/go/internal/modfetch/codehost" | |
5a8ea165 ILT |
20 | |
21 | "golang.org/x/mod/module" | |
dd931d9b ILT |
22 | ) |
23 | ||
24 | func TestMain(m *testing.M) { | |
25 | os.Exit(testMain(m)) | |
26 | } | |
27 | ||
28 | func testMain(m *testing.M) int { | |
aa8901e9 ILT |
29 | cfg.GOPROXY = "direct" |
30 | ||
dd931d9b ILT |
31 | dir, err := ioutil.TempDir("", "modload-test-") |
32 | if err != nil { | |
33 | log.Fatal(err) | |
34 | } | |
35 | defer os.RemoveAll(dir) | |
aa8901e9 ILT |
36 | |
37 | os.Setenv("GOPATH", dir) | |
38 | cfg.BuildContext.GOPATH = dir | |
dd931d9b ILT |
39 | modfetch.PkgMod = filepath.Join(dir, "pkg/mod") |
40 | codehost.WorkRoot = filepath.Join(dir, "codework") | |
41 | return m.Run() | |
42 | } | |
43 | ||
44 | var ( | |
45 | queryRepo = "vcs-test.golang.org/git/querytest.git" | |
46 | queryRepoV2 = queryRepo + "/v2" | |
47 | queryRepoV3 = queryRepo + "/v3" | |
48 | ||
49 | // Empty version list (no semver tags), not actually empty. | |
50 | emptyRepo = "vcs-test.golang.org/git/emptytest.git" | |
51 | ) | |
52 | ||
53 | var queryTests = []struct { | |
aa8901e9 ILT |
54 | path string |
55 | query string | |
56 | current string | |
57 | allow string | |
58 | vers string | |
59 | err string | |
dd931d9b ILT |
60 | }{ |
61 | /* | |
62 | git init | |
63 | echo module vcs-test.golang.org/git/querytest.git >go.mod | |
64 | git add go.mod | |
65 | git commit -m v1 go.mod | |
66 | git tag start | |
aa8901e9 | 67 | for i in v0.0.0-pre1 v0.0.0 v0.0.1 v0.0.2 v0.0.3 v0.1.0 v0.1.1 v0.1.2 v0.3.0 v1.0.0 v1.1.0 v1.9.0 v1.9.9 v1.9.10-pre1 v1.9.10-pre2+metadata; do |
dd931d9b ILT |
68 | echo before $i >status |
69 | git add status | |
70 | git commit -m "before $i" status | |
71 | echo at $i >status | |
72 | git commit -m "at $i" status | |
73 | git tag $i | |
74 | done | |
75 | git tag favorite v0.0.3 | |
76 | ||
77 | git branch v2 start | |
78 | git checkout v2 | |
79 | echo module vcs-test.golang.org/git/querytest.git/v2 >go.mod | |
80 | git commit -m v2 go.mod | |
aa8901e9 | 81 | for i in v2.0.0 v2.1.0 v2.2.0 v2.5.5 v2.6.0-pre1; do |
dd931d9b ILT |
82 | echo before $i >status |
83 | git add status | |
84 | git commit -m "before $i" status | |
85 | echo at $i >status | |
86 | git commit -m "at $i" status | |
87 | git tag $i | |
88 | done | |
aa8901e9 | 89 | git checkout v2.5.5 |
dd931d9b ILT |
90 | echo after v2.5.5 >status |
91 | git commit -m 'after v2.5.5' status | |
92 | git checkout master | |
93 | zip -r ../querytest.zip | |
94 | gsutil cp ../querytest.zip gs://vcs-test/git/querytest.zip | |
95 | curl 'https://vcs-test.golang.org/git/querytest?go-get=1' | |
96 | */ | |
97 | {path: queryRepo, query: "<v0.0.0", vers: "v0.0.0-pre1"}, | |
98 | {path: queryRepo, query: "<v0.0.0-pre1", err: `no matching versions for query "<v0.0.0-pre1"`}, | |
99 | {path: queryRepo, query: "<=v0.0.0", vers: "v0.0.0"}, | |
100 | {path: queryRepo, query: ">v0.0.0", vers: "v0.0.1"}, | |
101 | {path: queryRepo, query: ">=v0.0.0", vers: "v0.0.0"}, | |
102 | {path: queryRepo, query: "v0.0.1", vers: "v0.0.1"}, | |
103 | {path: queryRepo, query: "v0.0.1+foo", vers: "v0.0.1"}, | |
656297e1 | 104 | {path: queryRepo, query: "v0.0.99", err: `vcs-test.golang.org/git/querytest.git@v0.0.99: invalid version: unknown revision v0.0.99`}, |
dd931d9b ILT |
105 | {path: queryRepo, query: "v0", vers: "v0.3.0"}, |
106 | {path: queryRepo, query: "v0.1", vers: "v0.1.2"}, | |
107 | {path: queryRepo, query: "v0.2", err: `no matching versions for query "v0.2"`}, | |
108 | {path: queryRepo, query: "v0.0", vers: "v0.0.3"}, | |
aa8901e9 ILT |
109 | {path: queryRepo, query: "v1.9.10-pre2+metadata", vers: "v1.9.10-pre2.0.20190513201126-42abcb6df8ee"}, |
110 | ||
111 | // golang.org/issue/29262: The major version for for a module without a suffix | |
112 | // should be based on the most recent tag (v1 as appropriate, not v0 | |
113 | // unconditionally). | |
114 | {path: queryRepo, query: "42abcb6df8ee", vers: "v1.9.10-pre2.0.20190513201126-42abcb6df8ee"}, | |
115 | ||
656297e1 ILT |
116 | {path: queryRepo, query: "v1.9.10-pre2+wrongmetadata", err: `vcs-test.golang.org/git/querytest.git@v1.9.10-pre2+wrongmetadata: invalid version: unknown revision v1.9.10-pre2+wrongmetadata`}, |
117 | {path: queryRepo, query: "v1.9.10-pre2", err: `vcs-test.golang.org/git/querytest.git@v1.9.10-pre2: invalid version: unknown revision v1.9.10-pre2`}, | |
dd931d9b | 118 | {path: queryRepo, query: "latest", vers: "v1.9.9"}, |
656297e1 ILT |
119 | {path: queryRepo, query: "latest", current: "v1.9.10-pre1", vers: "v1.9.9"}, |
120 | {path: queryRepo, query: "upgrade", vers: "v1.9.9"}, | |
121 | {path: queryRepo, query: "upgrade", current: "v1.9.10-pre1", vers: "v1.9.10-pre1"}, | |
122 | {path: queryRepo, query: "upgrade", current: "v1.9.10-pre2+metadata", vers: "v1.9.10-pre2.0.20190513201126-42abcb6df8ee"}, | |
123 | {path: queryRepo, query: "upgrade", current: "v0.0.0-20190513201126-42abcb6df8ee", vers: "v0.0.0-20190513201126-42abcb6df8ee"}, | |
124 | {path: queryRepo, query: "upgrade", allow: "NOMATCH", err: `no matching versions for query "upgrade"`}, | |
125 | {path: queryRepo, query: "upgrade", current: "v1.9.9", allow: "NOMATCH", err: `no matching versions for query "upgrade" (current version is v1.9.9)`}, | |
126 | {path: queryRepo, query: "upgrade", current: "v1.99.99", err: `vcs-test.golang.org/git/querytest.git@v1.99.99: invalid version: unknown revision v1.99.99`}, | |
aa8901e9 ILT |
127 | {path: queryRepo, query: "patch", current: "", vers: "v1.9.9"}, |
128 | {path: queryRepo, query: "patch", current: "v0.1.0", vers: "v0.1.2"}, | |
129 | {path: queryRepo, query: "patch", current: "v1.9.0", vers: "v1.9.9"}, | |
130 | {path: queryRepo, query: "patch", current: "v1.9.10-pre1", vers: "v1.9.10-pre1"}, | |
131 | {path: queryRepo, query: "patch", current: "v1.9.10-pre2+metadata", vers: "v1.9.10-pre2.0.20190513201126-42abcb6df8ee"}, | |
132 | {path: queryRepo, query: "patch", current: "v1.99.99", err: `no matching versions for query "patch" (current version is v1.99.99)`}, | |
dd931d9b ILT |
133 | {path: queryRepo, query: ">v1.9.9", vers: "v1.9.10-pre1"}, |
134 | {path: queryRepo, query: ">v1.10.0", err: `no matching versions for query ">v1.10.0"`}, | |
135 | {path: queryRepo, query: ">=v1.10.0", err: `no matching versions for query ">=v1.10.0"`}, | |
136 | {path: queryRepo, query: "6cf84eb", vers: "v0.0.2-0.20180704023347-6cf84ebaea54"}, | |
aa8901e9 ILT |
137 | |
138 | // golang.org/issue/27173: A pseudo-version may be based on the highest tag on | |
139 | // any parent commit, or any existing semantically-lower tag: a given commit | |
140 | // could have been a pre-release for a backport tag at any point. | |
141 | {path: queryRepo, query: "3ef0cec634e0", vers: "v0.1.2-0.20180704023347-3ef0cec634e0"}, | |
142 | {path: queryRepo, query: "v0.1.2-0.20180704023347-3ef0cec634e0", vers: "v0.1.2-0.20180704023347-3ef0cec634e0"}, | |
143 | {path: queryRepo, query: "v0.1.1-0.20180704023347-3ef0cec634e0", vers: "v0.1.1-0.20180704023347-3ef0cec634e0"}, | |
144 | {path: queryRepo, query: "v0.0.4-0.20180704023347-3ef0cec634e0", vers: "v0.0.4-0.20180704023347-3ef0cec634e0"}, | |
145 | ||
146 | // Invalid tags are tested in cmd/go/testdata/script/mod_pseudo_invalid.txt. | |
147 | ||
dd931d9b | 148 | {path: queryRepo, query: "start", vers: "v0.0.0-20180704023101-5e9e31667ddf"}, |
aa8901e9 ILT |
149 | {path: queryRepo, query: "5e9e31667ddf", vers: "v0.0.0-20180704023101-5e9e31667ddf"}, |
150 | {path: queryRepo, query: "v0.0.0-20180704023101-5e9e31667ddf", vers: "v0.0.0-20180704023101-5e9e31667ddf"}, | |
151 | ||
dd931d9b ILT |
152 | {path: queryRepo, query: "7a1b6bf", vers: "v0.1.0"}, |
153 | ||
154 | {path: queryRepoV2, query: "<v0.0.0", err: `no matching versions for query "<v0.0.0"`}, | |
155 | {path: queryRepoV2, query: "<=v0.0.0", err: `no matching versions for query "<=v0.0.0"`}, | |
156 | {path: queryRepoV2, query: ">v0.0.0", vers: "v2.0.0"}, | |
157 | {path: queryRepoV2, query: ">=v0.0.0", vers: "v2.0.0"}, | |
aa8901e9 ILT |
158 | |
159 | {path: queryRepoV2, query: "v2", vers: "v2.5.5"}, | |
160 | {path: queryRepoV2, query: "v2.5", vers: "v2.5.5"}, | |
161 | {path: queryRepoV2, query: "v2.6", err: `no matching versions for query "v2.6"`}, | |
162 | {path: queryRepoV2, query: "v2.6.0-pre1", vers: "v2.6.0-pre1"}, | |
dd931d9b ILT |
163 | {path: queryRepoV2, query: "latest", vers: "v2.5.5"}, |
164 | ||
656297e1 ILT |
165 | // e0cf3de987e6 is the latest commit on the master branch, and it's actually |
166 | // v1.19.10-pre1, not anything resembling v3: attempting to query it as such | |
167 | // should fail. | |
168 | {path: queryRepoV3, query: "e0cf3de987e6", err: `vcs-test.golang.org/git/querytest.git/v3@v3.0.0-20180704024501-e0cf3de987e6: invalid version: go.mod has non-.../v3 module path "vcs-test.golang.org/git/querytest.git" (and .../v3/go.mod does not exist) at revision e0cf3de987e6`}, | |
169 | {path: queryRepoV3, query: "latest", err: `no matching versions for query "latest"`}, | |
dd931d9b ILT |
170 | |
171 | {path: emptyRepo, query: "latest", vers: "v0.0.0-20180704023549-7bb914627242"}, | |
172 | {path: emptyRepo, query: ">v0.0.0", err: `no matching versions for query ">v0.0.0"`}, | |
173 | {path: emptyRepo, query: "<v10.0.0", err: `no matching versions for query "<v10.0.0"`}, | |
174 | } | |
175 | ||
176 | func TestQuery(t *testing.T) { | |
177 | testenv.MustHaveExternalNetwork(t) | |
aa8901e9 | 178 | testenv.MustHaveExecPath(t, "git") |
dd931d9b ILT |
179 | |
180 | for _, tt := range queryTests { | |
181 | allow := tt.allow | |
182 | if allow == "" { | |
183 | allow = "*" | |
184 | } | |
185 | allowed := func(m module.Version) bool { | |
186 | ok, _ := path.Match(allow, m.Version) | |
187 | return ok | |
188 | } | |
656297e1 | 189 | tt := tt |
aa8901e9 | 190 | t.Run(strings.ReplaceAll(tt.path, "/", "_")+"/"+tt.query+"/"+tt.current+"/"+allow, func(t *testing.T) { |
656297e1 ILT |
191 | t.Parallel() |
192 | ||
aa8901e9 | 193 | info, err := Query(tt.path, tt.query, tt.current, allowed) |
dd931d9b | 194 | if tt.err != "" { |
aa8901e9 ILT |
195 | if err == nil { |
196 | t.Errorf("Query(%q, %q, %v) = %v, want error %q", tt.path, tt.query, allow, info.Version, tt.err) | |
197 | } else if err.Error() != tt.err { | |
198 | t.Errorf("Query(%q, %q, %v): %v, want error %q", tt.path, tt.query, allow, err, tt.err) | |
dd931d9b | 199 | } |
aa8901e9 | 200 | return |
dd931d9b ILT |
201 | } |
202 | if err != nil { | |
203 | t.Fatalf("Query(%q, %q, %v): %v", tt.path, tt.query, allow, err) | |
204 | } | |
205 | if info.Version != tt.vers { | |
206 | t.Errorf("Query(%q, %q, %v) = %v, want %v", tt.path, tt.query, allow, info.Version, tt.vers) | |
207 | } | |
208 | }) | |
209 | } | |
210 | } |