testify icon indicating copy to clipboard operation
testify copied to clipboard

Fix `CallerInfo()` source file paths

Open bozaro opened this issue 3 years ago • 1 comments

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

bozaro avatar Oct 26 '22 08:10 bozaro

:+1: thank you so much for this - it's been driving me nuts.

twelvelabs avatar Oct 27 '22 04:10 twelvelabs

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

bozaro avatar Oct 28 '22 20:10 bozaro

A create tree with two files:

  • example/helper/helper.go
  • example/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

bozaro avatar Oct 29 '22 09:10 bozaro

@boyan-soubachov, CallerInfo() is broken since 1.18 by printing path like filepath.Abs(path.Base(stacktracePath))

bozaro avatar Nov 07 '22 15:11 bozaro

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 avatar Apr 19 '23 09:04 hermanbanken

@hermanbanken , is there a way to fix it so that it works for trimpaths as well?

boyan-soubachov avatar Apr 19 '23 10:04 boyan-soubachov

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...

hermanbanken avatar Apr 19 '23 10:04 hermanbanken