-
Notifications
You must be signed in to change notification settings - Fork 13
fix: tests should not make real network calls [IDE-1269] #1013
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?
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Summary of ChangesHello @andrewrobinsonhodges-snyk, 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 refactors the unit test setup to strictly prevent real network calls, improving test reliability and speed. It introduces a new mock network layer, updates the Highlights
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.
Code Review
This pull request is a great step towards making the unit tests more robust and isolated by preventing real network calls. The introduction of mock_network.go and the new README.md for test utilities are excellent additions for maintainability and developer guidance.
I've identified a few areas for improvement to enhance the new testing framework's design and prevent potential issues. My main suggestions focus on:
- Improving the design of the
UnitTesthelper to allow for mock customization. - Correcting the documentation to reflect the proper usage pattern.
- Making the mock setup more robust to prevent potential panics.
- Cleaning up an unused parameter in a test helper function.
Overall, these changes are very positive, and with a few adjustments, the new test utilities will be even more powerful and safer to use.
application/server/server_test.go
Outdated
| return setupCustomServer(t, c, nil) | ||
| } | ||
|
|
||
| func setupServerWithCustomDI(t *testing.T, c *config.Config, useMocks bool) (server.Local, *testsupport.JsonRPCRecorder) { |
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.
The useMocks parameter is no longer used in this function after the removal of the if !useMocks block. To improve code clarity and remove dead code, this parameter should be removed from the function signature and any call sites.
| func setupServerWithCustomDI(t *testing.T, c *config.Config, useMocks bool) (server.Local, *testsupport.JsonRPCRecorder) { | |
| func setupServerWithCustomDI(t *testing.T, c *config.Config) (server.Local, *testsupport.JsonRPCRecorder) { |
| config.SetCurrentConfig(c) | ||
| CLIDownloadLockFileCleanUp(t, c) | ||
| c.Engine().GetConfiguration().Set(code_workflow.ConfigurationSastSettings, &sast_contract.SastResponse{SastEnabled: true, LocalCodeEngine: sast_contract.LocalCodeEngine{ | ||
| engineConfig.Set(code_workflow.ConfigurationSastSettings, &sast_contract.SastResponse{SastEnabled: true, LocalCodeEngine: sast_contract.LocalCodeEngine{ |
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.
Hm. If our engine is a mock, why do we need this? I would expect that we just need to set the expectation, because this Set should not do anything.
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.
Back compatibility; most tests just retrieve and use the configuration (which is a real object, not a mock)
rrama
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.
I am getting a lot of tests fail for me locally for the following reasons:
- Unexpected call to *mocks.MockEngine.SetConfiguration([0xblah])
- Unexpected call to *mocks.MockEngine.GetWorkflows([])
- Unexpected call to *mocks.MockEngine.Register(...)
- panic: runtime error: invalid memory address or nil pointer dereference
- Condition never satisfied (for init CLI tests)
- Wrong CLI distribution channel.
- Getting CLI download URL not mocked or not returning what it used to.
- Trying to get lessons.
- Sentry events not being as expected (or trying to be sent for real).
- Attempting to check CCI FF.
- No obvious reason.
As such, I cannot approve until all the tests that work for me on main work for me with your changes.
|
|
||
| ### Dependency Injection for Tests | ||
|
|
||
| After setting up the config, you need to initialize the dependency injection container. **Always use `di.TestInit(t)` for unit tests**: |
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.
"you may need to"*
Nit pick: Calling it a "container" seems like the wrong word.
How about:
After setting up the config, you need may to initialize the test dependencies via injection. This can be done by calling
di.TestInit(t)in your unit test.
| |-----------|--------------|----------|---------------| | ||
| | Unit Test | `testutil.UnitTest(t)` | `di.TestInit(t)` | ❌ Blocked | | ||
| | Integration Test | `testutil.IntegTest(t)` | `di.Init()` | ✅ Allowed | | ||
| | Smoke Test | `testutil.SmokeTest(t, false)` | `di.Init()` | ✅ Allowed | |
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.
Smoke test should never mock network calls, but integration tests can do some, so maybe we need a column for "Real Network Calls" and "Mocked Network Calls".
|
|
||
| **Never use `di.Init()` in unit tests** - it creates real API clients and scanners that can make network calls. | ||
|
|
||
| ### When to Use What |
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.
This section should be the first thing in the README IMO. Plus maybe add a bit about when to use what :p
Description
Small refactor of unit test setup to avoid making real network calls.
Integration and smoke tests are unaffected.
Checklist
make generate)make lint-fix)