chore: simplify tests with usetesting linter#2060
Open
alexandear wants to merge 1 commit intogithub:mainfrom
Open
chore: simplify tests with usetesting linter#2060alexandear wants to merge 1 commit intogithub:mainfrom
alexandear wants to merge 1 commit intogithub:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR simplifies unit tests across the repository by migrating from manual environment variable and context management to using built-in testing.T helper methods. The changes improve test maintainability by eliminating boilerplate cleanup code and reducing the risk of test pollution.
Changes:
- Enabled the
usetestinglinter in.golangci.ymlto enforce modern testing patterns - Replaced
context.Background()witht.Context()across ~200+ test call sites for automatic cancellation on test completion - Replaced manual
os.Setenv()/os.Unsetenv()witht.Setenv()for automatic cleanup inpkg/inventory/instructions_test.go - Replaced manual directory change/restore logic with
t.Chdir(t.TempDir())ininternal/toolsnaps/toolsnaps_test.go
Reviewed changes
Copilot reviewed 34 out of 34 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
.golangci.yml |
Added usetesting linter with 7 checks enabled (context-background, context-todo, os-chdir, os-mkdir-temp, os-setenv, os-create-temp, os-temp-dir) |
pkg/scopes/fetcher_test.go |
Replaced context.Background() → t.Context() in 4 locations |
pkg/raw/raw_test.go |
Replaced context.Background() → t.Context() and removed unused context import |
pkg/inventory/registry_test.go |
Replaced context.Background() → t.Context() in ~90 test assertions; improved error message formatting by extracting lengths to variables |
pkg/inventory/instructions_test.go |
Replaced 16 lines of manual os.Setenv/os.Unsetenv cleanup with single t.Setenv() call; removed unused os import |
pkg/http/transport/graphql_features_test.go |
Replaced context.Background() → t.Context() in 3 locations and removed unused context import |
pkg/http/handler_test.go |
Replaced context.Background() → t.Context() in 2 locations |
pkg/github/ui_capability_test.go |
Replaced context.Background() → t.Context() and removed unused context import |
pkg/github/server_test.go |
Replaced context.Background() → t.Context() in server creation |
pkg/github/security_advisories_test.go |
Replaced context.Background() → t.Context() in 4 test cases and removed unused context import |
pkg/github/secret_scanning_test.go |
Replaced context.Background() → t.Context() in 2 test cases and removed unused context import |
pkg/github/search_test.go |
Replaced context.Background() → t.Context() in 5 test cases and removed unused context import |
pkg/github/scope_filter_test.go |
Replaced context.Background() → t.Context() in 2 locations and removed unused context import |
pkg/github/repository_resource_test.go |
Replaced context.Background() → t.Context() and removed unused context import |
pkg/github/repositories_test.go |
Replaced context.Background() → t.Context() in 13 test cases and removed unused context import |
pkg/github/pullrequests_test.go |
Replaced context.Background() → t.Context() in 22 test cases; removed unused context import and cleaned up extra blank lines |
pkg/github/projects_test.go |
Replaced context.Background() → t.Context() in 15 test cases and removed unused context import |
pkg/github/notifications_test.go |
Replaced context.Background() → t.Context() in 6 test cases and removed unused context import |
pkg/github/labels_test.go |
Replaced context.Background() → t.Context() in 3 test cases and removed unused context import |
pkg/github/issues_test.go |
Replaced context.Background() → t.Context() in 17 test cases; reformatted var declarations and removed unused context import and extra blank lines |
pkg/github/helper_test.go |
Replaced context.Background() → t.Context() in test helper and removed unused context import |
pkg/github/git_test.go |
Replaced context.Background() → t.Context() and removed unused context import |
pkg/github/gists_test.go |
Replaced context.Background() → t.Context() in 4 test cases and removed unused context import |
pkg/github/feature_flags_test.go |
Replaced context.Background() → t.Context() in 2 test cases (using ContextWithDeps wrapper) |
pkg/github/dynamic_tools_test.go |
Replaced context.Background() → t.Context() in 5 handler calls and removed unused context import |
pkg/github/discussions_test.go |
Replaced context.Background() → t.Context() in 4 test cases and removed unused context import |
pkg/github/dependencies_test.go |
Replaced context.Background() → t.Context() in 5 feature flag checks |
pkg/github/dependabot_test.go |
Replaced context.Background() → t.Context() in 2 test cases and removed unused context import |
pkg/github/copilot_test.go |
Replaced context.Background() → t.Context() in poll config setup; changed var pageOfFakeBots = func → pageOfFakeBots := func (style improvement); removed unused context import and extra blank line |
pkg/github/context_tools_test.go |
Replaced context.Background() → t.Context() in 3 test cases and removed unused context import |
pkg/github/code_scanning_test.go |
Replaced context.Background() → t.Context() in 2 test cases and removed unused context import |
pkg/github/actions_test.go |
Replaced context.Background() → t.Context() in 11 test cases and removed unused context import |
pkg/errors/error_test.go |
Replaced context.Background() → t.Context() in 12 test cases involving error context management |
internal/toolsnaps/toolsnaps_test.go |
Replaced 5 lines of manual directory change/restore with t.Chdir(t.TempDir()) in helper function; added t.Helper() |
1483369 to
996c8d0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Simplifies unit tests by using
testing.Thelpers liket.Setenvandt.Context, and enables theusetestinglinter to enforce these patterns.Why
Keeps tests concise and consistent by relying on standard
testinghelpers and linting for cleanup-safe patterns.What changed
usetestinglinter in.golangci.yml.t.Setenvandt.Contextacross packages.MCP impact
Prompts tested (tool changes only)
Security / limits
Tool renaming
deprecated_tool_aliases.goLint & tests
./script/lint./script/testDocs