]> Cypherpunks repositories - gostls13.git/commitdiff
testing: add -fullpath to go test
authorHossein Zolfi <hossein.zolfi@gmail.com>
Fri, 27 Jan 2023 14:50:09 +0000 (18:20 +0330)
committerGopher Robot <gobot@golang.org>
Wed, 22 Feb 2023 16:42:04 +0000 (16:42 +0000)
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 <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>

src/cmd/go/alldocs.go
src/cmd/go/internal/test/flagdefs.go
src/cmd/go/internal/test/test.go
src/cmd/go/internal/test/testflag.go
src/cmd/go/testdata/script/test_fullpath.txt [new file with mode: 0644]
src/testing/testing.go

index 84afcab7a036557e039ebbf2d7bb1ffd6902c186..6780c919ae73dc1be2eb54c1abab1e9855f826ff 100644 (file)
 //     -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
index d9f4fca17a1c42bf6329e64391249830171e78b9..aa2207693c74a2a29e9bcbe0a8fe3b97e3e2a40b 100644 (file)
@@ -19,6 +19,7 @@ var passFlagToTest = map[string]bool{
        "cpu":                  true,
        "cpuprofile":           true,
        "failfast":             true,
+       "fullpath":             true,
        "fuzz":                 true,
        "fuzzminimizetime":     true,
        "fuzztime":             true,
index 250046104b9ec8273c375db0155a99784426911c..aaeb70a544de11f2d38582bca6d31f7c0504b3c6 100644 (file)
@@ -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
index 69c0a2872e4ebe9082a42a468ffbb277c201c358..970c2f59e9aa16a61426e80a9b8c48c549800f38 100644 (file)
@@ -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 (file)
index 0000000..8e01552
--- /dev/null
@@ -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")
+}
index fc34cbf28b57f2f901dac992d687e503e73aba4c..2d0fd89137aba023956a6ce2c4254ee48ef6f77e 100644 (file)
@@ -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:]