Skip to content

Commit 86e1054

Browse files
CopilotJoannaaKL
andcommitted
Migrate GitHub REST tests to internal testify-based mock helper
Co-authored-by: JoannaaKL <67866556+JoannaaKL@users.noreply.github.com>
1 parent 6570172 commit 86e1054

File tree

9 files changed

+420
-14
lines changed

9 files changed

+420
-14
lines changed

docs/testing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This project uses a combination of unit tests and end-to-end (e2e) tests to ensu
77
- Unit tests are located alongside implementation, with filenames ending in `_test.go`.
88
- Currently the preference is to use internal tests i.e. test files do not have `_test` package suffix.
99
- Tests use [testify](https://github.com/stretchr/testify) for assertions and require statements. Use `require` when continuing the test is not meaningful, for example it is almost never correct to continue after an error expectation.
10-
- Mocking is performed using [go-github-mock](https://github.com/migueleliasweb/go-github-mock) or `githubv4mock` for simulating GitHub rest and GQL API responses.
10+
- REST mocking in unit tests is performed with the internal `pkg/testmock` helpers (built on `net/http` and testify assertions) or `githubv4mock` for simulating GitHub REST and GQL API responses.
1111
- Each tool's schema is snapshotted and checked for changes using the `toolsnaps` utility (see below).
1212
- Tests are designed to be explicit and verbose to aid maintainability and clarity.
1313
- Handler unit tests should take the form of:

pkg/github/helper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const (
5959
PatchReposIssuesByOwnerByRepoByIssueNumber = "PATCH /repos/{owner}/{repo}/issues/{issue_number}"
6060
GetReposIssuesSubIssuesByOwnerByRepoByIssueNumber = "GET /repos/{owner}/{repo}/issues/{issue_number}/sub_issues"
6161
PostReposIssuesSubIssuesByOwnerByRepoByIssueNumber = "POST /repos/{owner}/{repo}/issues/{issue_number}/sub_issues"
62-
DeleteReposIssuesSubIssueByOwnerByRepoByIssueNumber = "DELETE /repos/{owner}/{repo}/issues/{issue_number}/sub_issues"
62+
DeleteReposIssuesSubIssueByOwnerByRepoByIssueNumber = "DELETE /repos/{owner}/{repo}/issues/{issue_number}/sub_issue"
6363
PatchReposIssuesSubIssuesPriorityByOwnerByRepoByIssueNumber = "PATCH /repos/{owner}/{repo}/issues/{issue_number}/sub_issues/priority"
6464

6565
// Pull request endpoints

pkg/github/issues_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
"github.com/github/github-mcp-server/internal/githubv4mock"
1515
"github.com/github/github-mcp-server/internal/toolsnaps"
1616
"github.com/github/github-mcp-server/pkg/lockdown"
17+
mock "github.com/github/github-mcp-server/pkg/testmock"
1718
"github.com/github/github-mcp-server/pkg/translations"
1819
"github.com/google/go-github/v79/github"
1920
"github.com/google/jsonschema-go/jsonschema"
20-
"github.com/migueleliasweb/go-github-mock/src/mock"
2121
"github.com/shurcooL/githubv4"
2222
"github.com/stretchr/testify/assert"
2323
"github.com/stretchr/testify/require"

pkg/github/projects_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"testing"
99

1010
"github.com/github/github-mcp-server/internal/toolsnaps"
11+
mock "github.com/github/github-mcp-server/pkg/testmock"
1112
"github.com/github/github-mcp-server/pkg/translations"
1213
gh "github.com/google/go-github/v79/github"
1314
"github.com/google/jsonschema-go/jsonschema"
14-
"github.com/migueleliasweb/go-github-mock/src/mock"
1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
1717
)

pkg/github/pullrequests_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/google/jsonschema-go/jsonschema"
1616
"github.com/shurcooL/githubv4"
1717

18-
"github.com/migueleliasweb/go-github-mock/src/mock"
18+
mock "github.com/github/github-mcp-server/pkg/testmock"
1919
"github.com/stretchr/testify/assert"
2020
"github.com/stretchr/testify/require"
2121
)

pkg/github/repositories_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111

1212
"github.com/github/github-mcp-server/internal/toolsnaps"
1313
"github.com/github/github-mcp-server/pkg/raw"
14+
mock "github.com/github/github-mcp-server/pkg/testmock"
1415
"github.com/github/github-mcp-server/pkg/translations"
1516
"github.com/github/github-mcp-server/pkg/utils"
1617
"github.com/google/go-github/v79/github"
1718
"github.com/google/jsonschema-go/jsonschema"
18-
"github.com/migueleliasweb/go-github-mock/src/mock"
1919
"github.com/modelcontextprotocol/go-sdk/mcp"
2020
"github.com/stretchr/testify/assert"
2121
"github.com/stretchr/testify/require"
@@ -306,11 +306,12 @@ func Test_GetFileContents(t *testing.T) {
306306
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
307307
// Request for "refs/heads/main" -> 404 (doesn't exist)
308308
// Request for "refs/heads/develop" (default branch) -> 200
309+
path := r.URL.EscapedPath()
309310
switch {
310-
case strings.Contains(r.URL.Path, "heads/main"):
311+
case strings.Contains(path, "heads/main") || strings.Contains(path, "heads%2Fmain"):
311312
w.WriteHeader(http.StatusNotFound)
312313
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
313-
case strings.Contains(r.URL.Path, "heads/develop"):
314+
case strings.Contains(path, "heads/develop") || strings.Contains(path, "heads%2Fdevelop"):
314315
w.WriteHeader(http.StatusOK)
315316
_, _ = w.Write([]byte(`{"ref": "refs/heads/develop", "object": {"sha": "abc123def456"}}`))
316317
default:

pkg/github/search_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"testing"
88

99
"github.com/github/github-mcp-server/internal/toolsnaps"
10+
mock "github.com/github/github-mcp-server/pkg/testmock"
1011
"github.com/github/github-mcp-server/pkg/translations"
1112
"github.com/google/go-github/v79/github"
1213
"github.com/google/jsonschema-go/jsonschema"
13-
"github.com/migueleliasweb/go-github-mock/src/mock"
1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
1616
)

pkg/raw/raw_mock.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package raw
22

3-
import "github.com/migueleliasweb/go-github-mock/src/mock"
3+
import "github.com/github/github-mcp-server/pkg/testmock"
44

5-
var GetRawReposContentsByOwnerByRepoByPath mock.EndpointPattern = mock.EndpointPattern{
5+
var GetRawReposContentsByOwnerByRepoByPath = testmock.EndpointPattern{
66
Pattern: "/{owner}/{repo}/HEAD/{path:.*}",
77
Method: "GET",
88
}
9-
var GetRawReposContentsByOwnerByRepoByBranchByPath mock.EndpointPattern = mock.EndpointPattern{
9+
var GetRawReposContentsByOwnerByRepoByBranchByPath = testmock.EndpointPattern{
1010
Pattern: "/{owner}/{repo}/refs/heads/{branch}/{path:.*}",
1111
Method: "GET",
1212
}
13-
var GetRawReposContentsByOwnerByRepoByTagByPath mock.EndpointPattern = mock.EndpointPattern{
13+
var GetRawReposContentsByOwnerByRepoByTagByPath = testmock.EndpointPattern{
1414
Pattern: "/{owner}/{repo}/refs/tags/{tag}/{path:.*}",
1515
Method: "GET",
1616
}
17-
var GetRawReposContentsByOwnerByRepoBySHAByPath mock.EndpointPattern = mock.EndpointPattern{
17+
var GetRawReposContentsByOwnerByRepoBySHAByPath = testmock.EndpointPattern{
1818
Pattern: "/{owner}/{repo}/{sha}/{path:.*}",
1919
Method: "GET",
2020
}

0 commit comments

Comments
 (0)