Add TempDir and NewLogFile test utilities#369
Conversation
| // | ||
| // When tests are run with -v, the temporary directory absolute | ||
| // path will be logged. | ||
| func TempDir(t *testing.T, path ...string) string { |
There was a problem hiding this comment.
I'm not a fan of copying so much of the code that exists in t.TempDir(). Can we use t.TempDir and then just have the cleanup function copy or archive the contents in the case of a failure?
There was a problem hiding this comment.
Personally I like that the temp dir is already created on its final destination, so no matter how wrong the test goes, the files are kept.
Copying/archiving the temp dir after a test failure adds another point of failure and complexity that I'd like to avoid.
I'm not a fan of copying so much of the code that exists in t.TempDir()
I haven't copied much of it, well, the bit I copied was only the part sanitising the test name to create the folder.
There was a problem hiding this comment.
Personally I like that the temp dir is already created on its final destination, so no matter how wrong the test goes, the files are kept.
Copying/archiving the temp dir after a test failure adds another point of failure and complexity that I'd like to avoid.
I agree, it makes sense
There was a problem hiding this comment.
This isn't a blocker.
but I strongly suggest implementing "removeAll" like the stdlib testing library does. https://cs.opensource.google/go/go/+/refs/tags/go1.25.5:src/testing/testing.go;l=1392-1420
Windows lock files are not released immediately, so even if the code is correct and has released the lock, it can be milliseconds before you can delete the file.
And things like this are why I'm not in favor of reimplementing something that has been thoroughly tested, with lots of edge cases. Especially when it is under active maintenance.
There was a problem hiding this comment.
That's an interesting edge case. I can open a follow up PR to implement something like that. Or maybe even do the moving archiving logic. This can be implemented without breaking the API/behaviour.
Co-authored-by: Anderson Queiroz <[email protected]>
💚 Build Succeeded
History
cc @belimawr |
|
@leehinman @kruskall did you folks have a chance to see my replies to your comments? |
What does this PR do?
This PR adds two helpers for tests:
fs.TempDircreates a temporary folder that is kept when tests fails.NewLogFilecreates a file that can be used as output for a logger or a a process. Methods to search strings in the file are also providedWhy is it important?
It helps us to keep temporary files and logs generated by tests when the test fails for easier debugging.
Checklist
## Author's Checklist## Related issues