From 7e368b3f5ae7a11648dcf590ec1f14a4126169f4 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Fri, 10 Jan 2020 14:48:49 -0500 Subject: [PATCH] cmd/go: convert TestGoBuildOutput to the script framework Adds a in-script go binary called inarchive to check that an archive is produced. A weaker could be done faster using grep, but this is more faithful to the original test. Part of converting all tests to script framework to improve test parallelism. Updates #36320 Updates #17751 Change-Id: I001fa0698063be80fe3da947c81d4eb0829be47f Reviewed-on: https://go-review.googlesource.com/c/go/+/214295 Reviewed-by: Jay Conrod --- src/cmd/go/go_test.go | 76 ------------------ src/cmd/go/testdata/script/build_output.txt | 87 +++++++++++++++++++++ 2 files changed, 87 insertions(+), 76 deletions(-) create mode 100644 src/cmd/go/testdata/script/build_output.txt diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index cd80e655d2..ae7ecdd8c9 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -788,21 +788,6 @@ func (tg *testgoData) wantExecutable(path, msg string) { } } -// wantArchive fails if path is not an archive. -func (tg *testgoData) wantArchive(path string) { - tg.t.Helper() - f, err := os.Open(path) - if err != nil { - tg.t.Fatal(err) - } - buf := make([]byte, 100) - io.ReadFull(f, buf) - f.Close() - if !bytes.HasPrefix(buf, []byte("!\n")) { - tg.t.Fatalf("file %s exists but is not an archive", path) - } -} - // isStale reports whether pkg is stale, and why func (tg *testgoData) isStale(pkg string) (bool, string) { tg.t.Helper() @@ -2219,67 +2204,6 @@ func TestIssue12096(t *testing.T) { tg.run("test", tg.path("test_test.go")) } -func TestGoBuildOutput(t *testing.T) { - skipIfGccgo(t, "gccgo has no standard packages") - tooSlow(t) - tg := testgo(t) - defer tg.cleanup() - - tg.makeTempdir() - tg.cd(tg.path(".")) - - nonExeSuffix := ".exe" - if exeSuffix == ".exe" { - nonExeSuffix = "" - } - - tg.tempFile("x.go", "package main\nfunc main(){}\n") - tg.run("build", "x.go") - tg.wantExecutable("x"+exeSuffix, "go build x.go did not write x"+exeSuffix) - tg.must(os.Remove(tg.path("x" + exeSuffix))) - tg.mustNotExist("x" + nonExeSuffix) - - tg.run("build", "-o", "myprog", "x.go") - tg.mustNotExist("x") - tg.mustNotExist("x.exe") - tg.wantExecutable("myprog", "go build -o myprog x.go did not write myprog") - tg.mustNotExist("myprog.exe") - - tg.tempFile("p.go", "package p\n") - tg.run("build", "p.go") - tg.mustNotExist("p") - tg.mustNotExist("p.a") - tg.mustNotExist("p.o") - tg.mustNotExist("p.exe") - - tg.run("build", "-o", "p.a", "p.go") - tg.wantArchive("p.a") - - tg.run("build", "cmd/gofmt") - tg.wantExecutable("gofmt"+exeSuffix, "go build cmd/gofmt did not write gofmt"+exeSuffix) - tg.must(os.Remove(tg.path("gofmt" + exeSuffix))) - tg.mustNotExist("gofmt" + nonExeSuffix) - - tg.run("build", "-o", "mygofmt", "cmd/gofmt") - tg.wantExecutable("mygofmt", "go build -o mygofmt cmd/gofmt did not write mygofmt") - tg.mustNotExist("mygofmt.exe") - tg.mustNotExist("gofmt") - tg.mustNotExist("gofmt.exe") - - tg.run("build", "sync/atomic") - tg.mustNotExist("atomic") - tg.mustNotExist("atomic.exe") - - tg.run("build", "-o", "myatomic.a", "sync/atomic") - tg.wantArchive("myatomic.a") - tg.mustNotExist("atomic") - tg.mustNotExist("atomic.a") - tg.mustNotExist("atomic.exe") - - tg.runFail("build", "-o", "whatever", "cmd/gofmt", "sync/atomic") - tg.grepStderr("multiple packages", "did not reject -o with multiple packages") -} - func TestGoBuildARM(t *testing.T) { if testing.Short() { t.Skip("skipping cross-compile in short mode") diff --git a/src/cmd/go/testdata/script/build_output.txt b/src/cmd/go/testdata/script/build_output.txt new file mode 100644 index 0000000000..e5a4852346 --- /dev/null +++ b/src/cmd/go/testdata/script/build_output.txt @@ -0,0 +1,87 @@ +[gccgo] skip 'gccgo has no standard packages' +[short] skip + +[!windows] env NONEXE='.exe' +[windows] env NONEXE='' + +env GOBIN=$WORK/tmp/bin +go install isarchive & + +go build x.go +exists -exec x$GOEXE +rm x$GOEXE +! exists x$NONEXE + +go build -o myprog x.go +! exists x +! exists x.exe +exists -exec myprog +! exists myprogr.exe + +go build p.go +! exists p +! exists p.a +! exists p.o +! exists p.exe + +wait # for isarchive + +go build -o p.a p.go +exists p.a +exec $GOBIN/isarchive p.a + +go build cmd/gofmt +exists -exec gofmt$GOEXE +rm gofmt$GOEXE +! exists gofmt$NONEXE + +go build -o mygofmt cmd/gofmt +exists -exec mygofmt +! exists mygofmt.exe +! exists gofmt +! exists gofmt.exe + +go build sync/atomic +! exists atomic +! exists atomic.exe + +go build -o myatomic.a sync/atomic +exists myatomic.a +exec $GOBIN/isarchive myatomic.a +! exists atomic +! exists atomic.a +! exists atomic.exe + +! go build -o whatever cmd/gofmt sync/atomic +stderr 'multiple packages' + +-- x.go -- +package main + +func main() {} +-- p.go -- +package p +-- isarchive/isarchive.go -- +package main + +import ( + "bytes" + "fmt" + "io" + "os" +) + +func main() { + f, err := os.Open(os.Args[1]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + buf := make([]byte, 100) + io.ReadFull(f, buf) + f.Close() + if !bytes.HasPrefix(buf, []byte("!\n")) { + fmt.Fprintf(os.Stderr, "file %s exists but is not an archive\n", os.Args[1]) + os.Exit(1) + } +} \ No newline at end of file