From 81316ff50a4a81cc2077f8a831b4e1896c56a564 Mon Sep 17 00:00:00 2001 From: Hossein Zolfi Date: Fri, 27 Jan 2023 18:20:09 +0330 Subject: [PATCH] testing: add -fullpath to go test When -test.fullpath flag is provided to go test, go test displays the full file names in error messages. Fixes #37708 Change-Id: I6096e75ed816fd42205810f20624e495047a73a2 Reviewed-on: https://go-review.googlesource.com/c/go/+/463837 Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills Reviewed-by: Than McIntosh Run-TryBot: Bryan Mills --- src/cmd/go/alldocs.go | 3 +++ src/cmd/go/internal/test/flagdefs.go | 1 + src/cmd/go/internal/test/test.go | 3 +++ src/cmd/go/internal/test/testflag.go | 1 + src/cmd/go/testdata/script/test_fullpath.txt | 21 ++++++++++++++++++++ src/testing/testing.go | 7 +++++-- 6 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/cmd/go/testdata/script/test_fullpath.txt diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 84afcab7a03..6780c919ae7 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -2987,6 +2987,9 @@ // -failfast // Do not start new tests after the first test failure. // +// -fullpath +// Show full file names in the error messages. +// // -fuzz regexp // Run the fuzz test matching the regular expression. When specified, // the command line argument must match exactly one package within the diff --git a/src/cmd/go/internal/test/flagdefs.go b/src/cmd/go/internal/test/flagdefs.go index d9f4fca17a1..aa2207693c7 100644 --- a/src/cmd/go/internal/test/flagdefs.go +++ b/src/cmd/go/internal/test/flagdefs.go @@ -19,6 +19,7 @@ var passFlagToTest = map[string]bool{ "cpu": true, "cpuprofile": true, "failfast": true, + "fullpath": true, "fuzz": true, "fuzzminimizetime": true, "fuzztime": true, diff --git a/src/cmd/go/internal/test/test.go b/src/cmd/go/internal/test/test.go index 250046104b9..aaeb70a544d 100644 --- a/src/cmd/go/internal/test/test.go +++ b/src/cmd/go/internal/test/test.go @@ -240,6 +240,9 @@ control the execution of any test: -failfast Do not start new tests after the first test failure. + -fullpath + Show full file names in the error messages. + -fuzz regexp Run the fuzz test matching the regular expression. When specified, the command line argument must match exactly one package within the diff --git a/src/cmd/go/internal/test/testflag.go b/src/cmd/go/internal/test/testflag.go index 69c0a2872e4..970c2f59e9a 100644 --- a/src/cmd/go/internal/test/testflag.go +++ b/src/cmd/go/internal/test/testflag.go @@ -50,6 +50,7 @@ func init() { cf.StringVar(&testCPUProfile, "cpuprofile", "", "") cf.Bool("failfast", false, "") cf.StringVar(&testFuzz, "fuzz", "", "") + cf.Bool("fullpath", false, "") cf.StringVar(&testList, "list", "", "") cf.StringVar(&testMemProfile, "memprofile", "", "") cf.String("memprofilerate", "", "") diff --git a/src/cmd/go/testdata/script/test_fullpath.txt b/src/cmd/go/testdata/script/test_fullpath.txt new file mode 100644 index 00000000000..8e015522381 --- /dev/null +++ b/src/cmd/go/testdata/script/test_fullpath.txt @@ -0,0 +1,21 @@ +[short] skip + +# test with -fullpath +! go test ./x/... -fullpath +stdout '^ +.+/gopath/src/x/fullpath/fullpath_test.go:8: test failed' +# test without -fullpath +! go test ./x/... +stdout '^ +fullpath_test.go:8: test failed' + +-- go.mod -- +module example +-- x/fullpath/fullpath_test.go -- +package fullpath_test + +import ( + "testing" +) + +func TestFullPath(t *testing.T) { + t.Error("test failed") +} diff --git a/src/testing/testing.go b/src/testing/testing.go index fc34cbf28b5..2d0fd89137a 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -441,6 +441,7 @@ func Init() { parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "run at most `n` tests in parallel") testlog = flag.String("test.testlogfile", "", "write test action log to `file` (for use only by cmd/go)") shuffle = flag.String("test.shuffle", "off", "randomize the execution order of tests and benchmarks") + fullPath = flag.Bool("test.fullpath", false, "show full file names in error messages") initBenchmarkFlags() initFuzzFlags() @@ -472,6 +473,7 @@ var ( parallel *int shuffle *string testlog *string + fullPath *bool haveExamples bool // are there examples? @@ -751,8 +753,9 @@ func (c *common) decorate(s string, skip int) string { file := frame.File line := frame.Line if file != "" { - // Truncate file name at last file name separator. - if index := strings.LastIndex(file, "/"); index >= 0 { + if *fullPath { + // If relative path, truncate file name at last file name separator. + } else if index := strings.LastIndex(file, "/"); index >= 0 { file = file[index+1:] } else if index = strings.LastIndex(file, "\\"); index >= 0 { file = file[index+1:]