-
Notifications
You must be signed in to change notification settings - Fork 87
Modernize tests #584
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
Modernize tests #584
Conversation
- Replace for i := 0; i < b.N; i++ with for b.Loop() (Go 1.24) - Replace os.Setenv/defer os.Unsetenv with t.Setenv() (Go 1.18) Assisted-By: cagent
Use t.TempDir() instead of manual os.MkdirTemp/defer os.RemoveAll patterns. This is cleaner, automatically handles cleanup on test failure, and is the recommended approach since Go 1.14. Assisted-By: cagent
Use t.Context() instead of context.Background() in tests. This is cleaner and ensures the context is properly tied to the test's lifecycle. Available since Go 1.24. Assisted-By: cagent
Summary of ChangesHello @dgageot, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the Go unit tests to align with modern best practices, primarily by integrating Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Hey - I've left some high level feedback:
- The change from the classic
for i := 0; i < b.N; i++ { ... }benchmark loop tofor b.Loop() { ... }inBenchmarkHandler_CreateResponseassumes a newer Go version; please confirm the project’s Go toolchain actually supportstesting.B.Loop()or keep the old pattern to avoid compilation issues. - The new
forbidigorules forcontext.Background/TODOrely on anissues.exclude-rulestext match ofuse t\.to scope them to tests; consider tightening this (e.g., using a dedicated message or linter ID) so future message wording changes don’t accidentally alter which files are exempted.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The change from the classic `for i := 0; i < b.N; i++ { ... }` benchmark loop to `for b.Loop() { ... }` in `BenchmarkHandler_CreateResponse` assumes a newer Go version; please confirm the project’s Go toolchain actually supports `testing.B.Loop()` or keep the old pattern to avoid compilation issues.
- The new `forbidigo` rules for `context.Background/TODO` rely on an `issues.exclude-rules` text match of `use t\.` to scope them to tests; consider tightening this (e.g., using a dedicated message or linter ID) so future message wording changes don’t accidentally alter which files are exempted.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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.
Code Review
This pull request is an excellent effort to modernize the Go test suite. The changes systematically replace older testing patterns with newer, safer, and cleaner alternatives provided by the Go testing package. Key improvements include using t.TempDir() for automatic temporary directory cleanup (which also fixes a resource leak), t.Setenv() for managing environment variables, and t.Context() for better cancellation propagation in tests. Additionally, benchmark tests have been updated to use the modern b.Loop() syntax. The inclusion of new forbidigo linter rules in .golangci.yml is a great step to enforce these best practices going forward. The changes are consistent, well-executed across numerous files, and significantly enhance the maintainability and robustness of the tests. The quality of this work is high, and I have no further recommendations for improvement.
|
I'm working on fixing the linter issue. The rules I've added have the nasty side effect of changing the existing forbidigo config |
Add lint rules to enforce modern Go test patterns: - t.TempDir() instead of os.MkdirTemp() in tests - t.Setenv() instead of os.Setenv() in tests - t.Context() instead of context.Background()/context.TODO() in tests The rules use forbidigo with proper pattern syntax (matching identifier names without parentheses) and are scoped to test files only via an exclusion rule with 'path-except: _test\.go$'. Assisted-By: cagent
1faf478 to
0ad19e2
Compare
ilopezluna
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 very much @dgageot
doringeman
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!
I tried to use a cagent based agent to modernize the Go unit tests as much as possible.