From bad14dc055f270ad3f4fa542a50bdbda62cf89b0 Mon Sep 17 00:00:00 2001 From: Stavros Ntentos <11300730-stavros-relex@users.noreply.gitlab.com> Date: Thu, 9 Jun 2022 13:50:15 +0300 Subject: [PATCH 1/2] 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 <133706+stdedos@users.noreply.github.com> --- assert/assertions.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/assert/assertions.go b/assert/assertions.go index 6355dd2cf..fa1245b18 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -8,6 +8,7 @@ import ( "fmt" "math" "os" + "path/filepath" "reflect" "regexp" "runtime" @@ -144,7 +145,8 @@ func CallerInfo() []string { if len(parts) > 1 { dir := parts[len(parts)-2] if (dir != "assert" && dir != "mock" && dir != "require") || file == "mock_test.go" { - callers = append(callers, fmt.Sprintf("%s:%d", file, line)) + path, _ := filepath.Abs(file) + callers = append(callers, fmt.Sprintf("%s:%d", path, line)) } } From d3286050d26cba73f58b770db7998a90b3e84d75 Mon Sep 17 00:00:00 2001 From: Stavros Ntentos <11300730-stavros-relex@users.noreply.gitlab.com> Date: Wed, 29 Jun 2022 08:20:10 +0300 Subject: [PATCH 2/2] test: fix the tests that depended on the previous behavior Signed-off-by: Stavros Ntentos <133706+stdedos@users.noreply.github.com> --- mock/mock_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mock/mock_test.go b/mock/mock_test.go index 5cb40e366..f77dfe461 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -151,7 +151,7 @@ func Test_Mock_Chained_On(t *testing.T) { var mockedService = new(TestExampleImplementation) // determine our current line number so we can assert the expected calls callerInfo properly - _, _, line, _ := runtime.Caller(0) + _, filename, line, _ := runtime.Caller(0) mockedService. On("TheExampleMethod", 1, 2, 3). Return(0). @@ -164,14 +164,14 @@ func Test_Mock_Chained_On(t *testing.T) { Method: "TheExampleMethod", Arguments: []interface{}{1, 2, 3}, ReturnArguments: []interface{}{0}, - callerInfo: []string{fmt.Sprintf("mock_test.go:%d", line+2)}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+2)}, }, { Parent: &mockedService.Mock, Method: "TheExampleMethod3", Arguments: []interface{}{AnythingOfType("*mock.ExampleType")}, ReturnArguments: []interface{}{nil}, - callerInfo: []string{fmt.Sprintf("mock_test.go:%d", line+4)}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+4)}, }, } assert.Equal(t, expectedCalls, mockedService.ExpectedCalls) @@ -521,7 +521,7 @@ func Test_Mock_Chained_UnsetOnlyUnsetsLastCall(t *testing.T) { var mockedService = new(TestExampleImplementation) // determine our current line number so we can assert the expected calls callerInfo properly - _, _, line, _ := runtime.Caller(0) + _, filename, line, _ := runtime.Caller(0) mockedService. On("TheExampleMethod1", 1, 1). Return(0). @@ -536,14 +536,14 @@ func Test_Mock_Chained_UnsetOnlyUnsetsLastCall(t *testing.T) { Method: "TheExampleMethod1", Arguments: []interface{}{1, 1}, ReturnArguments: []interface{}{0}, - callerInfo: []string{fmt.Sprintf("mock_test.go:%d", line+2)}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+2)}, }, { Parent: &mockedService.Mock, Method: "TheExampleMethod2", Arguments: []interface{}{2, 2}, ReturnArguments: []interface{}{}, - callerInfo: []string{fmt.Sprintf("mock_test.go:%d", line+4)}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+4)}, }, } assert.Equal(t, 2, len(expectedCalls))