Skip to content

Commit

Permalink
cmd/go: convert still more module tests to scripts
Browse files Browse the repository at this point in the history
Change-Id: I249bb848c9911948dbd84cd88ad043a61ed6ea6b
Reviewed-on: https://go-review.googlesource.com/124699
Reviewed-by: Bryan C. Mills <[email protected]>
  • Loading branch information
rsc committed Jul 19, 2018
1 parent e3f15e3 commit 6140829
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 1,060 deletions.
265 changes: 0 additions & 265 deletions src/cmd/go/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"testing"

"cmd/go/internal/cfg"
Expand Down Expand Up @@ -414,226 +412,6 @@ require x.3 v1.99.0
`)
}

func TestModGetVersions(t *testing.T) {
tg := testGoModules(t)
defer tg.cleanup()

tg.setenv(homeEnvName(), tg.path("home"))
tg.must(os.MkdirAll(tg.path("x"), 0777))
tg.cd(tg.path("x"))
tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`package x`), 0666))

tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
module x
require rsc.io/quote v1.1.0
`), 0666))
tg.run("get", "rsc.io/[email protected]")
tg.run("list", "-m", "all")
tg.grepStdout("rsc.io/quote.*v0.0.0-20180709153244-fd906ed3b100", "did downgrade to v0.0.0-*")

tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
module x
require rsc.io/quote v1.2.0
`), 0666))
tg.run("get", "rsc.io/[email protected]")
tg.run("list", "-m", "-u", "all")
tg.grepStdout(`rsc.io/quote v1.1.0`, "did downgrade to v1.1.0")
tg.grepStdout(`rsc.io/quote v1.1.0 \[v1`, "did show upgrade to v1.2.0 or later")

tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
module x
require rsc.io/quote v1.1.0
`), 0666))
tg.run("get", "rsc.io/[email protected]")
tg.run("list", "-m", "all")
tg.grepStdout("rsc.io/quote.*v1.2.0", "did upgrade to v1.2.0")

// @14c0d48ead0c should resolve,
// and also there should be no build error about not having Go files in the root.
tg.run("get", "golang.org/x/text@14c0d48ead0c")

// @14c0d48ead0c should resolve.
// Now there should be no build at all.
tg.run("get", "-m", "golang.org/x/text@14c0d48ead0c")

// language@7f39a6fea4fe9364 should not resolve with -m,
// because .../language is not a module path.
tg.runFail("get", "-m", "golang.org/x/text/language@14c0d48ead0c")

// language@7f39a6fea4fe9364 should resolve without -m.
// Because of -d, there should be no build at all.
tg.run("get", "-d", "-x", "golang.org/x/text/language@14c0d48ead0c")
tg.grepStderrNot(`compile|cp .*language\.a$`, "should not see compile steps")

// Dropping -d, we should see a build now.
tg.run("get", "-x", "golang.org/x/text/language@14c0d48ead0c")
tg.grepStderr(`compile|cp .*language\.a$`, "should see compile steps")

// Even with -d, we should see an error for unknown packages.
tg.runFail("get", "-x", "golang.org/x/text/foo@14c0d48ead0c")
}

func TestModGetUpgrade(t *testing.T) {
tg := testGoModules(t)
defer tg.cleanup()

tg.setenv(homeEnvName(), tg.path("home"))
tg.must(os.MkdirAll(tg.path("x"), 0777))
tg.cd(tg.path("x"))
tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`package x; import _ "rsc.io/quote"`), 0666))

tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
module x
require rsc.io/quote v1.5.1
`), 0666))

tg.run("get", "-x", "-u")
tg.run("list", "-m", "-f={{.Path}} {{.Version}}{{if .Indirect}} // indirect{{end}}", "all")
tg.grepStdout(`quote v1.5.2$`, "should have upgraded only to v1.5.2")
tg.grepStdout(`x/text [v0-9a-f.\-]+ // indirect`, "should list golang.org/x/text as indirect")

var gomod string
readGoMod := func() {
data, err := ioutil.ReadFile(tg.path("x/go.mod"))
if err != nil {
t.Fatal(err)
}
gomod = string(data)
}
readGoMod()
if !strings.Contains(gomod, "rsc.io/quote v1.5.2\n") {
t.Fatalf("expected rsc.io/quote direct requirement:\n%s", gomod)
}
if !regexp.MustCompile(`(?m)golang.org/x/text.* // indirect`).MatchString(gomod) {
t.Fatalf("expected golang.org/x/text indirect requirement:\n%s", gomod)
}

tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`package x; import _ "golang.org/x/text"`), 0666))
tg.run("list") // rescans directory
readGoMod()
if !strings.Contains(gomod, "rsc.io/quote v1.5.2\n") {
t.Fatalf("expected rsc.io/quote direct requirement:\n%s", gomod)
}
if !regexp.MustCompile(`(?m)golang.org/x/text[^/]+\n`).MatchString(gomod) {
t.Fatalf("expected golang.org/x/text DIRECT requirement:\n%s", gomod)
}

tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`package x; import _ "rsc.io/quote"`), 0666))
tg.run("mod", "-sync") // rescans everything, can put // indirect marks back
readGoMod()
if !strings.Contains(gomod, "rsc.io/quote v1.5.2\n") {
t.Fatalf("expected rsc.io/quote direct requirement:\n%s", gomod)
}
if !regexp.MustCompile(`(?m)golang.org/x/text.* // indirect\n`).MatchString(gomod) {
t.Fatalf("expected golang.org/x/text indirect requirement:\n%s", gomod)
}

tg.run("get", "rsc.io/[email protected]") // should record as (time-corrected) pseudo-version
readGoMod()
if !strings.Contains(gomod, "rsc.io/quote v0.0.0-20180214005840-23179ee8a569\n") {
t.Fatalf("expected rsc.io/quote v0.0.0-20180214005840-23179ee8a569 (not v1.5.1)\n%s", gomod)
}

tg.run("get", "rsc.io/quote@23179ee") // should record as v1.5.1
readGoMod()
if !strings.Contains(gomod, "rsc.io/quote v1.5.1\n") {
t.Fatalf("expected rsc.io/quote v1.5.1 (not 23179ee)\n%s", gomod)
}

tg.run("mod", "-require", "rsc.io/quote@23179ee") // should record as 23179ee
readGoMod()
if !strings.Contains(gomod, "rsc.io/quote 23179ee\n") {
t.Fatalf("expected rsc.io/quote 23179ee\n%s", gomod)
}

tg.run("mod", "-fix") // fixup in any future go command should find v1.5.1 again
readGoMod()
if !strings.Contains(gomod, "rsc.io/quote v1.5.1\n") {
t.Fatalf("expected rsc.io/quote v1.5.1\n%s", gomod)
}

tg.run("get", "-m", "rsc.io/quote@dd9747d")
tg.run("list", "-m", "all")
tg.grepStdout(`quote v0.0.0-20180628003336-dd9747d19b04$`, "should have moved to pseudo-commit")

tg.run("get", "-m", "-u")
tg.run("list", "-m", "all")
tg.grepStdout(`quote v0.0.0-20180628003336-dd9747d19b04$`, "should have stayed on pseudo-commit")

tg.run("get", "-m", "rsc.io/quote@e7a685a342")
tg.run("list", "-m", "all")
tg.grepStdout(`quote v0.0.0-20180214005133-e7a685a342c0$`, "should have moved to new pseudo-commit")

tg.run("get", "-m", "-u")
tg.run("list", "-m", "all")
tg.grepStdout(`quote v1.5.2$`, "should have moved off pseudo-commit")

tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
module x
`), 0666))
tg.run("list")
tg.run("list", "-m", "all")
tg.grepStdout(`quote v1.5.2$`, "should have added quote v1.5.2")
tg.grepStdoutNot(`v1.5.3-pre1`, "should not mention v1.5.3-pre1")

tg.run("list", "-m", "-versions", "rsc.io/quote")
want := "rsc.io/quote v1.0.0 v1.1.0 v1.2.0 v1.2.1 v1.3.0 v1.4.0 v1.5.0 v1.5.1 v1.5.2 v1.5.3-pre1\n"
if tg.getStdout() != want {
t.Errorf("go list versions:\nhave:\n%s\nwant:\n%s", tg.getStdout(), want)
}

tg.run("list", "-m", "rsc.io/quote@>v1.5.2")
tg.grepStdout(`v1.5.3-pre1`, "expected to find v1.5.3-pre1")
tg.run("list", "-m", "rsc.io/quote@<v1.5.4")
tg.grepStdout(`v1.5.2$`, "expected to find v1.5.2 (NOT v1.5.3-pre1)")

tg.runFail("list", "-m", "rsc.io/quote@>v1.5.3")
tg.grepStderr(`go list -m rsc.io/quote: no matching versions for query ">v1.5.3"`, "expected no matching version")

tg.run("list", "-m", "-e", "-f={{.Error.Err}}", "rsc.io/quote@>v1.5.3")
tg.grepStdout(`no matching versions for query ">v1.5.3"`, "expected no matching version")

tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
module x
require rsc.io/quote v1.4.0
`), 0666))

tg.run("list", "-m", "all")
tg.grepStdout(`rsc.io/sampler v1.0.0`, "expected sampler v1.0.0")

tg.run("get", "-m", "-u=patch", "rsc.io/quote")
tg.run("list", "-m", "all")
tg.grepStdout(`rsc.io/quote v1.5.2`, "expected quote v1.5.2") // rsc.io/quote gets implicit @latest (not -u=patch)
tg.grepStdout(`rsc.io/sampler v1.3.1`, "expected sampler v1.3.1") // even though v1.5.2 requires v1.3.0
tg.grepStdout(`golang.org/x/text v0.0.0-`, "expected x/text pseudo-version") // can't jump from v0.0.0- to v0.3.0

tg.run("get", "-m", "-u=patch", "rsc.io/[email protected]")
tg.run("list", "-m", "all")
tg.grepStdout(`rsc.io/quote v1.2.0`, "expected quote v1.2.0") // not v1.2.1: -u=patch applies to deps of args, not args
tg.grepStdout(`rsc.io/sampler v1.3.1`, "expected sampler line to stay") // even though v1.2.0 does not require sampler?

