From ea38df043613ddd7b49f4b2d0109b4784b69c204 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Fri, 10 Jan 2020 16:15:19 -0500 Subject: [PATCH] cmd/go: convert TestBadCgoDirectives to the script framework Part of converting all tests to script framework to improve test parallelism. Updates #36320 Updates #17751 Change-Id: I1328a87e2481b4555b01df5c898f1a8015412adc Reviewed-on: https://go-review.googlesource.com/c/go/+/214296 Reviewed-by: Jay Conrod --- src/cmd/go/go_test.go | 112 ---------------- .../go/testdata/script/cgo_bad_directives.txt | 126 ++++++++++++++++++ 2 files changed, 126 insertions(+), 112 deletions(-) create mode 100644 src/cmd/go/testdata/script/cgo_bad_directives.txt diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index ae7ecdd8c977a7..2c829f38b69598 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -3340,118 +3340,6 @@ func TestBadCommandLines(t *testing.T) { tg.grepStderr("invalid import path \"-x/y\"", "did not reject -x/y import path") } -func TestBadCgoDirectives(t *testing.T) { - if !canCgo { - t.Skip("no cgo") - } - tooSlow(t) - tg := testgo(t) - defer tg.cleanup() - - tg.tempFile("src/x/x.go", "package x\n") - tg.setenv("GOPATH", tg.path(".")) - - if runtime.Compiler == "gc" { - tg.tempFile("src/x/x.go", `package x - - //go:cgo_ldflag "-fplugin=foo.so" - - import "C" - `) - tg.runFail("build", "x") - tg.grepStderr("//go:cgo_ldflag .* only allowed in cgo-generated code", "did not reject //go:cgo_ldflag directive") - } - - tg.must(os.Remove(tg.path("src/x/x.go"))) - tg.runFail("build", "x") - tg.grepStderr("no Go files", "did not report missing source code") - tg.tempFile("src/x/_cgo_yy.go", `package x - - //go:cgo_ldflag "-fplugin=foo.so" - - import "C" - `) - tg.runFail("build", "x") - tg.grepStderr("no Go files", "did not report missing source code") // _* files are ignored... - - if runtime.Compiler == "gc" { - tg.runFail("build", tg.path("src/x/_cgo_yy.go")) // ... but if forced, the comment is rejected - // Actually, today there is a separate issue that _ files named - // on the command line are ignored. Once that is fixed, - // we want to see the cgo_ldflag error. - tg.grepStderr("//go:cgo_ldflag only allowed in cgo-generated code|no Go files", "did not reject //go:cgo_ldflag directive") - } - - tg.must(os.Remove(tg.path("src/x/_cgo_yy.go"))) - - tg.tempFile("src/x/x.go", "package x\n") - tg.tempFile("src/x/y.go", `package x - // #cgo CFLAGS: -fplugin=foo.so - import "C" - `) - tg.runFail("build", "x") - tg.grepStderr("invalid flag in #cgo CFLAGS: -fplugin=foo.so", "did not reject -fplugin") - - tg.tempFile("src/x/y.go", `package x - // #cgo CFLAGS: -Ibar -fplugin=foo.so - import "C" - `) - tg.runFail("build", "x") - tg.grepStderr("invalid flag in #cgo CFLAGS: -fplugin=foo.so", "did not reject -fplugin") - - tg.tempFile("src/x/y.go", `package x - // #cgo pkg-config: -foo - import "C" - `) - tg.runFail("build", "x") - tg.grepStderr("invalid pkg-config package name: -foo", "did not reject pkg-config: -foo") - - tg.tempFile("src/x/y.go", `package x - // #cgo pkg-config: @foo - import "C" - `) - tg.runFail("build", "x") - tg.grepStderr("invalid pkg-config package name: @foo", "did not reject pkg-config: -foo") - - tg.tempFile("src/x/y.go", `package x - // #cgo CFLAGS: @foo - import "C" - `) - tg.runFail("build", "x") - tg.grepStderr("invalid flag in #cgo CFLAGS: @foo", "did not reject @foo flag") - - tg.tempFile("src/x/y.go", `package x - // #cgo CFLAGS: -D - import "C" - `) - tg.runFail("build", "x") - tg.grepStderr("invalid flag in #cgo CFLAGS: -D without argument", "did not reject trailing -I flag") - - // Note that -I @foo is allowed because we rewrite it into -I /path/to/src/@foo - // before the check is applied. There's no such rewrite for -D. - - tg.tempFile("src/x/y.go", `package x - // #cgo CFLAGS: -D @foo - import "C" - `) - tg.runFail("build", "x") - tg.grepStderr("invalid flag in #cgo CFLAGS: -D @foo", "did not reject -D @foo flag") - - tg.tempFile("src/x/y.go", `package x - // #cgo CFLAGS: -D@foo - import "C" - `) - tg.runFail("build", "x") - tg.grepStderr("invalid flag in #cgo CFLAGS: -D@foo", "did not reject -D@foo flag") - - tg.setenv("CGO_CFLAGS", "-D@foo") - tg.tempFile("src/x/y.go", `package x - import "C" - `) - tg.run("build", "-n", "x") - tg.grepStderr("-D@foo", "did not find -D@foo in commands") -} - func TestTwoPkgConfigs(t *testing.T) { if !canCgo { t.Skip("no cgo") diff --git a/src/cmd/go/testdata/script/cgo_bad_directives.txt b/src/cmd/go/testdata/script/cgo_bad_directives.txt new file mode 100644 index 00000000000000..358284ffec0dee --- /dev/null +++ b/src/cmd/go/testdata/script/cgo_bad_directives.txt @@ -0,0 +1,126 @@ +[!cgo] skip +[short] skip + +mkdir x +cp x.go.txt x/x.go + +# Only allow //go:cgo_ldflag .* in cgo-generated code +[gc] cp x_gc.go.txt x/x.go +[gc] ! go build x +[gc] stderr '//go:cgo_ldflag .* only allowed in cgo-generated code' + +# Ignore _* files +rm x/x.go +! go build x +stderr 'no Go files' +cp cgo_yy.go.txt x/_cgo_yy.go +! go build x +stderr 'no Go files' #_* files are ignored... + +[gc] ! go build x/_cgo_yy.go # ... but if forced, the comment is rejected +# Actually, today there is a separate issue that _ files named +# on the command line are ignored. Once that is fixed, +# we want to see the cgo_ldflag error. +[gc] stderr '//go:cgo_ldflag only allowed in cgo-generated code|no Go files' + +rm x/_cgo_yy.go + +# Reject #cgo CFLAGS: -fplugin=foo.so +cp x.go.txt x/x.go +cp y_fplugin.go.txt x/y.go +! go build x +stderr 'invalid flag in #cgo CFLAGS: -fplugin=foo.so' + +# Reject #cgo CFLAGS: -lbar -fplugin=foo.so +cp y_lbar_fplugin.go.txt x/y.go +! go build x +stderr 'invalid flag in #cgo CFLAGS: -fplugin=foo.so' + +# Reject #cgo pkg-config: -foo +cp y_pkgconfig_dash_foo.txt x/y.go +! go build x +stderr 'invalid pkg-config package name: -foo' + +# Reject #cgo pkg-config: @foo +cp y_pkgconfig_at_foo.txt x/y.go +! go build x +stderr 'invalid pkg-config package name: @foo' + +# Reject #cgo CFLAGS: @foo +cp y_cflags_at_foo.txt x/y.go +! go build x +stderr 'invalid flag in #cgo CFLAGS: @foo' + +# Reject #cgo CFLAGS: -D +cp y_cflags_dash_d.txt x/y.go +! go build x +stderr 'invalid flag in #cgo CFLAGS: -D without argument' + +# Note that -I @foo is allowed because we rewrite it into -I /path/to/src/@foo +# before the check is applied. There's no such rewrite for -D. + +# Reject #cgo CFLAGS: -D @foo +cp y_cflags_dash_d_space_at_foo.txt x/y.go +! go build x +stderr 'invalid flag in #cgo CFLAGS: -D @foo' + +# Reject #cgo CFLAGS -D@foo +cp y_cflags_dash_d_at_foo.txt x/y.go +! go build x +stderr 'invalid flag in #cgo CFLAGS: -D@foo' + +# Check for CFLAGS in commands +env CGO_CFLAGS=-D@foo +cp y_no_cflags.txt x/y.go +go build -n x +stderr '-D@foo' + +-- x_gc.go.txt -- +package x + +//go:cgo_ldflag "-fplugin=foo.so" + +import "C" +-- cgo_yy.go.txt -- +package x + +//go:cgo_ldflag "-fplugin=foo.so" + +import "C" +-- x.go.txt -- +package x +-- y_fplugin.go.txt -- +package x +// #cgo CFLAGS: -fplugin=foo.so +import "C" +-- y_lbar_fplugin.go.txt -- +package x +// #cgo CFLAGS: -Ibar -fplugin=foo.so +import "C" +-- y_pkgconfig_dash_foo.txt -- +package x +// #cgo pkg-config: -foo +import "C" +-- y_pkgconfig_at_foo.txt -- +package x +// #cgo pkg-config: @foo +import "C" +-- y_cflags_at_foo.txt -- +package x +// #cgo CFLAGS: @foo +import "C" +-- y_cflags_dash_d.txt -- +package x +// #cgo CFLAGS: -D +import "C" +-- y_cflags_dash_d_space_at_foo.txt -- +package x +// #cgo CFLAGS: -D @foo +import "C" +-- y_cflags_dash_d_at_foo.txt -- +package x +// #cgo CFLAGS: -D@foo +import "C" +-- y_no_cflags.txt -- +package x +import "C" \ No newline at end of file