Skip to content

Commit e3f15e3

Browse files
committed
cmd/go: convert even more module tests to scripts
Change-Id: Iba185e00e9df2462e9089566053f6c64e24a6a92 Reviewed-on: https://go-review.googlesource.com/124698 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 4d5bf3c commit e3f15e3

File tree

8 files changed

+251
-357
lines changed

8 files changed

+251
-357
lines changed

src/cmd/go/mod_test.go

Lines changed: 0 additions & 357 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"internal/testenv"
1010
"io/ioutil"
1111
"os"
12-
"os/exec"
1312
"path/filepath"
1413
"regexp"
1514
"sort"
@@ -635,62 +634,6 @@ func TestModGetUpgrade(t *testing.T) {
635634
tg.grepStderr(`go get: disabled by -getmode=vendor`, "expected disabled")
636635
}
637636

638-
func TestModPathCase(t *testing.T) {
639-
tg := testGoModules(t)
640-
defer tg.cleanup()
641-
642-
tg.run("get", "rsc.io/QUOTE")
643-
644-
tg.run("list", "-m", "all")
645-
tg.grepStdout(`^rsc.io/quote v1.5.2`, "want lower-case quote v1.5.2")
646-
tg.grepStdout(`^rsc.io/QUOTE v1.5.2`, "want upper-case quote v1.5.2")
647-
648-
// Note: the package is rsc.io/QUOTE/QUOTE to avoid
649-
// a case-sensitive import collision error in load/pkg.go.
650-
tg.run("list", "-f=DEPS {{.Deps}}\nDIR {{.Dir}}", "rsc.io/QUOTE/QUOTE")
651-
tg.grepStdout(`DEPS.*rsc.io/quote`, "want quote as dep")
652-
tg.grepStdout(`DIR.*!q!u!o!t!e`, "want !q!u!o!t!e in directory name")
653-
}
654-
655-
func TestModFileNames(t *testing.T) {
656-
tg := testGoModules(t)
657-
defer tg.cleanup()
658-
659-
tg.runFail("get",
660-
"rsc.io/badfile1",
661-
"rsc.io/badfile2",
662-
"rsc.io/badfile3",
663-
"rsc.io/badfile4",
664-
"rsc.io/badfile5",
665-
)
666-
tg.grepStderrNot(`unzip .*badfile1.*:`, "badfile1 should be OK")
667-
tg.grepStderr(`rsc.io/badfile2.*malformed file path "☺.go": invalid char '☺'`, "want diagnosed invalid character")
668-
tg.grepStderr(`rsc.io/badfile3.*malformed file path "x\?y.go": invalid char '\?'`, "want diagnosed invalid character")
669-
tg.grepStderr(`rsc.io/badfile4.*case-insensitive file name collision: "x/Y.go" and "x/y.go"`, "want case collision")
670-
tg.grepStderr(`rsc.io/badfile5.*case-insensitive file name collision: "x/y" and "x/Y"`, "want case collision")
671-
}
672-
673-
func TestModBadDomain(t *testing.T) {
674-
tg := testGoModules(t)
675-
defer tg.cleanup()
676-
677-
tg.tempFile("work/x.go", `
678-
package x
679-
680-
import _ "appengine"
681-
import _ "nonexistent.rsc.io" // domain does not exist
682-
`)
683-
684-
tg.runFail("get", "appengine")
685-
tg.grepStderr(`cannot find module providing package appengine`, "expected module error ")
686-
tg.runFail("get", "x/y.z")
687-
tg.grepStderr(`cannot find module providing package x/y.z`, "expected module error")
688-
689-
tg.runFail("build")
690-
tg.grepStderrNot("unknown module appengine: not a domain name", "expected nothing about appengine")
691-
tg.grepStderr("cannot find module providing package nonexistent.rsc.io", "expected error for nonexistent.rsc.io")
692-
}
693-
694637
func TestModSync(t *testing.T) {
695638
tg := testGoModules(t)
696639
defer tg.cleanup()
@@ -851,306 +794,6 @@ func TestModVendor(t *testing.T) {
851794
}
852795
}
853796

