Skip to content

Commit d286d4b

Browse files
committed
cmd/go: convert module tests to scripts
Change-Id: If0976d15027db795f1383ef709c49c838cbb6953 Reviewed-on: https://go-review.googlesource.com/124696 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
1 parent c54bc34 commit d286d4b

9 files changed

+205
-178
lines changed

src/cmd/go/mod_test.go

Lines changed: 0 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -69,83 +69,6 @@ func (tg *testgoData) extract(file string) {
6969
}
7070
}
7171

72-
func TestModGO111MODULE(t *testing.T) {
73-
tg := testGoModules(t)
74-
defer tg.cleanup()
75-
76-
tg.tempFile("gp/src/x/y/z/go.mod", "module x/y/z")
77-
tg.tempFile("gp/src/x/y/z/w/w.txt", "")
78-
tg.tempFile("gp/foo/go.mod", "module example.com/mod")
79-
tg.tempFile("gp/foo/bar/baz/quux.txt", "")
80-
tg.tempFile("gp/bar/x.txt", "")
81-
tg.setenv("GOPATH", tg.path("gp"))
82-
83-
// In GOPATH/src with go.mod.
84-
tg.cd(tg.path("gp/src/x/y/z"))
85-
tg.setenv("GO111MODULE", "auto")
86-
tg.run("env", "GOMOD")
87-
tg.grepStdoutNot(`go.mod`, "expected module mode disabled")
88-
89-
tg.cd(tg.path("gp/src/x/y/z/w"))
90-
tg.run("env", "GOMOD")
91-
tg.grepStdoutNot(`go.mod`, "expected module mode disabled")
92-
93-
tg.setenv("GO111MODULE", "off")
94-
tg.run("env", "GOMOD")
95-
tg.grepStdoutNot(`go.mod`, "expected module mode disabled")
96-
97-
tg.setenv("GO111MODULE", "on")
98-
tg.run("env", "GOMOD")
99-
tg.grepStdout(`.*z[/\\]go.mod$`, "expected module mode enabled")
100-
101-
// In GOPATH/src without go.mod.
102-
tg.cd(tg.path("gp/src/x/y"))
103-
tg.setenv("GO111MODULE", "auto")
104-
tg.run("env", "GOMOD")
105-
tg.grepStdoutNot(`go.mod`, "expected module mode disabled")
106-
107-
tg.setenv("GO111MODULE", "off")
108-
tg.run("env", "GOMOD")
109-
tg.grepStdoutNot(`go.mod`, "expected module mode disabled")
110-
111-
tg.setenv("GO111MODULE", "on")
112-
tg.runFail("env", "GOMOD")
113-
tg.grepStderr(`cannot find main module root`, "expected module mode failure")
114-
115-
// Outside GOPATH/src with go.mod.
116-
tg.cd(tg.path("gp/foo"))
117-
tg.setenv("GO111MODULE", "auto")
118-
tg.run("env", "GOMOD")
119-
tg.grepStdout(`.*foo[/\\]go.mod$`, "expected module mode enabled")
120-
121-
tg.cd(tg.path("gp/foo/bar/baz"))
122-
tg.run("env", "GOMOD")
123-
tg.grepStdout(`.*foo[/\\]go.mod$`, "expected module mode enabled")
124-
125-
tg.setenv("GO111MODULE", "off")
126-
tg.run("env", "GOMOD")
127-
tg.grepStdoutNot(`go.mod`, "expected module mode disabled")
128-
}
129-
130-
func TestModVersionsInGOPATHMode(t *testing.T) {
131-
tg := testgo(t)
132-
tg.setenv("GO111MODULE", "off") // GOPATH mode
133-
defer tg.cleanup()
134-
tg.makeTempdir()
135-
136-
tg.runFail("get", "rsc.io/quote@v1.5.1")
137-
tg.grepStderr(`go: cannot use path@version syntax in GOPATH mode`, "expected path@version error")
138-
139-
tg.runFail("build", "rsc.io/quote@v1.5.1")
140-
tg.grepStderr(`can't load package:.* cannot use path@version syntax in GOPATH mode`, "expected path@version error")
141-
142-
tg.setenv("GO111MODULE", "on") // GOPATH mode
143-
tg.tempFile("x/go.mod", "module x")
144-
tg.cd(tg.path("x"))
145-
tg.runFail("build", "rsc.io/quote@v1.5.1")
146-
tg.grepStderr(`can't load package:.* can only use path@version syntax with 'go get'`, "expected path@version error")
147-
}
148-
14972
func TestModFindModuleRoot(t *testing.T) {
15073
tg := testGoModules(t)
15174
defer tg.cleanup()
@@ -328,16 +251,6 @@ func TestModFindModulePath(t *testing.T) {
328251
// }
329252
}
330253

331-
func TestModImportModFails(t *testing.T) {
332-
tg := testGoModules(t)
333-
defer tg.cleanup()
334-
335-
tg.setenv("GO111MODULE", "off")
336-
tg.tempFile("gopath/src/mod/foo/foo.go", "package foo")
337-
tg.runFail("list", "mod/foo")
338-
tg.grepStderr(`disallowed import path`, "expected disallowed because of module cache")
339-
}
340-
341254
func TestModEdit(t *testing.T) {
342255
// Test that local replacements work
343256
// and that they can use a dummy name
@@ -502,91 +415,6 @@ require x.3 v1.99.0
502415
`)
503416
}
504417

505-
func TestModLocalModule(t *testing.T) {
506-
// Test that local replacements work
507-
// and that they can use a dummy name
508-
// that isn't resolvable and need not even
509-
// include a dot. See golang.org/issue/24100.
510-
tg := testGoModules(t)
511-
defer tg.cleanup()
512-
513-
tg.must(os.MkdirAll(tg.path("x/y"), 0777))
514-
tg.must(os.MkdirAll(tg.path("x/z"), 0777))
515-
tg.must(ioutil.WriteFile(tg.path("x/y/go.mod"), []byte(`
516-
module x/y
517-
require zz v1.0.0
518-
replace zz v1.0.0 => ../z
519-
`), 0666))
520-
tg.must(ioutil.WriteFile(tg.path("x/y/y.go"), []byte(`package y; import _ "zz"`), 0666))
521-
tg.must(ioutil.WriteFile(tg.path("x/z/go.mod"), []byte(`
522-
module x/z
523-
`), 0666))
524-
tg.must(ioutil.WriteFile(tg.path("x/z/z.go"), []byte(`package z`), 0666))
525-
tg.cd(tg.path("x/y"))
526-
tg.run("build")
527-
}
528-
529-
func TestModTags(t *testing.T) {
530-
// Test that build tags are used. See golang.org/issue/24053.
531-
tg := testGoModules(t)
532-
defer tg.cleanup()
533-
534-
tg.must(os.MkdirAll(tg.path("x"), 0777))
535-
tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
536-
module x
537-
`), 0666))
538-
tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`// +build tag1
539-
540-
package y
541-
`), 0666))
542-
tg.must(ioutil.WriteFile(tg.path("x/y.go"), []byte(`// +build tag2
543-
544-
package y
545-
`), 0666))
546-
tg.cd(tg.path("x"))
547-
548-
tg.runFail("list", "-f={{.GoFiles}}")
549-
tg.grepStderr("build constraints exclude all Go files", "no Go source files without tags")
550-
551-
tg.run("list", "-f={{.GoFiles}}", "-tags=tag1")
552-
tg.grepStdout(`\[x.go\]`, "Go source files for tag1")
553-
554-
tg.run("list", "-f={{.GoFiles}}", "-tags", "tag2")
555-
tg.grepStdout(`\[y.go\]`, "Go source files for tag2")
556-
557-
tg.run("list", "-f={{.GoFiles}}", "-tags", "tag1 tag2")
558-
tg.grepStdout(`\[x.go y.go\]`, "Go source files for tag1 and tag2")
559-
}
560-
561-
func TestModFSPatterns(t *testing.T) {
562-
tg := testGoModules(t)
563-
defer tg.cleanup()
564-
565-
tg.must(os.MkdirAll(tg.path("x/vendor/v"), 0777))
566-
tg.must(os.MkdirAll(tg.path("x/y/z/w"), 0777))
567-
tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
568-
module m
569-
`), 0666))
570-
571-
tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`package x`), 0666))
572-
tg.must(ioutil.WriteFile(tg.path("x/vendor/v/v.go"), []byte(`package v; import "golang.org/x/crypto"`), 0666))
573-
tg.must(ioutil.WriteFile(tg.path("x/vendor/v.go"), []byte(`package main`), 0666))
574-
tg.must(ioutil.WriteFile(tg.path("x/y/y.go"), []byte(`package y`), 0666))
575-
tg.must(ioutil.WriteFile(tg.path("x/y/z/go.mod"), []byte(`syntax error`), 0666))
576-
tg.must(ioutil.WriteFile(tg.path("x/y/z/z.go"), []byte(`package z`), 0666))
577-
tg.must(ioutil.WriteFile(tg.path("x/y/z/w/w.go"), []byte(`package w`), 0666))
578-
579-
tg.cd(tg.path("x"))
580-
for _, pattern := range []string{"all", "m/...", "./..."} {
581-
tg.run("list", pattern)
582-
tg.grepStdout(`^m$`, "expected m")
583-
tg.grepStdout(`^m/vendor$`, "must see package named vendor")
584-
tg.grepStdoutNot(`vendor/`, "must not see vendored packages")
585-
tg.grepStdout(`^m/y$`, "expected m/y")
586-
tg.grepStdoutNot(`^m/y/z`, "should ignore submodule m/y/z...")
587-
}
588-
}
589-
590418
func TestModGetVersions(t *testing.T) {
591419
tg := testGoModules(t)
592420
defer tg.cleanup()

src/cmd/go/script_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type testScript struct {
7373

7474
// setup sets up the test execution temporary directory and environment.
7575
func (ts *testScript) setup() {
76+
StartProxy()
7677
ts.workdir = filepath.Join(testTmpDir, "script-"+ts.name)
7778
ts.check(os.MkdirAll(filepath.Join(ts.workdir, "tmp"), 0777))
7879
ts.check(os.MkdirAll(filepath.Join(ts.workdir, "gopath/src"), 0777))
@@ -85,6 +86,7 @@ func (ts *testScript) setup() {
8586
"GOCACHE=" + testGOCACHE,
8687
"GOOS=" + runtime.GOOS,
8788
"GOPATH=" + filepath.Join(ts.workdir, "gopath"),
89+
"GOPROXY=" + proxyURL,
8890
"GOROOT=" + testGOROOT,
8991
tempEnvName() + "=" + filepath.Join(ts.workdir, "tmp"),
9092
"devnull=" + os.DevNull,
@@ -604,15 +606,15 @@ func (ts *testScript) parse(line string) []string {
604606
quoted = false // currently processing quoted text
605607
)
606608
for i := 0; ; i++ {
607-
if !quoted && (i >= len(line) || line[i] == ' ' || line[i] == '\t' || line[i] == '\r') {
609+
if !quoted && (i >= len(line) || line[i] == ' ' || line[i] == '\t' || line[i] == '\r' || line[i] == '#') {
608610
// Found arg-separating space.
609611
if start >= 0 {
610612
arg += ts.expand(line[start:i])
611613
args = append(args, arg)
612614
start = -1
613615
arg = ""
614616
}
615-
if i >= len(line) {
617+
if i >= len(line) || line[i] == '#' {
616618
break
617619
}
618620
continue

src/cmd/go/testdata/script/README

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Scripts also have access to these other environment variables:
3030
GOCACHE=<actual GOCACHE being used outside the test>
3131
GOOS=<target GOOS>
3232
GOPATH=$WORK/gopath
33+
GOPROXY=<local module proxy serving from cmd/go/testdata/mod>
3334
GOROOT=<actual GOROOT>
3435
HOME=/no-home
3536
PATH=<actual PATH>
@@ -48,10 +49,11 @@ by a tiny script engine in ../../script_test.go (not the system shell).
4849
The script stops and the overall test fails if any particular command fails.
4950

5051
Each line is parsed into a sequence of space-separated command words,
51-
with environment variable expansion. Adding single quotes around text
52-
keeps spaces in that text from being treated as word separators and also
53-
disables environment variable expansion. Inside a single-quoted block of
54-
text, a repeated single quote indicates a literal single quote, as in:
52+
with environment variable expansion and # marking an end-of-line comment.
53+
Adding single quotes around text keeps spaces in that text from being treated
54+
as word separators and also disables environment variable expansion.
55+
Inside a single-quoted block of text, a repeated single quote indicates
56+
a literal single quote, as in:
5557

5658
'Don''t communicate by sharing memory.'
5759

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Test that build tags are used.
2+
# golang.org/issue/24053.
3+
4+
env GO111MODULE=on
5+
6+
cd x
7+
! go list -f {{.GoFiles}}
8+
stderr 'build constraints exclude all Go files'
9+
10+
go list -f {{.GoFiles}} -tags tag1
11+
stdout '\[x.go\]'
12+
13+
go list -f {{.GoFiles}} -tags tag2
14+
stdout '\[y\.go\]'
15+
16+
go list -f {{.GoFiles}} -tags 'tag1 tag2'
17+
stdout '\[x\.go y\.go\]'
18+
19+
-- x/go.mod --
20+
module x
21+
22+
-- x/x.go --
23+
// +build tag1
24+
25+
package y
26+
27+
-- x/y.go --
28+
// +build tag2
29+
30+
package y
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# GO111MODULE=auto should only trigger outside GOPATH/src
2+
env GO111MODULE=auto
3+
4+
cd $GOPATH/src/x/y/z
5+
go env GOMOD
6+
! stdout . # no non-empty lines
7+
8+
cd $GOPATH/src/x/y/z/w
9+
go env GOMOD
10+
! stdout .
11+
12+
cd $GOPATH/src/x/y
13+
go env GOMOD
14+
! stdout .
15+
16+
cd $GOPATH/foo
17+
go env GOMOD
18+
stdout foo[/\\]go.mod
19+
20+
cd $GOPATH/foo/bar/baz
21+
go env GOMOD
22+
stdout foo[/\\]go.mod
23+
24+
# GO111MODULE=on should trigger everywhere
25+
env GO111MODULE=on
26+
27+
cd $GOPATH/src/x/y/z
28+
go env GOMOD
29+
stdout z[/\\]go.mod
30+
31+
cd $GOPATH/src/x/y/z/w
32+
go env GOMOD
33+
stdout z[/\\]go.mod
34+
35+
cd $GOPATH/src/x/y
36+
! go env GOMOD
37+
stderr 'cannot find main module root'
38+
39+
cd $GOPATH/foo
40+
go env GOMOD
41+
stdout foo[/\\]go.mod
42+
43+
cd $GOPATH/foo/bar/baz
44+
go env GOMOD
45+
stdout foo[/\\]go.mod
46+
47+
# GO111MODULE=off should trigger nowhere
48+
env GO111MODULE=off
49+
50+
cd $GOPATH/src/x/y/z
51+
go env GOMOD
52+
! stdout .+
53+
54+
cd $GOPATH/foo
55+
go env GOMOD
56+
! stdout .+
57+
58+
cd $GOPATH/foo/bar/baz
59+
go env GOMOD
60+
! stdout .+
61+
62+
-- $GOPATH/src/x/y/z/go.mod --
63+
module x/y/z
64+
-- $GOPATH/src/x/y/z/w/w.txt --
65+
-- $GOPATH/foo/go.mod --
66+
module example.com/mod
67+
-- $GOPATH/foo/bar/baz/quux.txt --

0 commit comments

Comments
 (0)