Skip to content

Comments

chore: simplify tests with usetesting linter#2060

Open
alexandear wants to merge 1 commit intogithub:mainfrom
alexandear-org:chore/usetesting
Open

chore: simplify tests with usetesting linter#2060
alexandear wants to merge 1 commit intogithub:mainfrom
alexandear-org:chore/usetesting

Conversation

@alexandear
Copy link
Contributor

Summary

Simplifies unit tests by using testing.T helpers like t.Setenv and t.Context, and enables the usetesting linter to enforce these patterns.

Why

Keeps tests concise and consistent by relying on standard testing helpers and linting for cleanup-safe patterns.

What changed

  • Enabled the usetesting linter in .golangci.yml.
  • Replaced manual env/context handling in tests with t.Setenv and t.Context across packages.

MCP impact

  • No tool or API changes
  • Tool schema or behavior changed
  • New tool added

Prompts tested (tool changes only)

  • N/A

Security / limits

  • No security or limits impact
  • Auth / permissions considered
  • Data exposure, filtering, or token/size limits considered

Tool renaming

  • I am renaming tools as part of this PR (e.g. a part of a consolidation effort)
    • I have added the new tool aliases in deprecated_tool_aliases.go
  • I am not renaming tools as part of this PR

Lint & tests

  • Linted locally with ./script/lint
  • Tested locally with ./script/test

Docs

  • Not needed
  • Updated (README / docs / examples)

@alexandear alexandear requested a review from a team as a code owner February 23, 2026 11:02
Copilot AI review requested due to automatic review settings February 23, 2026 11:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 usetesting linter in .golangci.yml to enforce modern testing patterns
  • Replaced context.Background() with t.Context() across ~200+ test call sites for automatic cancellation on test completion
  • Replaced manual os.Setenv()/os.Unsetenv() with t.Setenv() for automatic cleanup in pkg/inventory/instructions_test.go
  • Replaced manual directory change/restore logic with t.Chdir(t.TempDir()) in internal/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 = funcpageOfFakeBots := 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()

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 34 out of 34 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant