|
| 1 | +# go-github-mock to testify Migration Guide |
| 2 | + |
| 3 | +## Status |
| 4 | + |
| 5 | +**Completed: 8/14 files (57%)** |
| 6 | + |
| 7 | +### ✅ Migrated Files |
| 8 | +1. pkg/raw/raw_test.go |
| 9 | +2. pkg/github/actions_test.go (1,428 lines) |
| 10 | +3. pkg/github/context_tools_test.go (530 lines) |
| 11 | +4. pkg/github/dependabot_test.go (271 lines) |
| 12 | +5. pkg/github/gists_test.go (617 lines) |
| 13 | +6. pkg/github/repository_resource_test.go (307 lines) |
| 14 | +7. pkg/github/secret_scanning_test.go (267 lines) |
| 15 | +8. pkg/github/security_advisories_test.go (551 lines) |
| 16 | + |
| 17 | +### ⏳ Remaining Files |
| 18 | +1. pkg/github/issues_test.go (3,755 lines) - **Largest file** |
| 19 | +2. pkg/github/pullrequests_test.go (3,355 lines) |
| 20 | +3. pkg/github/repositories_test.go (3,532 lines) |
| 21 | +4. pkg/github/projects_test.go (1,711 lines) |
| 22 | +5. pkg/github/notifications_test.go (801 lines) |
| 23 | +6. pkg/github/search_test.go (776 lines) |
| 24 | + |
| 25 | +**Total remaining: ~13,930 lines** |
| 26 | + |
| 27 | +## Migration Pattern |
| 28 | + |
| 29 | +### Step 1: Remove Import |
| 30 | +```go |
| 31 | +// Remove this line |
| 32 | +"github.com/migueleliasweb/go-github-mock/src/mock" |
| 33 | +``` |
| 34 | + |
| 35 | +### Step 2: Replace Mock Client Creation |
| 36 | + |
| 37 | +**Pattern A: Simple Mock with Data** |
| 38 | +```go |
| 39 | +// BEFORE: |
| 40 | +mock.NewMockedHTTPClient( |
| 41 | + mock.WithRequestMatch( |
| 42 | + mock.GetAdvisories, |
| 43 | + mockData, |
| 44 | + ), |
| 45 | +) |
| 46 | + |
| 47 | +// AFTER: |
| 48 | +MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 49 | + GetAdvisories: mockResponse(t, http.StatusOK, mockData), |
| 50 | +}) |
| 51 | +``` |
| 52 | + |
| 53 | +**Pattern B: Mock with Custom Handler** |
| 54 | +```go |
| 55 | +// BEFORE: |
| 56 | +mock.NewMockedHTTPClient( |
| 57 | + mock.WithRequestMatchHandler( |
| 58 | + mock.GetUser, |
| 59 | + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 60 | + w.WriteHeader(http.StatusOK) |
| 61 | + json.NewEncoder(w).Encode(mockUser) |
| 62 | + }), |
| 63 | + ), |
| 64 | +) |
| 65 | + |
| 66 | +// AFTER: |
| 67 | +MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 68 | + GetUser: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 69 | + w.WriteHeader(http.StatusOK) |
| 70 | + json.NewEncoder(w).Encode(mockUser) |
| 71 | + }), |
| 72 | +}) |
| 73 | +``` |
| 74 | + |
| 75 | +**Pattern C: Mock with Query Parameter Expectations** |
| 76 | +```go |
| 77 | +// BEFORE: |
| 78 | +mock.NewMockedHTTPClient( |
| 79 | + mock.WithRequestMatchHandler( |
| 80 | + mock.GetSearchRepositories, |
| 81 | + expectQueryParams(t, map[string]string{"q": "test"}).andThen( |
| 82 | + mockResponse(t, http.StatusOK, mockData), |
| 83 | + ), |
| 84 | + ), |
| 85 | +) |
| 86 | + |
| 87 | +// AFTER: |
| 88 | +MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 89 | + GetSearchRepositories: expectQueryParams(t, map[string]string{"q": "test"}).andThen( |
| 90 | + mockResponse(t, http.StatusOK, mockData), |
| 91 | + ), |
| 92 | +}) |
| 93 | +``` |
| 94 | + |
| 95 | +**Pattern D: Empty Mock (for validation tests)** |
| 96 | +```go |
| 97 | +// BEFORE: |
| 98 | +mock.NewMockedHTTPClient() |
| 99 | + |
| 100 | +// AFTER: |
| 101 | +MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}) |
| 102 | +``` |
| 103 | + |
| 104 | +### Step 3: Fix Constant Names |
| 105 | + |
| 106 | +Replace old mock constants with new ones (note ID vs Id): |
| 107 | +- `mock.GetGistsByGistId` → `GetGistsByGistID` |
| 108 | +- `mock.GetNotificationsThreadsByThreadId` → `GetNotificationsThreadsByThreadID` |
| 109 | +- `mock.PatchGistsByGistId` → `PatchGistsByGistID` |
| 110 | + |
| 111 | +All endpoint constants are defined in `pkg/github/helper_test.go`. |
| 112 | + |
| 113 | +## Common Issues and Solutions |
| 114 | + |
| 115 | +### Issue 1: Extra Closing Braces |
| 116 | +**Symptom:** Syntax errors with `expected operand, found '}'` |
| 117 | + |
| 118 | +**Cause:** Regex replacement left extra `)` or `}` |
| 119 | + |
| 120 | +**Fix:** Check for patterns like: |
| 121 | +- `mockResponse(t, http.StatusOK, data})` should be `mockResponse(t, http.StatusOK, data)` |
| 122 | +- `}),` at wrong indentation - should be `})` to close map then `,` |
| 123 | + |
| 124 | +### Issue 2: Missing Closing Braces |
| 125 | +**Symptom:** `missing ',' in argument list` |
| 126 | + |
| 127 | +**Fix:** Map literal should close with `})` not just `)`: |
| 128 | +```go |
| 129 | +MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 130 | + GetUser: mockResponse(t, http.StatusOK, mockUser), |
| 131 | +}), // Note the closing }), |
| 132 | +``` |
| 133 | + |
| 134 | +### Issue 3: Raw Content Endpoints |
| 135 | +For raw content endpoints that handle paths with slashes (e.g., `pkg/github/actions.go`), use wildcard patterns: |
| 136 | +- `GetRawReposContentsByOwnerByRepoByPath` uses `{path:.*}` pattern |
| 137 | +- This is already configured in `helper_test.go` |
| 138 | + |
| 139 | +## Testing After Migration |
| 140 | + |
| 141 | +```bash |
| 142 | +# Test specific file |
| 143 | +go test ./pkg/github -run TestFunctionName -v |
| 144 | + |
| 145 | +# Test all GitHub tests |
| 146 | +go test ./pkg/github -v |
| 147 | + |
| 148 | +# Run linter |
| 149 | +script/lint |
| 150 | +``` |
| 151 | + |
| 152 | +## Available Helper Functions |
| 153 | + |
| 154 | +All defined in `pkg/github/helper_test.go`: |
| 155 | + |
| 156 | +1. **MockHTTPClientWithHandlers** - Creates HTTP client with route handlers |
| 157 | +2. **mockResponse** - Creates standard JSON response handler |
| 158 | +3. **expectQueryParams** - Validates query parameters |
| 159 | +4. **expectPath** - Validates request path |
| 160 | +5. **expect** - Combines multiple expectations |
| 161 | + |
| 162 | +## Endpoint Constants |
| 163 | + |
| 164 | +All ~130 endpoint constants are in `pkg/github/helper_test.go`, including: |
| 165 | +- User endpoints (GetUser, GetUserStarred, etc.) |
| 166 | +- Repository endpoints (GetReposByOwnerByRepo, etc.) |
| 167 | +- Issues endpoints (GetReposIssuesByOwnerByRepoByIssueNumber, etc.) |
| 168 | +- Pull request endpoints |
| 169 | +- Actions endpoints |
| 170 | +- And many more... |
| 171 | + |
| 172 | +## Final Steps (After All Files Migrated) |
| 173 | + |
| 174 | +1. Remove `migueleliasweb/go-github-mock` from go.mod |
| 175 | +2. Remove `pkg/raw/raw_mock.go` (temporarily restored for compatibility) |
| 176 | +3. Run `go mod tidy` |
| 177 | +4. Run `script/licenses` to update license files |
| 178 | +5. Run full test suite: `script/test` |
| 179 | +6. Run linter: `script/lint` |
0 commit comments