tg.run("get", "-m", "-u=patch")
tg.run("list", "-m", "all")
tg.grepStdout(`rsc.io/quote v1.2.1`, "expected quote v1.2.1") // -u=patch with no args applies to deps of main module
tg.grepStdout(`rsc.io/sampler v1.3.1`, "expected sampler line to stay")
tg.grepStdout(`golang.org/x/text v0.0.0-`, "expected x/text pseudo-version") // even though x/text v0.3.0 is tagged

tg.run("get", "-m", "rsc.io/[email protected]")
tg.run("mod", "-vendor")
tg.setenv("GOPATH", tg.path("empty"))
tg.setenv("GOPROXY", "file:///nonexist")

tg.run("list", "-getmode=vendor", "all")
tg.run("list", "-getmode=vendor", "-m", "-f={{.Path}} {{.Version}} {{.Dir}}", "all")
tg.grepStdout(`rsc.io/quote v1.5.1 .*vendor[\\/]rsc.io[\\/]quote`, "expected vendored rsc.io/quote")
tg.grepStdout(`golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text`, "expected vendored golang.org/x/text")

tg.runFail("list", "-getmode=vendor", "-m", "rsc.io/quote@latest")
tg.grepStderr(`module lookup disabled by -getmode=vendor`, "expected disabled")
tg.runFail("get", "-getmode=vendor", "-u")
tg.grepStderr(`go get: disabled by -getmode=vendor`, "expected disabled")
}

func TestModSync(t *testing.T) {
tg := testGoModules(t)
defer tg.cleanup()
Expand Down Expand Up @@ -793,46 +571,3 @@ func TestModVendor(t *testing.T) {
tg.run("test", "-getmode=vendor", "./...")
}
}

func TestModMultiVersion(t *testing.T) {
tg := testGoModules(t)
defer tg.cleanup()

checkModules := func(dirs ...string) {
t.Helper()
tg.run("list", "-deps", "-f", "{{.ImportPath}}: {{.Dir}}")
for _, line := range strings.Split(tg.getStdout(), "\n") {
line = strings.Replace(line, `\`, `/`, -1) // windows!
if strings.HasPrefix(line, "rsc.io/quote: ") {
if strings.Contains(line, "/src/mod/") {
t.Fatalf("rsc.io/quote should not be from module cache: %v", line)
}
} else if strings.Contains(line, "rsc.io/quote") {
if !strings.Contains(line, "/src/mod/") {
t.Fatalf("rsc.io/quote/* should be from module cache: %v", line)
}
}
}
}

tg.extract("testdata/mod/rsc.io_quote_v1.5.2.txt")
checkModules()

// These are git checkouts from rsc.io/quote not downlaoded modules.
// As such they contain extra files like spurious pieces of other modules.
tg.extract("testdata/rsc.io_quote_0d003b9.txt") // wraps v2
checkModules()

tg.extract("testdata/rsc.io_quote_b44a0b1.txt") // adds go.mod
checkModules()

tg.extract("testdata/rsc.io_quote_fe488b8.txt") // adds broken v3 subdirectory
tg.run("list", "./...") // should ignore v3 because v3/go.mod exists
checkModules()

tg.extract("testdata/rsc.io_quote_a91498b.txt") // wraps v3
checkModules() // looks up v3 from internet, not v3 subdirectory

tg.extract("testdata/rsc.io_quote_5d9f230.txt") // adds go.mod
checkModules() // knows which v3 to use (still needs download from internet, cached from last step)
}
15 changes: 13 additions & 2 deletions src/cmd/go/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,23 +569,34 @@ func scriptMatch(ts *testScript, neg bool, args []string, text, name string) {
re, err := regexp.Compile(`(?m)` + pattern)
ts.check(err)

if name == "grep" {
isGrep := name == "grep"
if isGrep {
name = args[1] // for error messages
data, err := ioutil.ReadFile(ts.mkabs(args[1]))
ts.check(err)
text = string(data)
}

if neg {
if re.MatchString(text) {
ts.fatalf("unexpected match for %#q found in %s: %s %q", pattern, name, text, re.FindString(text))
if isGrep {
fmt.Fprintf(&ts.log, "[%s]\n%s\n", name, text)
}
ts.fatalf("unexpected match for %#q found in %s: %s", pattern, name, re.FindString(text))
}
} else {
if !re.MatchString(text) {
if isGrep {
fmt.Fprintf(&ts.log, "[%s]\n%s\n", name, text)
}
ts.fatalf("no match for %#q found in %s", pattern, name)
}
if n > 0 {
count := len(re.FindAllString(text, -1))
if count != n {
if isGrep {
fmt.Fprintf(&ts.log, "[%s]\n%s\n", name, text)
}
ts.fatalf("have %d matches for %#q, want %d", count, pattern, n)
}
}
Expand Down
Loading

0 comments on commit 6140829

Please sign in to comment.