-
-
Notifications
You must be signed in to change notification settings - Fork 239
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
add stacktrace to chain and formatted data #396
Conversation
Hey @gavv. I decided to add Stacktrace to chain and fill it when fail() called. chain_test started failing because of comparing assertionFailure in tests. Should I remake tests to make it not compare |
This is a good idea indeed, this way trace would be much more convenient. |
I think it's done. Please check Maybe there is a point to think about shortening stacktrace if it's too long. |
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 PR! Here are some comments.
opChain.leave() | ||
|
||
assert.NotNil(t, handler.failure) | ||
assert.NotEmpty(t, handler.failure.Stacktrace) |
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.
Can we do some simple sanity check here?
E.g. that Stacktrace contains "TestChain_Stacktrace" and "chain.fail".
formatter.go
Outdated
// Enables printing of stacktrace on failure | ||
EnableStacktrace bool |
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.
nit: let's move it upper, right after other flags (..., DisableRequests, DisableResponses)
formatter.go
Outdated
@@ -183,7 +186,8 @@ type FormatData struct { | |||
AssertType string | |||
AssertSeverity string | |||
|
|||
Errors []string | |||
Errors []string | |||
Stacktrace []string |
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.
nit: let's move it after Request and Response, and add HaveStacktrace, for consistency with other fields in FormatData. HaveXxx is not strictly necessary, but is descriptive when used in template.
formatter.go
Outdated
{{- range $n, $call := .Stacktrace }} | ||
{{ $call | indent }} | ||
{{- end -}} |
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.
Let's guard it with HaveStacktrace check, for consistency with other stuff. We should ensure that if HaveStacktrace, no additional empty lines are inserted.
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.
Also let's move stacktrace between "test name" and "assertion" and add "trace" header. It will look like this:
<error message>
test name: TestBlah
trace:
<stacktrace here>
assertion:
Request("GET").Expect().JSON().Object()
actual value:
<blah>
reference value:
<blah>
This way, error message and test name, which are most important, go first; trace and assertion are grouped together, which makes sense because both of them are kind of traces; assertion and actual/expected/reference are also grouped which is also logical because all them describe assertion made.
formatter.go
Outdated
func (f *DefaultFormatter) fillStacktrace( | ||
data *FormatData, ctx *AssertionContext, failure *AssertionFailure, | ||
) { | ||
if f.EnableStacktrace { | ||
data.Stacktrace = strings.Split(failure.Stacktrace, "\n\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.
Let's make fillStacktrace guarantee that data.Stacktrace is always a non-nil slice, like we do for Errors in fillErrors.
TBH I don't remember why, but for some reason it was important or useful for Errors slice, in the context of templates :)
I've played a bit with this patch and I think it may be convenient to resemble behavior of testify to produce a more compact trace. It is implemented here: https://github.com/stretchr/testify/blob/c5fc9d6b6b21ea89be8480c0dc35e2977ab988f6/assert/assertions.go#L147 If we'll use assert.CallerInfo() instead of debug.Stack(), the stack will look like this:
which is very compact, fits the style of httpexpect output, and looks usual to testify users. However, it loses some information compared to original trace, and some users may prefer to see the full trace. So I'd make it configurable - replace EnableStacktrace boolean with enum of three values: disabled, full, and compact. For compact mode, if feasible, I'd even reimplement assert.CallerInfo() and use the following format instead:
so instead of:
it would be:
IMHO this is the best compromise between providing enough information and keeping it compact. What do you think? Do you find these ideas useful? Would you like to implement some of them in this PR, or should I create a separate issue? |
TBH I don't see much difference between compact and full modes. |
Agree
Awesome, thanks!
I feel that supporting stanard format makes sense just because it's common and complete. Someone may be just used to it, someone may want absolute paths, someone may want to use tools like panicparse or maybe their editor understands this trace format, etc. Also having unprocessed trace may be useful to debug httpexpect itself. We could add it later. Could you use enum instead of boolean? This will allow us to add formats in future, doesn't matter if this pr will implement 1 or 2. |
Another little thought. Since we'll collect caller info and then format it, we can store caller info instead of formatted string in AssertionFailure, and do formatting in DefaultFormatter. First, it's just logical, and second, it will give us additional flexibility for free: user will be able to implement their own formatting when using custom formatter. |
I got your idea. I'll do it and ping you when it's ready for review |
1e7da05
to
71795fe
Compare
@gavv I think it's ready for review. |
Thanks, will look in upcoming days! BTW I don't remember if I mentioned - welcome to discord chat if you like that format. |
Thanks for update! Follow-up commit: ca5a5dd renames, two stacktrace modes, changes in stacktrace format, and improvements in tests |
#160
EnableStacktrace
Stacktrace
on fail in chainStacktrace
toFormatData