-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Chore: Add testify-based HTTP mocking infrastructure and pilot migration #1606
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
…st.go - Add MockHTTPClientWithHandlers helper function for HTTP-level mocking - Add path pattern matching support for GitHub API endpoints - Migrate pkg/github/git_test.go from go-github-mock to new infrastructure - Keep go-github-mock dependency for now (other files still use it) Co-authored-by: JoannaaKL <[email protected]>
- Migrate pkg/github/code_scanning_test.go to new infrastructure - Add comprehensive migration documentation in docs/testing-migration.md - Fix linter warning in helper_test.go - All tests and lint checks pass - 2 of 16 test files migrated, 14 remaining for incremental migration Co-authored-by: JoannaaKL <[email protected]>
- Remove unused OnAny method - Extract executeHandler helper to eliminate code duplication - Consolidate MockHTTPClientWithHandler to use MockHTTPClientWithHandlers - Simplify transport implementation (single multiHandlerTransport) - All tests and lint checks pass Co-authored-by: JoannaaKL <[email protected]>
|
@copilot remove this file - |
Co-authored-by: JoannaaKL <[email protected]>
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.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
SamMorrowDrums
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 like this, but is there value in defining github API route constances somewhere and sharing them versus writing them out per test?
Maybe, just a passing thought. The new pattern is otherwise clean and nice I feel.
pkg/github/code_scanning_test.go
Outdated
| ), | ||
| ), | ||
| mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ | ||
| "GET /repos/{owner}/{repo}/code-scanning/alerts/{alert_number}": mockResponse(t, http.StatusOK, mockAlert), |
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.
@copilot don't add a path constant, reuse mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber.Pattern. Do it in other tests too
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.
Done in 9e403f5. Updated both code_scanning_test.go and git_test.go to reuse mock.EndpointPattern constants instead of hardcoded paths.
- Add mock import to code_scanning_test.go and git_test.go - Replace hardcoded paths with mock.GetRepos*.Pattern references - Ensures consistency with existing test patterns and easier maintenance Co-authored-by: JoannaaKL <[email protected]>
pkg/github/code_scanning_test.go
Outdated
| ), | ||
| ), | ||
| mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ | ||
| mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber.Method + " " + mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber.Pattern: mockResponse(t, http.StatusOK, mockAlert), |
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.
@copilot sorry for the confusion, revert previous changes. You need to define constants in this repo and not reuse them from migueleliasweb/go-github-mock. In the end the goal is to stop using this repo for mocks.
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.
Done in b96a791. I've reverted the previous changes and now define the endpoint path constants in helper_test.go within this repo instead of reusing them from go-github-mock. This prepares for the eventual removal of the go-github-mock dependency.
…hub-mock - Add endpoint pattern constants to helper_test.go - Update code_scanning_test.go to use new constants - Update git_test.go to use new constants - Removes dependency on go-github-mock endpoint patterns - Prepares for eventual removal of go-github-mock dependency Co-authored-by: JoannaaKL <[email protected]>
Closes #1492
Description
Implements Phase 1 and begins Phase 2 of migrating test infrastructure from
migueleliasweb/go-github-mocktostretchr/testify-based HTTP mocking. Consolidates testing dependencies while establishing patterns for incremental migration of remaining test files.Migration Status: 2 of 16 test files migrated (454 lines), 14 files remaining (~14,210 lines).
Changes
New HTTP Mocking Infrastructure (
pkg/github/helper_test.go)MockHTTPClientWithHandlers: Route-based HTTP client mocking with GitHub API path patterns{owner},{repo},{tree}wildcards for flexible endpoint matchingexecuteHandler: Eliminates response creation duplication across transport implementationshelper_test.go:GetReposByOwnerByRepoGetReposGitTreesByOwnerByRepoByTreeGetReposCodeScanningAlertsByOwnerByRepoGetReposCodeScanningAlertsByOwnerByRepoByAlertNumberMigrated Test Files
pkg/github/git_test.go(196 lines) - pilot migration, simplest filepkg/github/code_scanning_test.go(258 lines) - demonstrates query parameter validationBoth test files now use locally-defined endpoint pattern constants instead of hardcoded paths for better maintainability and consistency.
Dependencies
github.com/stretchr/objx(required by testify/mock)go-github-mockuntil full migration complete (will be removed in future PRs)Tradeoffs
Partial migration over full replacement: Migrated 2 files to establish infrastructure and patterns rather than all 16 files. Reduces PR scope and risk while enabling future incremental work. Full migration deferred to subsequent PRs as outlined in issue.
HTTP-level mocking over interface-based: Maintains current architectural patterns (HTTP transport mocking) rather than introducing interface wrappers around GitHub client. Interface-based approach would require extensive production code changes; HTTP-level mocking achieves dependency consolidation goals with test-only changes.
Local constants over go-github-mock patterns: Defines endpoint pattern constants locally in this repo rather than reusing
go-github-mockpatterns. This enables eventual removal of thego-github-mockdependency while maintaining clean, maintainable test code.No migration documentation: Migration patterns are documented inline in test files and helper functions rather than in a separate documentation file to keep the PR focused on code changes.
Alternatives
sed/automated migration: Considered batch migration via scripting, but endpoint patterns vary significantly across test files (query params, error scenarios, pagination). Manual migration ensures correctness and provides learning for establishing robust patterns.
Remove go-github-mock immediately: Cannot remove dependency until all test files migrated. Interim state maintains both approaches until migration complete.
Reuse mock.EndpointPattern constants: Initially considered reusing existing
mock.EndpointPatternconstants fromgo-github-mock, but defining local constants better supports the goal of removing thego-github-mockdependency entirely.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.