Skip to content

Commit 181cea6

Browse files
stdedosStavros Ntentos
and
Stavros Ntentos
authored
impr: CallerInfo should print full paths to the terminal (#1201)
* impr: `CallerInfo` should print full paths to the terminal I am proposing this simple change, which changes this output ``` --- FAIL: TestABC (0.00s) --- FAIL: TestABC/C (0.00s) /this/is/a/path/to/file_test.go:258: Error Trace: file_test.go:258 file_test.go:748 Error: Not equal: ... ``` to this: ``` --- FAIL: TestABC (0.00s) --- FAIL: TestABC/C (0.00s) /this/is/a/path/to/file_test.go:258: Error Trace: /this/is/a/path/to/file_test.go:258 /this/is/a/path/to/file_test.go:748 Error: Not equal: ... ``` With the latter output, it is much more straightforward to find the file you are looking for, even though in the displayed case, the file is the same. However, for VSCodium, the case is a little more helpful, since VSCodium's terminal is smart enough to recognize the output, and make links out of that input. Signed-off-by: Stavros Ntentos <[email protected]> * test: fix the tests that depended on the previous behavior Signed-off-by: Stavros Ntentos <[email protected]> Co-authored-by: Stavros Ntentos <[email protected]>
1 parent cf1284f commit 181cea6

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

assert/assertions.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"math"
1010
"os"
11+
"path/filepath"
1112
"reflect"
1213
"regexp"
1314
"runtime"
@@ -144,7 +145,8 @@ func CallerInfo() []string {
144145
if len(parts) > 1 {
145146
dir := parts[len(parts)-2]
146147
if (dir != "assert" && dir != "mock" && dir != "require") || file == "mock_test.go" {
147-
callers = append(callers, fmt.Sprintf("%s:%d", file, line))
148+
path, _ := filepath.Abs(file)
149+
callers = append(callers, fmt.Sprintf("%s:%d", path, line))
148150
}
149151
}
150152

mock/mock_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func Test_Mock_Chained_On(t *testing.T) {
151151
var mockedService = new(TestExampleImplementation)
152152

153153
// determine our current line number so we can assert the expected calls callerInfo properly
154-
_, _, line, _ := runtime.Caller(0)
154+
_, filename, line, _ := runtime.Caller(0)
155155
mockedService.
156156
On("TheExampleMethod", 1, 2, 3).
157157
Return(0).
@@ -164,14 +164,14 @@ func Test_Mock_Chained_On(t *testing.T) {
164164
Method: "TheExampleMethod",
165165
Arguments: []interface{}{1, 2, 3},
166166
ReturnArguments: []interface{}{0},
167-
callerInfo: []string{fmt.Sprintf("mock_test.go:%d", line+2)},
167+
callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+2)},
168168
},
169169
{
170170
Parent: &mockedService.Mock,
171171
Method: "TheExampleMethod3",
172172
Arguments: []interface{}{AnythingOfType("*mock.ExampleType")},
173173
ReturnArguments: []interface{}{nil},
174-
callerInfo: []string{fmt.Sprintf("mock_test.go:%d", line+4)},
174+
callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+4)},
175175
},
176176
}
177177
assert.Equal(t, expectedCalls, mockedService.ExpectedCalls)
@@ -521,7 +521,7 @@ func Test_Mock_Chained_UnsetOnlyUnsetsLastCall(t *testing.T) {
521521
var mockedService = new(TestExampleImplementation)
522522

523523
// determine our current line number so we can assert the expected calls callerInfo properly
524-
_, _, line, _ := runtime.Caller(0)
524+
_, filename, line, _ := runtime.Caller(0)
525525
mockedService.
526526
On("TheExampleMethod1", 1, 1).
527527
Return(0).
@@ -536,14 +536,14 @@ func Test_Mock_Chained_UnsetOnlyUnsetsLastCall(t *testing.T) {
536536
Method: "TheExampleMethod1",
537537
Arguments: []interface{}{1, 1},
538538
ReturnArguments: []interface{}{0},
539-
callerInfo: []string{fmt.Sprintf("mock_test.go:%d", line+2)},
539+
callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+2)},
540540
},
541541
{
542542
Parent: &mockedService.Mock,
543543
Method: "TheExampleMethod2",
544544
Arguments: []interface{}{2, 2},
545545
ReturnArguments: []interface{}{},
546-
callerInfo: []string{fmt.Sprintf("mock_test.go:%d", line+4)},
546+
callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+4)},
547547
},
548548
}
549549
assert.Equal(t, 2, len(expectedCalls))

0 commit comments

Comments
 (0)