Skip to content

Commit a4a08e2

Browse files
committed
Reduce the amount of content served from files in tests.
1 parent e732b0c commit a4a08e2

9 files changed

+52
-59
lines changed

internal/pull/pull_test.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@ import (
1818
const initialActionRepository = "./pull_test/codeql-action-initial.git"
1919
const modifiedActionRepository = "./pull_test/codeql-action-modified.git"
2020

21+
const releaseSomeCodeQLVersionOnMainContent = "This isn't really a CodeQL bundle!"
22+
23+
var releaseSomeCodeQLVersionOnMain = github.RepositoryRelease{
24+
TagName: github.String("some-codeql-version-on-main"),
25+
Name: github.String("some-codeql-version-on-main"),
26+
Assets: []*github.ReleaseAsset{
27+
&github.ReleaseAsset{
28+
ID: github.Int64(1),
29+
Name: github.String("codeql-bundle.tar.gz"),
30+
Size: github.Int(len(releaseSomeCodeQLVersionOnMainContent)),
31+
},
32+
},
33+
}
34+
35+
const releaseSomeCodeQLVersionOnV1AndV2Content = "This isn't a CodeQL bundle either, but it's a different not-bundle."
36+
37+
var releaseSomeCodeQLVersionOnV1AndV2 = github.RepositoryRelease{
38+
TagName: github.String("some-codeql-version-on-v1-and-v2"),
39+
Name: github.String("some-codeql-version-on-v1-and-v2"),
40+
Assets: []*github.ReleaseAsset{
41+
&github.ReleaseAsset{
42+
ID: github.Int64(2),
43+
Name: github.String("codeql-bundle.tar.gz"),
44+
Size: github.Int(len(releaseSomeCodeQLVersionOnV1AndV2Content)),
45+
},
46+
},
47+
}
48+
2149
func getTestPullService(t *testing.T, temporaryDirectory string, gitCloneURL string, githubURL string) pullService {
2250
cacheDirectory := cachedirectory.NewCacheDirectory(temporaryDirectory)
2351
var githubDotComClient *github.Client
@@ -123,43 +151,43 @@ func TestPullReleases(t *testing.T) {
123151
temporaryDirectory := test.CreateTemporaryDirectory(t)
124152
githubTestServer, githubURL := test.GetTestHTTPServer(t)
125153
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) {
126-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-main.json", response)
154+
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnMain, response)
127155
}).Methods("GET")
128156
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/1", func(response http.ResponseWriter, request *http.Request) {
129-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-main.bin", response)
157+
test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnMainContent, response)
130158
}).Methods("GET").Headers("accept", "application/octet-stream")
131159
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) {
132-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-v1-and-v2.json", response)
160+
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnV1AndV2, response)
133161
}).Methods("GET")
134162
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/2", func(response http.ResponseWriter, request *http.Request) {
135-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", response)
163+
test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnV1AndV2Content, response)
136164
}).Methods("GET").Headers("accept", "application/octet-stream")
137165
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL)
138166
err := pullService.pullGit(true)
139167
require.NoError(t, err)
140168
err = pullService.pullReleases()
141169
require.NoError(t, err)
142170

143-
test.RequireFilesAreEqual(t, "./pull_test/api/asset-some-codeql-version-on-main.bin", pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz"))
144-
test.RequireFilesAreEqual(t, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"))
171+
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnMainContent, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz"))
172+
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnV1AndV2Content, pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"))
145173

146174
// If we pull again, we should only download assets where the size mismatches.
147175
err = ioutil.WriteFile(pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"), []byte("Some nonsense."), 0644)
148176
require.NoError(t, err)
149177
githubTestServer, githubURL = test.GetTestHTTPServer(t)
150178
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) {
151-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-main.json", response)
179+
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnMain, response)
152180
}).Methods("GET")
153181
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) {
154-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-v1-and-v2.json", response)
182+
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnV1AndV2, response)
155183
}).Methods("GET")
156184
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/2", func(response http.ResponseWriter, request *http.Request) {
157-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", response)
185+
test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnV1AndV2Content, response)
158186
}).Methods("GET").Headers("accept", "application/octet-stream")
159187
pullService = getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL)
160188
err = pullService.pullReleases()
161189
require.NoError(t, err)
162190

163-
test.RequireFilesAreEqual(t, "./pull_test/api/asset-some-codeql-version-on-main.bin", pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz"))
164-
test.RequireFilesAreEqual(t, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"))
191+
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnMainContent, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz"))
192+
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnV1AndV2Content, pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"))
165193
}

internal/pull/pull_test/api/asset-some-codeql-version-on-main.bin

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/pull/pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/pull/pull_test/api/release-some-codeql-version-on-main.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

internal/pull/pull_test/api/release-some-codeql-version-on-v1-and-v2.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

internal/push/push_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ func TestCreateRepositoryWhenUserIsOwner(t *testing.T) {
4444
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
4545
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
4646
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
47-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-owner.json", response)
47+
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response)
4848
}).Methods("GET")
4949
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
5050
response.WriteHeader(http.StatusNotFound)
5151
}).Methods("GET")
5252
githubTestServer.HandleFunc("/api/v3/user/repos", func(response http.ResponseWriter, request *http.Request) {
53-
response.Write([]byte("{}"))
53+
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
5454
}).Methods("POST")
5555
_, err := pushService.createRepository()
5656
require.NoError(t, err)
@@ -61,13 +61,13 @@ func TestUpdateRepositoryWhenUserIsOwner(t *testing.T) {
6161
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
6262
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
6363
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
64-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-owner.json", response)
64+
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response)
6565
}).Methods("GET")
6666
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
6767
test.ServeHTTPResponseFromObject(t, github.Repository{Homepage: github.String(repositoryHomepage)}, response)
6868
}).Methods("GET")
6969
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
70-
response.Write([]byte("{}"))
70+
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
7171
}).Methods("PATCH")
7272
_, err := pushService.createRepository()
7373
require.NoError(t, err)
@@ -78,7 +78,7 @@ func TestUpdateRepositoryWhenUserIsOwnerForced(t *testing.T) {
7878
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
7979
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
8080
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
81-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-owner.json", response)
81+
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response)
8282
}).Methods("GET")
8383
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
8484
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
@@ -99,24 +99,24 @@ func TestCreateOrganizationAndRepositoryWhenOrganizationIsOwner(t *testing.T) {
9999
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
100100
organizationCreated := false
101101
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
102-
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-not-owner.json", response)
102+
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("user")}, response)
103103
}).Methods("GET")
104104
githubTestServer.HandleFunc("/api/v3/orgs/destination-repository-owner", func(response http.ResponseWriter, request *http.Request) {
105105
if organizationCreated {
106-
response.Write([]byte("{}"))
106+
test.ServeHTTPResponseFromObject(t, github.Organization{}, response)
107107
} else {
108108
response.WriteHeader(http.StatusNotFound)
109109
}
110110
}).Methods("GET")
111111
githubTestServer.HandleFunc("/api/v3/admin/organizations", func(response http.ResponseWriter, request *http.Request) {
112-
response.Write([]byte("{}"))
112+
test.ServeHTTPResponseFromObject(t, github.Organization{}, response)
113113
organizationCreated = true
114114
}).Methods("POST")
115115
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
116116
response.WriteHeader(http.StatusNotFound)
117117
}).Methods("GET")
118118
githubTestServer.HandleFunc("/api/v3/orgs/destination-repository-owner/repos", func(response http.ResponseWriter, request *http.Request) {
119-
response.Write([]byte("{}"))
119+
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
120120
}).Methods("POST")
121121
_, err := pushService.createRepository()
122122
require.NoError(t, err)

internal/push/push_test/api/user-is-not-owner.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

internal/push/push_test/api/user-is-owner.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ func GetTestHTTPServer(t *testing.T) (*mux.Router, string) {
3636
return mux, server.URL
3737
}
3838

39-
func ServeHTTPResponseFromFile(t *testing.T, statusCode int, path string, response http.ResponseWriter) {
40-
data, err := ioutil.ReadFile(path)
41-
require.NoError(t, err)
42-
response.WriteHeader(statusCode)
43-
_, err = response.Write(data)
39+
func ServeHTTPResponseFromString(t *testing.T, content string, response http.ResponseWriter) {
40+
_, err := response.Write([]byte(content))
4441
require.NoError(t, err)
4542
}
4643

@@ -51,12 +48,10 @@ func ServeHTTPResponseFromObject(t *testing.T, object interface{}, response http
5148
require.NoError(t, err)
5249
}
5350

54-
func RequireFilesAreEqual(t *testing.T, expectedPath string, actualPath string) {
55-
expectedData, err := ioutil.ReadFile(expectedPath)
56-
require.NoError(t, err)
51+
func RequireFileHasContent(t *testing.T, expectedContent string, actualPath string) {
5752
actualData, err := ioutil.ReadFile(actualPath)
5853
require.NoError(t, err)
59-
require.Equal(t, expectedData, actualData)
54+
require.Equal(t, []byte(expectedContent), actualData)
6055
}
6156

6257
func CheckExpectedReferencesInRepository(t *testing.T, repositoryPath string, expectedReferences []string) {

0 commit comments

Comments
 (0)