-
Notifications
You must be signed in to change notification settings - Fork 157
Add the file/line to the output of Junit XML #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add the file/line to the output of Junit XML #470
Conversation
4aa0ab1 to
ab484fc
Compare
|
@dnephin kindly review when you get the chance 🙏 happy to address any concerns |
dnephin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR! If you pull in the latest changes from main that should fix the test failures.
In general I think the approach of parsing the file:line from the output is ok, but I don't think it's safe to expect the "Error Trace" prefix.
We may want to do this parsing on the event before we join all the lines. I think that will make it easier to find the relevant file:line.
There's also this proposal to add an OutputType field: golang/go#62728 (comment)
I think that field would make it much easier to find the appropriate file:line in the output. We could parse it directly from only the events that have an OutputType: error.
cmd/tool/parser/parser.go
Outdated
| // Usually the failure would contain a line like this: | ||
| // Error Trace: /Users/user/proje/path/to/package/some_test.go:42 | ||
| // where the full path to the file is in the same line as "Error Trace:" | ||
| if strings.Contains(outputLine, "Error Trace") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this I've ever see an "Error Trace" line like this before. Is this from a specific test helper library?
Most test failures I see look like this:
fails_test.go:50: failed sub
I don't think we can rely on this prefix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad! I misunderstood where 'Error Trace' it's coming from, and wrongly assumed it's from Go's test tooling. But, in fact this comes from the testify package that I often use for constructing assertions.
Here's how they define the value for an 'error trace': https://github.com/stretchr/testify/blob/7127b6099902025b069e842d93bcdd6e8462eb5c/assert/assertions.go#L207-L286
I picked this option because it was a more correct file:line value in some cases. But clearly we cannot rely on users to been using testify, and there's no way for us to mimic this behaviour without some code being invoked in the error...
I think it's best that we switch to the common output for now, as you recommend. We can discuss separately if it's worth to add optional "support for testify-style error output", if we find it valuable.
|
Hi, bumping this PR - also a user of gotestsum, and interested in a file attribute for tests. @asfaltboy are you planning on coming back to this? |
|
Hi! Bumping this as well as it's a feature that'd be highly useful for me. thanks! |
match filename.go:1 instead as per discussion & comments: gotestyourself#470 (review)
ab484fc to
c3a3dad
Compare
Pardon for the delay, and thanks a lot for the review @dnephin , you were right to block the merging I messed up with the 'Error Trace' assumption. I just realise that a big benefit of that testify feature is that it returns a full path to the file, which they obtain from the stack trace. I think I addressed the comments as requested, and I'd rather merge it as-is as if we can, and come back to this to iterate for other features like getting the full path (requires understanding where the package lives in the path, and where our file is in that package). |
|
@dnephin ping. Is this something we could expect merged in the next couple of weeks? |
dnephin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reworking this!
A couple more test cases and I think this is good.
| // ParseFailure parses the output of the `go test` for a test failure and | ||
| // returns the file and line number of the failed test case. | ||
| func ParseFailure(output string) (file string, line int, err error) { | ||
| re, err := regexp.Compile(`^\s*([_\w]+\.go):(\d+):`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are many other valid filename characters, space, dash, dots, and maybe even unicode? I assume other characters like + could also be valid?
I'm not sure if regex is the best tool here. https://pkg.go.dev/strings#Cut or strings.Index maybe?
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestParseFailure_Ok(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some negative tests would also be great. Ex:
What if the file:line appears in the middle of other text?
What if there is no match?
Implement the proposed solution in #264 (comment) to parse the text output of a failure (which contains a useful trace) add then file/line attributes to the junit xml output.
This is useful when parsing the XML output e.g in CI, in order to link to the file (e.g Github annotations, as done by mikepenz/action-junit-report).
I tested it on a local project, and can confirm the relative path to the file and the line was added as attributes when I ran gotestsum in the root of my module. Here is some anonymised output example excerpt:
Merging this would be a proper fix to #264
TODO
current working dir and clean it up, hard-code in tests), as it's currently a bit too low down the treegoModuleFilePath