Skip to content

Commit

Permalink
cmd/go: in tests, don't assume that the 'git' binary is present
Browse files Browse the repository at this point in the history
Add a helper-function to testenv to make these skips more ergonomic.
Also update a few existing skips in cmd/go/... to use it.

Updates #25300

Change-Id: I4205b4fb2b685dfac1cff3c999f954bff7b0f3c1
Reviewed-on: https://go-review.googlesource.com/c/go/+/181538
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
Bryan C. Mills committed Jun 10, 2019
1 parent 8f296f5 commit 350f71f
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 45 deletions.
92 changes: 51 additions & 41 deletions src/cmd/go/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,10 +1222,12 @@ func TestInternalCache(t *testing.T) {
}

func TestMoveGit(t *testing.T) {
testenv.MustHaveExecPath(t, "git")
testMove(t, "git", "rsc.io/pdf", "pdf", "rsc.io/pdf/.git/config")
}

func TestMoveHG(t *testing.T) {
testenv.MustHaveExecPath(t, "hg")
testMove(t, "hg", "vcs-test.golang.org/go/custom-hg-hello", "custom-hg-hello", "vcs-test.golang.org/go/custom-hg-hello/.hg/hgrc")
}

Expand Down Expand Up @@ -1287,9 +1289,7 @@ func TestImportCycle(t *testing.T) {
// cmd/go: custom import path checking should not apply to Go packages without import comment.
func TestIssue10952(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
if _, err := exec.LookPath("git"); err != nil {
t.Skip("skipping because git binary not found")
}
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand All @@ -1305,9 +1305,7 @@ func TestIssue10952(t *testing.T) {

func TestIssue16471(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
if _, err := exec.LookPath("git"); err != nil {
t.Skip("skipping because git binary not found")
}
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand All @@ -1323,9 +1321,7 @@ func TestIssue16471(t *testing.T) {
// Test git clone URL that uses SCP-like syntax and custom import path checking.
func TestIssue11457(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
if _, err := exec.LookPath("git"); err != nil {
t.Skip("skipping because git binary not found")
}
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand All @@ -1350,9 +1346,7 @@ func TestIssue11457(t *testing.T) {

func TestGetGitDefaultBranch(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
if _, err := exec.LookPath("git"); err != nil {
t.Skip("skipping because git binary not found")
}
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand All @@ -1378,9 +1372,7 @@ func TestGetGitDefaultBranch(t *testing.T) {
// Security issue. Don't disable. See golang.org/issue/22125.
func TestAccidentalGitCheckout(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
if _, err := exec.LookPath("git"); err != nil {
t.Skip("skipping because git binary not found")
}
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -1653,6 +1645,7 @@ func TestInstallToGOBINCommandLinePackage(t *testing.T) {

func TestGoGetNonPkg(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand All @@ -1669,6 +1662,7 @@ func TestGoGetNonPkg(t *testing.T) {

func TestGoGetTestOnlyPkg(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -2058,6 +2052,7 @@ func TestDefaultGOPATH(t *testing.T) {

func TestDefaultGOPATHGet(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -2439,6 +2434,7 @@ func TestSymlinkWarning(t *testing.T) {
// Issue 8181.
func TestGoGetDashTIssue8181(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand All @@ -2453,6 +2449,7 @@ func TestGoGetDashTIssue8181(t *testing.T) {
func TestIssue11307(t *testing.T) {
// go get -u was not working except in checkout directory
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -2931,6 +2928,7 @@ func TestCgoPkgConfig(t *testing.T) {

tg.run("env", "PKG_CONFIG")
pkgConfig := strings.TrimSpace(tg.getStdout())
testenv.MustHaveExecPath(t, pkgConfig)
if out, err := exec.Command(pkgConfig, "--atleast-pkgconfig-version", "0.24").CombinedOutput(); err != nil {
t.Skipf("%s --atleast-pkgconfig-version 0.24: %v\n%s", pkgConfig, err, out)
}
Expand Down Expand Up @@ -3033,9 +3031,7 @@ func TestIssue7573(t *testing.T) {
if !canCgo {
t.Skip("skipping because cgo not enabled")
}
if _, err := exec.LookPath("gccgo"); err != nil {
t.Skip("skipping because no gccgo compiler found")
}
testenv.MustHaveExecPath(t, "gccgo")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -3324,6 +3320,7 @@ func TestGoGenerateBadImports(t *testing.T) {

func TestGoGetCustomDomainWildcard(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand All @@ -3335,6 +3332,7 @@ func TestGoGetCustomDomainWildcard(t *testing.T) {

func TestGoGetInternalWildcard(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -3407,6 +3405,7 @@ func TestVetWithOnlyCgoFiles(t *testing.T) {
// Issue 9767, 19769.
func TestGoGetDotSlashDownload(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -3662,6 +3661,7 @@ func TestImportLocal(t *testing.T) {
func TestGoGetInsecure(t *testing.T) {
test := func(t *testing.T, modules bool) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -3702,6 +3702,7 @@ func TestGoGetInsecure(t *testing.T) {

func TestGoGetUpdateInsecure(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand All @@ -3726,6 +3727,7 @@ func TestGoGetUpdateInsecure(t *testing.T) {

func TestGoGetUpdateUnknownProtocol(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -3760,6 +3762,7 @@ func TestGoGetUpdateUnknownProtocol(t *testing.T) {

func TestGoGetInsecureCustomDomain(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -3862,6 +3865,7 @@ func TestGoGetUpdate(t *testing.T) {
// former dependencies, not current ones.

testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -3889,6 +3893,7 @@ func TestGoGetUpdate(t *testing.T) {
// Issue #20512.
func TestGoGetRace(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")
if !canRace {
t.Skip("skipping because race detector not supported")
}
Expand All @@ -3905,6 +3910,7 @@ func TestGoGetDomainRoot(t *testing.T) {
// go get foo.io (not foo.io/subdir) was not working consistently.

testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -4295,6 +4301,7 @@ func TestGenerateUsesBuildContext(t *testing.T) {
// Issue 14450: go get -u .../ tried to import not downloaded package
func TestGoGetUpdateWithWildcard(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -5043,9 +5050,8 @@ func TestExecBuildX(t *testing.T) {
t.Skip("skipping because cgo not enabled")
}

if runtime.GOOS == "plan9" || runtime.GOOS == "windows" {
t.Skipf("skipping because unix shell is not supported on %s", runtime.GOOS)
}
testenv.MustHaveExecPath(t, "/usr/bin/env")
testenv.MustHaveExecPath(t, "bash")

tg := testgo(t)
defer tg.cleanup()
Expand Down Expand Up @@ -5126,9 +5132,10 @@ func TestUpxCompression(t *testing.T) {
t.Skipf("skipping upx test on %s/%s", runtime.GOOS, runtime.GOARCH)
}

testenv.MustHaveExecPath(t, "upx")
out, err := exec.Command("upx", "--version").CombinedOutput()
if err != nil {
t.Skip("skipping because upx is not available")
t.Fatalf("upx --version failed: %v", err)
}

// upx --version prints `upx <version>` in the first line of output:
Expand All @@ -5137,13 +5144,13 @@ func TestUpxCompression(t *testing.T) {
re := regexp.MustCompile(`([[:digit:]]+)\.([[:digit:]]+)`)
upxVersion := re.FindStringSubmatch(string(out))
if len(upxVersion) != 3 {
t.Errorf("bad upx version string: %s", upxVersion)
t.Fatalf("bad upx version string: %s", upxVersion)
}

major, err1 := strconv.Atoi(upxVersion[1])
minor, err2 := strconv.Atoi(upxVersion[2])
if err1 != nil || err2 != nil {
t.Errorf("bad upx version string: %s", upxVersion[0])
t.Fatalf("bad upx version string: %s", upxVersion[0])
}

// Anything below 3.94 is known not to work with go binaries
Expand Down Expand Up @@ -5196,26 +5203,29 @@ func TestQEMUUserMode(t *testing.T) {
src, obj := tg.path("main.go"), tg.path("main")

for _, arch := range testArchs {
out, err := exec.Command("qemu-"+arch.qemu, "--version").CombinedOutput()
if err != nil {
t.Logf("Skipping %s test (qemu-%s not available)", arch.g, arch.qemu)
continue
}
arch := arch
t.Run(arch.g, func(t *testing.T) {
qemu := "qemu-" + arch.qemu
testenv.MustHaveExecPath(t, qemu)

out, err := exec.Command(qemu, "--version").CombinedOutput()
if err != nil {
t.Fatalf("%s --version failed: %v", qemu, err)
}

tg.setenv("GOARCH", arch.g)
tg.run("build", "-o", obj, src)
tg.setenv("GOARCH", arch.g)
tg.run("build", "-o", obj, src)

out, err = exec.Command("qemu-"+arch.qemu, obj).CombinedOutput()
if err != nil {
t.Logf("qemu-%s output:\n%s\n", arch.qemu, out)
t.Errorf("qemu-%s failed with %v", arch.qemu, err)
continue
}
if want := "hello qemu-user"; string(out) != want {
t.Errorf("bad output from qemu-%s:\ngot %s; want %s", arch.qemu, out, want)
}
out, err = exec.Command(qemu, obj).CombinedOutput()
if err != nil {
t.Logf("%s output:\n%s\n", qemu, out)
t.Fatalf("%s failed with %v", qemu, err)
}
if want := "hello qemu-user"; string(out) != want {
t.Errorf("bad output from %s:\ngot %s; want %s", qemu, out, want)
}
})
}

}

func TestCacheListStale(t *testing.T) {
Expand Down
Loading

0 comments on commit 350f71f

Please sign in to comment.