guyarb / golang-test-annotations

A github action which annotates failed tests.
MIT License
34 stars 10 forks source link

Test logging no longer affects retrieval of file name and line number #19

Closed IsaacLambat closed 2 years ago

IsaacLambat commented 2 years ago

Currently, if any logging is in the output of the test before the assert failure, the file name and line number is not detected properly causing GitHub to not be able to do the annotations. This change makes it so the assert failures are extracted (using regex), and then the file name and line number is extracted from that.

For example, a Go test with the following code on in main_test.go with the failure on line 18:

func TestReproLog(t *testing.T) {
    log.Println("This is a test")
    require.True(t, false)
}

used to cause Github to receive the annotation text:

::error file=/2022/04/04 18,line=40::=== RUN   TestReproLog%0A2022/04/04 18:40:10 This is a test%0A    main_test.go:18: %0A         Error Trace:    main_test.go:18%0A          Error:          Should be true%0A           Test:           TestReproLog%0A--- FAIL: TestReproLog (0.00s)%0A

and now it would receive:

::error file=main_test.go,line=18::    main_test.go:18: %0A         Error Trace:    main_test.go:18%0A          Error:          Should be true%0A           Test:           TestReproLog%0A

Example tests for this change can be seen here (along with tests that can be ran locally if needed using https://github.com/nektos/act): https://github.com/IsaacLambat/golang-test-annotations-testing

Example of annotations on a PR: https://github.com/IsaacLambat/golang-test-annotations-testing/pull/1/files