7
7
"encoding/json"
8
8
"os"
9
9
"os/exec"
10
+ "sync"
10
11
"testing"
11
12
"time"
12
13
@@ -16,16 +17,48 @@ import (
16
17
"github.com/stretchr/testify/require"
17
18
)
18
19
19
- func TestE2E (t * testing.T ) {
20
- e2eServerToken := os .Getenv ("GITHUB_MCP_SERVER_E2E_TOKEN" )
21
- if e2eServerToken == "" {
22
- t .Fatalf ("GITHUB_MCP_SERVER_E2E_TOKEN environment variable is not set" )
23
- }
20
+ var (
21
+ // Shared variables and sync.Once instances to ensure one-time execution
22
+ getTokenOnce sync.Once
23
+ e2eToken string
24
24
25
- // Build the Docker image for the MCP server.
26
- buildDockerImage (t )
25
+ buildOnce sync.Once
26
+ buildError error
27
+ )
27
28
28
- t .Setenv ("GITHUB_PERSONAL_ACCESS_TOKEN" , e2eServerToken ) // The MCP Client merges the existing environment.
29
+ // getE2EToken ensures the environment variable is checked only once and returns the token
30
+ func getE2EToken (t * testing.T ) string {
31
+ getTokenOnce .Do (func () {
32
+ e2eToken = os .Getenv ("GITHUB_MCP_SERVER_E2E_TOKEN" )
33
+ if e2eToken == "" {
34
+ t .Fatalf ("GITHUB_MCP_SERVER_E2E_TOKEN environment variable is not set" )
35
+ }
36
+ })
37
+ return e2eToken
38
+ }
39
+
40
+ // ensureDockerImageBuilt makes sure the Docker image is built only once across all tests
41
+ func ensureDockerImageBuilt (t * testing.T ) {
42
+ buildOnce .Do (func () {
43
+ t .Log ("Building Docker image for e2e tests..." )
44
+ cmd := exec .Command ("docker" , "build" , "-t" , "github/e2e-github-mcp-server" , "." )
45
+ cmd .Dir = ".." // Run this in the context of the root, where the Dockerfile is located.
46
+ output , err := cmd .CombinedOutput ()
47
+ buildError = err
48
+ if err != nil {
49
+ t .Logf ("Docker build output: %s" , string (output ))
50
+ }
51
+ })
52
+
53
+ // Check if the build was successful
54
+ require .NoError (t , buildError , "expected to build Docker image successfully" )
55
+ }
56
+
57
+ func TestE2E (t * testing.T ) {
58
+ token := getE2EToken (t )
59
+ ensureDockerImageBuilt (t )
60
+
61
+ t .Setenv ("GITHUB_PERSONAL_ACCESS_TOKEN" , token ) // The MCP Client merges the existing environment.
29
62
args := []string {
30
63
"docker" ,
31
64
"run" ,
@@ -81,20 +114,11 @@ func TestE2E(t *testing.T) {
81
114
82
115
// Then the login in the response should match the login obtained via the same
83
116
// token using the GitHub API.
84
- client := github .NewClient (nil ).WithAuthToken (e2eServerToken )
117
+ client := github .NewClient (nil ).WithAuthToken (token )
85
118
user , _ , err := client .Users .Get (context .Background (), "" )
86
119
require .NoError (t , err , "expected to get user successfully" )
87
120
require .Equal (t , trimmedContent .Login , * user .Login , "expected login to match" )
88
121
})
89
122
90
123
require .NoError (t , client .Close (), "expected to close client successfully" )
91
124
}
92
-
93
- func buildDockerImage (t * testing.T ) {
94
- t .Log ("Building Docker image for e2e tests..." )
95
-
96
- cmd := exec .Command ("docker" , "build" , "-t" , "github/e2e-github-mcp-server" , "." )
97
- cmd .Dir = ".." // Run this in the context of the root, where the Dockerfile is located.
98
- output , err := cmd .CombinedOutput ()
99
- require .NoError (t , err , "expected to build Docker image successfully, output: %s" , string (output ))
100
- }
0 commit comments