Make Error and Fatal functions compatible with testing.T#286
Make Error and Fatal functions compatible with testing.T#286sirockin wants to merge 6 commits intoform3tech-oss:masterfrom
Conversation
…/f1 into sirockin-tb-compatibility
|
I'm surprised the CI pipeline is failing on interface compatibility given the changes are non-breaking. |
|
@sirockin This is a breaking change for any users that may have already done what we're trying here - create an abstraction to share code between f1 and other parts of their codebase. We want to avoid breaking users as much as possible hence my comment on the other PR: #279 (comment) I'm open to ideas, but I still think adding a |
| // Special case, single error argument | ||
| if len(args) == 1 { | ||
| err, ok := args[0].(error) | ||
| if ok { | ||
| t.logger.Error("iteration failed", log.IterationAttr(t.Iteration), log.ErrorAttr(err)) | ||
| t.FailNow() | ||
| return | ||
| } | ||
| } | ||
| t.Log(args...) |
There was a problem hiding this comment.
This would have different logging depending on the number of arguments.
I think if we just add a new log Attr helper that merges a list of errors, we can handle any number of arguments in a consistent way
There was a problem hiding this comment.
Thanks for the quick review and useful feedback, @nvloff-f3 .
I'm still not exactly clear on what you are looking for though. The original comment said // Error is equivalent to Log followed by Fail. (consistent with behaviour of testing.T) so I was aiming to get as close to that as possible.
Please could you clarify the behaviour you would like to see like to see with:
- a single argument of type
error - a single argument of type other than
error - more than one argument, all of type
error - more than one argument, not all of type
error
It would be great if we could create some tests for these scenarios but given the way that concrete logger instances are embedded into t, that may be difficult to achieve.
It could also be argued that rather than introducing a helper to log the arguments, we should amend Log so that it does what we want then use that in the methods. This would simplify code, make behaviour consistent across these methods and make the original comments for Error and Fatal accurate. However it would mean that we couldn't force logger.Error to be called if standard printf arguments were supplied to Error or Fatal.
Anyway let me know your thoughts and I'll prepare a commit.
Thanks again!
I may be being dense (it's early in the morning for me 😂 ) but I'm still not clear on why this would be a breaking change. The two methods will still behave exactly as previously for the only case which was previously compilable: a single error argument. |
Change Error and Fatal to accept args ...any instead of err error, matching testing.T and allowing shared test helpers between f1 scenarios and standard go tests. - Error, Errorf, Fatal, Fatalf log at ERROR level (not via Log/Logf) - Log/Logf use fmt.Sprintln for formatting (aligned with testing.T) - Log/Logf add iteration and vuid context to all output - Add commonTInterface for compile-time signature verification - Add tests for exact logging format (level, msg, iteration, vuid) Based on #286 by @sirockin BREAKING CHANGE: Error(err error) and Fatal(err error) are now Error(args ...any) and Fatal(args ...any). Single-error call sites remain valid: Error(err) and Fatal(err) still compile and behave as before. Closes #278 Closes #286
What
Making
testing.Errorandtesting.Fatalfunctions compatible with testing.T by changing the singleerrorargument to a variable argument list acceptingany.Why
Facilitates reusing test scenarios by extracting common testing interface
Testing
The only tests which currently exist for the two functions confirm that the methods fail the load test do not test the logging functionality. We update these with test cases to cover different arguments being passed in.
Example code
main.go:
main_test.go:
Resolves #278