testify
testify copied to clipboard
Fix `CallerInfo()` source file paths
Summary
Error Trace produce strange output with invalid file paths: it join current directory and source filename, but source file can be located in another directory.
It is especially strange that these path conversions are not needed, since the desired path initially got from stacktrace.
Related issues
Closes https://github.com/stretchr/testify/issues/1230
:+1: thank you so much for this - it's been driving me nuts.
I compare test output before and after this PR (source code: https://github.com/bozaro/testify-example).
go test ./example
Before
--- FAIL: TestSimple (0.00s)
example_test.go:9:
Error Trace: /home/bozaro/github/testify-example/1.8.1/example/example_test.go:9
Error: Hello, world!
Test: TestSimple
FAIL
After
--- FAIL: TestSimple (0.00s)
example_test.go:9:
Error Trace: /home/bozaro/github/testify-example/1.8.1+1288/example/example_test.go:9
Error: Hello, world!
Test: TestSimple
FAIL
go test -trimpath ./example
Before
--- FAIL: TestSimple (0.00s)
example_test.go:9:
Error Trace: /home/bozaro/github/testify-example/1.8.1/example/example_test.go:9
Error: Hello, world!
Test: TestSimple
FAIL
After
--- FAIL: TestSimple (0.00s)
example_test.go:9:
Error Trace: github.com/bozaro/testify-example/example/example_test.go:9
Error: Hello, world!
Test: TestSimple
FAIL
bazel test --test_output=errors //...
Before
--- FAIL: TestSimple (0.00s)
example_test.go:9:
Error Trace: /home/bozaro/.cache/bazel/_bazel_bozaro/b8360faabc5337403861613e54adf2d0/sandbox/linux-sandbox/122/execroot/__main__/bazel-out/k8-fastbuild/bin/example/example_test_/example_test.runfiles/__main__/example/example_test.go:9
Error: Hello, world!
Test: TestSimple
FAIL
After
--- FAIL: TestSimple (0.00s)
example_test.go:9:
Error Trace: example/example_test.go:9
Error: Hello, world!
Test: TestSimple
FAIL
A create tree with two files:
example/helper/helper.goexample/example_test.go
And run go test ./example
Before
--- FAIL: TestHelper (0.00s)
helper.go:9:
Error Trace: /home/bozaro/github/testify-example/1.8.1/example/helper.go:9
/home/bozaro/github/testify-example/1.8.1/example/example_test.go:14
Error: Hello from helper!
Test: TestHelper
FAIL
File /home/bozaro/github/testify-example/1.8.1/example/helper.go is not exists.
After
--- FAIL: TestHelper (0.00s)
helper.go:9:
Error Trace: /home/bozaro/github/testify-example/1.8.1+1288/example/helper/helper.go:9
/home/bozaro/github/testify-example/1.8.1+1288/example/example_test.go:14
Error: Hello from helper!
Test: TestHelper
@boyan-soubachov, CallerInfo() is broken since 1.18 by printing path like filepath.Abs(path.Base(stacktracePath))
This breaks the Terminal File Links detection in VSCode, if you enable trimpaths. If you use Go Modules at ~/Repos/myrepo and the module is "github.com/me/myrepo" then this prints:
--- FAIL: TestSimple (0.00s)
example_test.go:9:
Error Trace: github.com/me/myrepo/example/example_test.go:9
Error: Hello, world!
Test: TestSimple
FAIL
This is annoying, and to me it was not clear how to fix this. Apparently I had "trimpaths" enabled, but I will now disable it again. Leaving this comment for discoverability by others.
@hermanbanken , is there a way to fix it so that it works for trimpaths as well?
Currently not I think, except maybe only doing trimpaths for the non-current module (is that possible?). I've submitted this feature request: https://github.com/golang/vscode-go/issues/2737.
I spend so much time debugging this debugging pulling my hair out that I want to avoid others from the same pain...