github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/modload/query_test.go (about) 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 "github.com/gagliardetto/golang-go/not-internal/testenv" 9 "io/ioutil" 10 "log" 11 "os" 12 "path" 13 "path/filepath" 14 "strings" 15 "testing" 16 17 "github.com/gagliardetto/golang-go/cmd/go/not-internal/cfg" 18 "github.com/gagliardetto/golang-go/cmd/go/not-internal/modfetch" 19 "github.com/gagliardetto/golang-go/cmd/go/not-internal/modfetch/codehost" 20 21 "golang.org/x/mod/module" 22 ) 23 24 func TestMain(m *testing.M) { 25 os.Exit(testMain(m)) 26 } 27 28 func testMain(m *testing.M) int { 29 cfg.GOPROXY = "direct" 30 31 dir, err := ioutil.TempDir("", "modload-test-") 32 if err != nil { 33 log.Fatal(err) 34 } 35 defer os.RemoveAll(dir) 36 37 os.Setenv("GOPATH", dir) 38 cfg.BuildContext.GOPATH = dir 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 { 54 path string 55 query string 56 current string 57 allow string 58 vers string 59 err string 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 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 unversioned; do 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 81 for i in v2.0.0 v2.1.0 v2.2.0 v2.5.5 v2.6.0-pre1; do 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 89 git checkout v2.5.5 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"}, 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`}, 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"}, 109 {path: queryRepo, query: "v1.9.10-pre2+metadata", vers: "v1.9.10-pre2.0.20190513201126-42abcb6df8ee"}, 110 {path: queryRepo, query: "ed5ffdaa", vers: "v1.9.10-pre2.0.20191220134614-ed5ffdaa1f5e"}, 111 112 // golang.org/issue/29262: The major version for for a module without a suffix 113 // should be based on the most recent tag (v1 as appropriate, not v0 114 // unconditionally). 115 {path: queryRepo, query: "42abcb6df8ee", vers: "v1.9.10-pre2.0.20190513201126-42abcb6df8ee"}, 116 117 {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`}, 118 {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`}, 119 {path: queryRepo, query: "latest", vers: "v1.9.9"}, 120 {path: queryRepo, query: "latest", current: "v1.9.10-pre1", vers: "v1.9.9"}, 121 {path: queryRepo, query: "upgrade", vers: "v1.9.9"}, 122 {path: queryRepo, query: "upgrade", current: "v1.9.10-pre1", vers: "v1.9.10-pre1"}, 123 {path: queryRepo, query: "upgrade", current: "v1.9.10-pre2+metadata", vers: "v1.9.10-pre2.0.20190513201126-42abcb6df8ee"}, 124 {path: queryRepo, query: "upgrade", current: "v0.0.0-20190513201126-42abcb6df8ee", vers: "v0.0.0-20190513201126-42abcb6df8ee"}, 125 {path: queryRepo, query: "upgrade", allow: "NOMATCH", err: `no matching versions for query "upgrade"`}, 126 {path: queryRepo, query: "upgrade", current: "v1.9.9", allow: "NOMATCH", err: `no matching versions for query "upgrade" (current version is v1.9.9)`}, 127 {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`}, 128 {path: queryRepo, query: "patch", current: "", vers: "v1.9.9"}, 129 {path: queryRepo, query: "patch", current: "v0.1.0", vers: "v0.1.2"}, 130 {path: queryRepo, query: "patch", current: "v1.9.0", vers: "v1.9.9"}, 131 {path: queryRepo, query: "patch", current: "v1.9.10-pre1", vers: "v1.9.10-pre1"}, 132 {path: queryRepo, query: "patch", current: "v1.9.10-pre2+metadata", vers: "v1.9.10-pre2.0.20190513201126-42abcb6df8ee"}, 133 {path: queryRepo, query: "patch", current: "v1.99.99", err: `no matching versions for query "patch" (current version is v1.99.99)`}, 134 {path: queryRepo, query: ">v1.9.9", vers: "v1.9.10-pre1"}, 135 {path: queryRepo, query: ">v1.10.0", err: `no matching versions for query ">v1.10.0"`}, 136 {path: queryRepo, query: ">=v1.10.0", err: `no matching versions for query ">=v1.10.0"`}, 137 {path: queryRepo, query: "6cf84eb", vers: "v0.0.2-0.20180704023347-6cf84ebaea54"}, 138 139 // golang.org/issue/27173: A pseudo-version may be based on the highest tag on 140 // any parent commit, or any existing semantically-lower tag: a given commit 141 // could have been a pre-release for a backport tag at any point. 142 {path: queryRepo, query: "3ef0cec634e0", vers: "v0.1.2-0.20180704023347-3ef0cec634e0"}, 143 {path: queryRepo, query: "v0.1.2-0.20180704023347-3ef0cec634e0", vers: "v0.1.2-0.20180704023347-3ef0cec634e0"}, 144 {path: queryRepo, query: "v0.1.1-0.20180704023347-3ef0cec634e0", vers: "v0.1.1-0.20180704023347-3ef0cec634e0"}, 145 {path: queryRepo, query: "v0.0.4-0.20180704023347-3ef0cec634e0", vers: "v0.0.4-0.20180704023347-3ef0cec634e0"}, 146 147 // Invalid tags are tested in cmd/go/testdata/script/mod_pseudo_invalid.txt. 148 149 {path: queryRepo, query: "start", vers: "v0.0.0-20180704023101-5e9e31667ddf"}, 150 {path: queryRepo, query: "5e9e31667ddf", vers: "v0.0.0-20180704023101-5e9e31667ddf"}, 151 {path: queryRepo, query: "v0.0.0-20180704023101-5e9e31667ddf", vers: "v0.0.0-20180704023101-5e9e31667ddf"}, 152 153 {path: queryRepo, query: "7a1b6bf", vers: "v0.1.0"}, 154 155 {path: queryRepoV2, query: "<v0.0.0", err: `no matching versions for query "<v0.0.0"`}, 156 {path: queryRepoV2, query: "<=v0.0.0", err: `no matching versions for query "<=v0.0.0"`}, 157 {path: queryRepoV2, query: ">v0.0.0", vers: "v2.0.0"}, 158 {path: queryRepoV2, query: ">=v0.0.0", vers: "v2.0.0"}, 159 160 {path: queryRepoV2, query: "v2", vers: "v2.5.5"}, 161 {path: queryRepoV2, query: "v2.5", vers: "v2.5.5"}, 162 {path: queryRepoV2, query: "v2.6", err: `no matching versions for query "v2.6"`}, 163 {path: queryRepoV2, query: "v2.6.0-pre1", vers: "v2.6.0-pre1"}, 164 {path: queryRepoV2, query: "latest", vers: "v2.5.5"}, 165 166 // Commit e0cf3de987e6 is actually v1.19.10-pre1, not anything resembling v3, 167 // and it has a go.mod file with a non-v3 module path. Attempting to query it 168 // as the v3 module should fail. 169 {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`}, 170 171 // The querytest repo does not have any commits tagged with major version 3, 172 // and the latest commit in the repo has a go.mod file specifying a non-v3 path. 173 // That should prevent us from resolving any version for the /v3 path. 174 {path: queryRepoV3, query: "latest", err: `no matching versions for query "latest"`}, 175 176 {path: emptyRepo, query: "latest", vers: "v0.0.0-20180704023549-7bb914627242"}, 177 {path: emptyRepo, query: ">v0.0.0", err: `no matching versions for query ">v0.0.0"`}, 178 {path: emptyRepo, query: "<v10.0.0", err: `no matching versions for query "<v10.0.0"`}, 179 } 180 181 func TestQuery(t *testing.T) { 182 testenv.MustHaveExternalNetwork(t) 183 testenv.MustHaveExecPath(t, "git") 184 185 for _, tt := range queryTests { 186 allow := tt.allow 187 if allow == "" { 188 allow = "*" 189 } 190 allowed := func(m module.Version) bool { 191 ok, _ := path.Match(allow, m.Version) 192 return ok 193 } 194 tt := tt 195 t.Run(strings.ReplaceAll(tt.path, "/", "_")+"/"+tt.query+"/"+tt.current+"/"+allow, func(t *testing.T) { 196 t.Parallel() 197 198 info, err := Query(tt.path, tt.query, tt.current, allowed) 199 if tt.err != "" { 200 if err == nil { 201 t.Errorf("Query(%q, %q, %v) = %v, want error %q", tt.path, tt.query, allow, info.Version, tt.err) 202 } else if err.Error() != tt.err { 203 t.Errorf("Query(%q, %q, %v): %v, want error %q", tt.path, tt.query, allow, err, tt.err) 204 } 205 return 206 } 207 if err != nil { 208 t.Fatalf("Query(%q, %q, %v): %v", tt.path, tt.query, allow, err) 209 } 210 if info.Version != tt.vers { 211 t.Errorf("Query(%q, %q, %v) = %v, want %v", tt.path, tt.query, allow, info.Version, tt.vers) 212 } 213 }) 214 } 215 }