854-
func TestModList(t *testing.T) {
855-
tg := testGoModules(t)
856-
defer tg.cleanup()
857-
858-
tg.setenv(homeEnvName(), tg.path("."))
859-
tg.must(os.MkdirAll(tg.path("x"), 0777))
860-
tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`
861-
package x
862-
import _ "rsc.io/quote"
863-
`), 0666))
864-
tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
865-
module x
866-
require rsc.io/quote v1.5.2
867-
`), 0666))
868-
tg.cd(tg.path("x"))
869-
870-
tg.run("list", "-m", "-f={{.Main}}: {{.Dir}}")
871-
tg.grepStdout(`^true: `, "expected main module to have Main=true")
872-
tg.grepStdout(regexp.QuoteMeta(tg.path("x")), "expected Dir of main module to be present")
873-
874-
tg.run("list", "-m", "-f={{.Main}}: {{.Dir}}", "rsc.io/quote")
875-
tg.grepStdout(`^false: `, "expected non-main module to have Main=false")
876-
tg.grepStdoutNot(`quote@`, "should not have local copy of code")
877-
878-
tg.run("list", "-f={{.Dir}}", "rsc.io/quote") // downloads code to load package
879-
tg.grepStdout(`mod[\\/]rsc.io[\\/][email protected]`, "expected cached copy of code")
880-
dir := strings.TrimSpace(tg.getStdout())
881-
info, err := os.Stat(dir)
882-
if err != nil {
883-
t.Fatal(err)
884-
}
885-
if info.Mode()&0222 != 0 {
886-
t.Fatalf("%s should be unwritable", dir)
887-
}
888-
info, err = os.Stat(filepath.Join(dir, "buggy"))
889-
if err != nil {
890-
t.Fatal(err)
891-
}
892-
if info.Mode()&0222 != 0 {
893-
t.Fatalf("%s should be unwritable", filepath.Join(dir, "buggy"))
894-
}
895-
896-
tg.run("clean", "-modcache")
897-
if _, err = os.Stat(dir); err == nil {
898-
t.Fatal("clean -modcache did not remove download dir")
899-
}
900-
901-
tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
902-
module x
903-
require rsc.io/quote v1.5.1
904-
replace rsc.io/sampler v1.3.0 => rsc.io/sampler v1.3.1
905-
`), 0666))
906-
907-
tg.run("list", "-f={{.Dir}}", "rsc.io/quote") // downloads code to load package
908-
tg.run("list", "-m", "-f={{.Path}} {{.Version}} {{.Dir}}{{with .Replace}} => {{.Version}} {{.Dir}}{{end}}", "all")
909-
tg.grepStdout(`mod[\\/]rsc.io[\\/][email protected]`, "expected cached copy of code")
910-
tg.grepStdout(`v1.3.0 .*mod[\\/]rsc.io[\\/][email protected] => v1.3.1 .*@v1.3.1`, "expected v1.3.1 replacement")
911-
912-
// check that list std works; also check that rsc.io/quote/buggy is a listable package
913-
tg.run("list", "std", "rsc.io/quote/buggy")
914-
tg.grepStdout("^math/big", "expected standard library")
915-
916-
tg.run("list", "-m", "-e", "-f={{.Path}} {{.Error.Err}}", "nonexist", "rsc.io/quote/buggy")
917-
tg.grepStdout(`^nonexist module "nonexist" is not a known dependency`, "expected error via template")
918-
tg.grepStdout(`^rsc.io/quote/buggy module "rsc.io/quote/buggy" is not a known dependency`, "expected error via template")
919-
920-
tg.runFail("list", "-m", "nonexist", "rsc.io/quote/buggy")
921-
tg.grepStderr(`go list -m nonexist: module "nonexist" is not a known dependency`, "expected error on stderr")
922-
tg.grepStderr(`go list -m rsc.io/quote/buggy: module "rsc.io/quote/buggy" is not a known dependency`, "expected error on stderr")
923-
924-
// Check that module loader does not interfere with list -e (golang.org/issue/24149).
925-
tg.run("list", "-e", "-f={{.ImportPath}} {{.Error.Err}}", "database")
926-
tg.grepStdout(`^database no Go files in `, "expected error via template")
927-
tg.runFail("list", "database")
928-
tg.grepStderr(`package database: no Go files`, "expected error on stderr")
929-
930-
}
931-
932-
func TestModInitLegacy(t *testing.T) {
933-
testenv.MustHaveExternalNetwork(t)
934-
if _, err := exec.LookPath("git"); err != nil {
935-
t.Skip("skipping because git binary not found")
936-
}
937-
938-
tg := testGoModules(t)
939-
defer tg.cleanup()
940-
941-
tg.setenv(homeEnvName(), tg.path("."))
942-
tg.must(os.MkdirAll(tg.path("x"), 0777))
943-
tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`
944-
package x
945-
`), 0666))
946-
947-
tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
948-
module x
949-
`), 0666))
950-
tg.must(ioutil.WriteFile(tg.path("x/Gopkg.lock"), []byte(`
951-
[[projects]]
952-
name = "rsc.io/sampler"
953-
version = "v1.0.0"
954-
`), 0666))
955-
956-
tg.cd(tg.path("x"))
957-
tg.run("build", "-v")
958-
tg.grepStderr("copying requirements from .*Gopkg.lock", "did not copy Gopkg.lock")
959-
tg.run("list", "-m", "all")
960-
tg.grepStderrNot("copying requirements from .*Gopkg.lock", "should not copy Gopkg.lock again")
961-
tg.grepStdout("rsc.io/sampler.*v1.0.0", "did not copy Gopkg.lock")
962-
963-
tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
964-
module x
965-
`), 0666))
966-
tg.must(ioutil.WriteFile(tg.path("x/Gopkg.lock"), []byte(`
967-
`), 0666))
968-
969-
tg.run("list")
970-
tg.grepStderr("copying requirements from .*Gopkg.lock", "did not copy Gopkg.lock")
971-
tg.run("list")
972-
tg.grepStderrNot("copying requirements from .*Gopkg.lock", "should not copy Gopkg.lock again")
973-
}
974-
975-
func TestModInitLegacy2(t *testing.T) {
976-
testenv.MustHaveExternalNetwork(t)
977-
if _, err := exec.LookPath("git"); err != nil {
978-
t.Skip("skipping because git binary not found")
979-
}
980-
981-
tg := testGoModules(t)
982-
defer tg.cleanup()
983-
984-
tg.setenv(homeEnvName(), tg.path("."))
985-
986-
// Testing that on Windows the path x/Gopkg.lock turning into x\Gopkg.lock does not confuse converter.
987-
tg.must(os.MkdirAll(tg.path("x"), 0777))
988-
tg.must(ioutil.WriteFile(tg.path("x/Gopkg.lock"), []byte(`
989-
[[projects]]
990-
name = "rsc.io/quote"
991-
packages = ["."]
992-
revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
993-
version = "v1.4.0"`), 0666))
994-
tg.must(ioutil.WriteFile(tg.path("x/main.go"), []byte("package x // import \"x\"\n import _ \"github.com/pkg/errors\""), 0666))
995-
tg.cd(tg.path("x"))
996-
tg.run("list", "-m", "all")
997-
998-
// If the conversion just ignored the Gopkg.lock entirely
999-
// it would choose a newer version (like v1.5.2 or maybe
1000-
// something even newer). Check for the older version to
1001-
// make sure Gopkg.lock was properly used.
1002-
tg.grepStdout("v1.4.0", "expected rsc.io/quote at v1.4.0")
1003-
}
1004-
1005-
func TestModVerify(t *testing.T) {
1006-
tg := testGoModules(t)
1007-
defer tg.cleanup()
1008-
1009-
gopath := tg.path("gp")
1010-
tg.setenv("GOPATH", gopath)
1011-
tg.must(os.MkdirAll(tg.path("x"), 0777))
1012-
tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
1013-
module x
1014-
require rsc.io/quote v1.1.0
1015-
`), 0666))
1016-
tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`package x; import _ "rsc.io/quote"`), 0666))
1017-
1018-
// With correct go.sum,verify succeeds but avoids download.
1019-
tg.must(ioutil.WriteFile(tg.path("x/go.sum"), []byte(`rsc.io/quote v1.1.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
1020-
`), 0666))
1021-
tg.cd(tg.path("x"))
1022-
tg.run("mod", "-verify")
1023-
tg.mustNotExist(filepath.Join(gopath, "src/mod/cache/download/rsc.io/quote/@v/v1.1.0.zip"))
1024-
tg.mustNotExist(filepath.Join(gopath, "src/mod/github.com/pkg"))
1025-
1026-
// With incorrect sum, sync (which must download) fails.
1027-
// Even if the incorrect sum is in the old legacy go.modverify file.
1028-
tg.must(ioutil.WriteFile(tg.path("x/go.sum"), []byte(`
1029-
`), 0666))
1030-
tg.must(ioutil.WriteFile(tg.path("x/go.modverify"), []byte(`rsc.io/quote v1.1.0 h1:a3YaZoizPtXyv6ZsJ74oo2L4/bwOSTKMY7MAyo4O/1c=
1031-
`), 0666))
1032-
tg.runFail("mod", "-sync") // downloads pkg/errors
1033-
tg.grepStderr("checksum mismatch", "must detect mismatch")
1034-
tg.mustNotExist(filepath.Join(gopath, "src/mod/cache/download/rsc.io/quote/@v/v1.1.0.zip"))
1035-
tg.mustNotExist(filepath.Join(gopath, "src/mod/github.com/pkg"))
1036-
1037-
// With corrected sum, sync works.
1038-
tg.must(ioutil.WriteFile(tg.path("x/go.modverify"), []byte(`rsc.io/quote v1.1.0 h1:a3YaZoizPtXyv6ZsJ74oo2L4/bwOSTKMY7MAyo4O/0c=
1039-
`), 0666))
1040-
tg.run("mod", "-sync")
1041-
tg.mustExist(filepath.Join(gopath, "src/mod/cache/download/rsc.io/quote/@v/v1.1.0.zip"))
1042-
tg.mustExist(filepath.Join(gopath, "src/mod/rsc.io"))
1043-
tg.mustNotExist(tg.path("x/go.modverify")) // moved into go.sum
1044-
1045-
// Sync should have added sum for go.mod.
1046-
data, err := ioutil.ReadFile(tg.path("x/go.sum"))
1047-
if !strings.Contains(string(data), "\nrsc.io/quote v1.1.0/go.mod ") {
1048-
t.Fatalf("cannot find go.mod hash in go.sum: %v\n%s", err, data)
1049-
}
1050-
1051-
// Verify should work too.
1052-
tg.run("mod", "-verify")
1053-
1054-
// Even the most basic attempt to load the module graph should detect incorrect go.mod files.
1055-
tg.run("mod", "-graph") // loads module graph, is OK
1056-
tg.must(ioutil.WriteFile(tg.path("x/go.sum"), []byte(`rsc.io/quote v1.1.0 a3YaZoizPtXyv6ZsJ74oo2L4/bwOSTKMY7MAyo4O/1c=
1057-
rsc.io/quote v1.1.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl1=
1058-
`), 0666))
1059-
tg.runFail("mod", "-graph") // loads module graph, fails (even though sum is in old go.modverify file)
1060-
tg.grepStderr("go.mod: checksum mismatch", "must detect mismatch")
1061-
1062-
// go.sum should be created and updated automatically.
1063-
tg.must(os.Remove(tg.path("x/go.sum")))
1064-
tg.run("mod", "-graph")
1065-
tg.mustExist(tg.path("x/go.sum"))
1066-
data, err = ioutil.ReadFile(tg.path("x/go.sum"))
1067-
if !strings.Contains(string(data), " v1.1.0/go.mod ") {
1068-
t.Fatalf("cannot find go.mod hash in go.sum: %v\n%s", err, data)
1069-
}
1070-
if strings.Contains(string(data), " v1.1.0 ") {
1071-
t.Fatalf("unexpected module tree hash in go.sum: %v\n%s", err, data)
1072-
}
1073-
tg.run("mod", "-sync")
1074-
data, err = ioutil.ReadFile(tg.path("x/go.sum"))
1075-
if !strings.Contains(string(data), " v1.1.0/go.mod ") {
1076-
t.Fatalf("cannot find go.mod hash in go.sum: %v\n%s", err, data)
1077-
}
1078-
if !strings.Contains(string(data), " v1.1.0 ") {
1079-
t.Fatalf("cannot find module tree hash in go.sum: %v\n%s", err, data)
1080-
}
1081-
1082-
tg.must(os.Remove(filepath.Join(gopath, "src/mod/cache/download/rsc.io/quote/@v/v1.1.0.ziphash")))
1083-
tg.run("mod", "-sync") // ignores missing ziphash file for ordinary go.sum validation
1084-
1085-
tg.runFail("mod", "-verify") // explicit verify fails with missing ziphash
1086-
1087-
tg.run("mod", "-droprequire", "rsc.io/quote")
1088-
tg.run("list", "rsc.io/quote/buggy")
1089-
data, err = ioutil.ReadFile(tg.path("x/go.sum"))
1090-
if strings.Contains(string(data), "buggy") {
1091-
t.Fatalf("did not expect buggy in go.sum:\n%s", data)
1092-
}
1093-
if !strings.Contains(string(data), "rsc.io/quote v1.5.2/go.mod") {
1094-
t.Fatalf("did expect rsc.io/quote go.mod in go.sum:\n%s", data)
1095-
}
1096-
1097-
tg.run("mod", "-droprequire", "rsc.io/quote")
1098-
tg.runFail("list", "rsc.io/quote/buggy/foo")
1099-
data, err = ioutil.ReadFile(tg.path("x/go.sum"))
1100-
if strings.Contains(string(data), "buggy") {
1101-
t.Fatalf("did not expect buggy in go.sum:\n%s", data)
1102-
}
1103-
if !strings.Contains(string(data), "rsc.io/quote v1.5.2/go.mod") {
1104-
t.Fatalf("did expect rsc.io/quote go.mod in go.sum:\n%s", data)
1105-
}
1106-
1107-
tg.run("mod", "-droprequire", "rsc.io/quote")
1108-
tg.runFail("list", "rsc.io/quote/morebuggy")
1109-
if strings.Contains(string(data), "morebuggy") {
1110-
t.Fatalf("did not expect morebuggy in go.sum:\n%s", data)
1111-
}
1112-
if !strings.Contains(string(data), "rsc.io/quote v1.5.2/go.mod") {
1113-
t.Fatalf("did expect rsc.io/quote go.mod in go.sum:\n%s", data)
1114-
}
1115-
}
1116-
1117-
func TestModFileProxy(t *testing.T) {
1118-
tg := testGoModules(t)
1119-
defer tg.cleanup()
1120-
1121-
tg.setenv("GOPATH", tg.path("gp1"))
1122-
1123-
tg.must(os.MkdirAll(tg.path("x"), 0777))
1124-
tg.must(ioutil.WriteFile(tg.path("x/main.go"), []byte(`package x; import _ "rsc.io/quote"`), 0666))
1125-
tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`module x
1126-
require rsc.io/quote v1.5.1`), 0666))
1127-
tg.cd(tg.path("x"))
1128-
tg.run("list", "all")
1129-
tg.run("list", "-getmode=local", "all")
1130-
tg.mustExist(tg.path("gp1/src/mod/cache/download/rsc.io/quote/@v/list"))
1131-
1132-
// @v/list should contain version list.
1133-
data, err := ioutil.ReadFile(tg.path("gp1/src/mod/cache/download/rsc.io/quote/@v/list"))
1134-
if err != nil {
1135-
t.Fatal(err)
1136-
}
1137-
if !strings.Contains(string(data), "v1.5.1\n") {
1138-
t.Fatalf("cannot find v1.5.1 in @v/list:\n%s", data)
1139-
}
1140-
1141-
tg.setenv("GOPROXY", "file:///nonexist")
1142-
tg.run("list", "-getmode=local", "all")
1143-
1144-
tg.setenv("GOPATH", tg.path("gp2"))
1145-
tg.runFail("list", "-getmode=local", "all")
1146-
tg.runFail("list", "all") // because GOPROXY is bogus
1147-
1148-
tg.setenv("GOPROXY", "file://"+filepath.ToSlash(tg.path("gp1/src/mod/cache/download")))
1149-
tg.runFail("list", "-getmode=local", "all")
1150-
tg.run("list", "all")
1151-
tg.mustExist(tg.path("gp2/src/mod/cache/download/rsc.io/quote/@v/list"))
1152-
}
1153-
1154797
func TestModMultiVersion(t *testing.T) {
1155798
tg := testGoModules(t)
1156799
defer tg.cleanup()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
env GO111MODULE=on
2+
3+
# explicit get should report errors about bad names
4+
! go get appengine
5+
stderr 'cannot find module providing package appengine'
6+
! go get x/y.z
7+
stderr 'cannot find module providing package x/y.z'
8+
9+
# build should skip over appengine imports
10+
! go build
11+
! stderr appengine
12+
stderr 'cannot find module providing package nonexistent.rsc.io'
13+
14+
-- go.mod --
15+
module x
16+
17+
-- x.go --
18+
package x
19+
20+
import _ "appengine"
21+
import _ "nonexistent.rsc.io" // domain does not exist

0 commit comments

Comments
 (0)