diff --git a/internal/pull/pull_test.go b/internal/pull/pull_test.go index aedb905..af4572e 100644 --- a/internal/pull/pull_test.go +++ b/internal/pull/pull_test.go @@ -18,6 +18,34 @@ import ( const initialActionRepository = "./pull_test/codeql-action-initial.git" const modifiedActionRepository = "./pull_test/codeql-action-modified.git" +const releaseSomeCodeQLVersionOnMainContent = "This isn't really a CodeQL bundle!" + +var releaseSomeCodeQLVersionOnMain = github.RepositoryRelease{ + TagName: github.String("some-codeql-version-on-main"), + Name: github.String("some-codeql-version-on-main"), + Assets: []*github.ReleaseAsset{ + &github.ReleaseAsset{ + ID: github.Int64(1), + Name: github.String("codeql-bundle.tar.gz"), + Size: github.Int(len(releaseSomeCodeQLVersionOnMainContent)), + }, + }, +} + +const releaseSomeCodeQLVersionOnV1AndV2Content = "This isn't a CodeQL bundle either, but it's a different not-bundle." + +var releaseSomeCodeQLVersionOnV1AndV2 = github.RepositoryRelease{ + TagName: github.String("some-codeql-version-on-v1-and-v2"), + Name: github.String("some-codeql-version-on-v1-and-v2"), + Assets: []*github.ReleaseAsset{ + &github.ReleaseAsset{ + ID: github.Int64(2), + Name: github.String("codeql-bundle.tar.gz"), + Size: github.Int(len(releaseSomeCodeQLVersionOnV1AndV2Content)), + }, + }, +} + func getTestPullService(t *testing.T, temporaryDirectory string, gitCloneURL string, githubURL string) pullService { cacheDirectory := cachedirectory.NewCacheDirectory(temporaryDirectory) var githubDotComClient *github.Client @@ -123,16 +151,16 @@ func TestPullReleases(t *testing.T) { temporaryDirectory := test.CreateTemporaryDirectory(t) githubTestServer, githubURL := test.GetTestHTTPServer(t) githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-main.json", response) + test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnMain, response) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/1", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-main.bin", response) + test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnMainContent, response) }).Methods("GET").Headers("accept", "application/octet-stream") githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-v1-and-v2.json", response) + test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnV1AndV2, response) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/2", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", response) + test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnV1AndV2Content, response) }).Methods("GET").Headers("accept", "application/octet-stream") pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL) err := pullService.pullGit(true) @@ -140,26 +168,26 @@ func TestPullReleases(t *testing.T) { err = pullService.pullReleases() require.NoError(t, err) - 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")) - 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")) + test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnMainContent, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz")) + test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnV1AndV2Content, pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz")) // If we pull again, we should only download assets where the size mismatches. err = ioutil.WriteFile(pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"), []byte("Some nonsense."), 0644) require.NoError(t, err) githubTestServer, githubURL = test.GetTestHTTPServer(t) githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-main.json", response) + test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnMain, response) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-v1-and-v2.json", response) + test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnV1AndV2, response) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/2", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", response) + test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnV1AndV2Content, response) }).Methods("GET").Headers("accept", "application/octet-stream") pullService = getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL) err = pullService.pullReleases() require.NoError(t, err) - 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")) - 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")) + test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnMainContent, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz")) + test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnV1AndV2Content, pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz")) } diff --git a/internal/pull/pull_test/api/asset-some-codeql-version-on-main.bin b/internal/pull/pull_test/api/asset-some-codeql-version-on-main.bin deleted file mode 100644 index fe72b6d..0000000 --- a/internal/pull/pull_test/api/asset-some-codeql-version-on-main.bin +++ /dev/null @@ -1 +0,0 @@ -This isn't really a CodeQL bundle! diff --git a/internal/pull/pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin b/internal/pull/pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin deleted file mode 100644 index e4cf4ac..0000000 --- a/internal/pull/pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin +++ /dev/null @@ -1 +0,0 @@ -This isn't a CodeQL bundle either, but it's a different not-bundle. diff --git a/internal/pull/pull_test/api/release-some-codeql-version-on-main.json b/internal/pull/pull_test/api/release-some-codeql-version-on-main.json deleted file mode 100644 index 2de3c34..0000000 --- a/internal/pull/pull_test/api/release-some-codeql-version-on-main.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "tag_name": "some-codeql-version-on-main", - "name": "some-codeql-version-on-main", - "assets": [ - { - "id": 1, - "name": "codeql-bundle.tar.gz", - "size": 35 - } - ] -} diff --git a/internal/pull/pull_test/api/release-some-codeql-version-on-v1-and-v2.json b/internal/pull/pull_test/api/release-some-codeql-version-on-v1-and-v2.json deleted file mode 100644 index 09256bf..0000000 --- a/internal/pull/pull_test/api/release-some-codeql-version-on-v1-and-v2.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "tag_name": "some-codeql-version-on-v1-and-v2", - "name": "some-codeql-version-on-v1-and-v2", - "assets": [ - { - "id": 2, - "name": "codeql-bundle.tar.gz", - "size": 68 - } - ] -} diff --git a/internal/push/push_test.go b/internal/push/push_test.go index c14505e..aecc753 100644 --- a/internal/push/push_test.go +++ b/internal/push/push_test.go @@ -44,13 +44,13 @@ func TestCreateRepositoryWhenUserIsOwner(t *testing.T) { githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t) pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL) githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-owner.json", response) + test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) { response.WriteHeader(http.StatusNotFound) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/user/repos", func(response http.ResponseWriter, request *http.Request) { - response.Write([]byte("{}")) + test.ServeHTTPResponseFromObject(t, github.Repository{}, response) }).Methods("POST") _, err := pushService.createRepository() require.NoError(t, err) @@ -61,13 +61,13 @@ func TestUpdateRepositoryWhenUserIsOwner(t *testing.T) { githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t) pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL) githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-owner.json", response) + test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) { test.ServeHTTPResponseFromObject(t, github.Repository{Homepage: github.String(repositoryHomepage)}, response) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) { - response.Write([]byte("{}")) + test.ServeHTTPResponseFromObject(t, github.Repository{}, response) }).Methods("PATCH") _, err := pushService.createRepository() require.NoError(t, err) @@ -78,7 +78,7 @@ func TestUpdateRepositoryWhenUserIsOwnerForced(t *testing.T) { githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t) pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL) githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-owner.json", response) + test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) { test.ServeHTTPResponseFromObject(t, github.Repository{}, response) @@ -99,24 +99,24 @@ func TestCreateOrganizationAndRepositoryWhenOrganizationIsOwner(t *testing.T) { pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL) organizationCreated := false githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) { - test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-not-owner.json", response) + test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("user")}, response) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/orgs/destination-repository-owner", func(response http.ResponseWriter, request *http.Request) { if organizationCreated { - response.Write([]byte("{}")) + test.ServeHTTPResponseFromObject(t, github.Organization{}, response) } else { response.WriteHeader(http.StatusNotFound) } }).Methods("GET") githubTestServer.HandleFunc("/api/v3/admin/organizations", func(response http.ResponseWriter, request *http.Request) { - response.Write([]byte("{}")) + test.ServeHTTPResponseFromObject(t, github.Organization{}, response) organizationCreated = true }).Methods("POST") githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) { response.WriteHeader(http.StatusNotFound) }).Methods("GET") githubTestServer.HandleFunc("/api/v3/orgs/destination-repository-owner/repos", func(response http.ResponseWriter, request *http.Request) { - response.Write([]byte("{}")) + test.ServeHTTPResponseFromObject(t, github.Repository{}, response) }).Methods("POST") _, err := pushService.createRepository() require.NoError(t, err) diff --git a/internal/push/push_test/api/user-is-not-owner.json b/internal/push/push_test/api/user-is-not-owner.json deleted file mode 100644 index 32f1cf0..0000000 --- a/internal/push/push_test/api/user-is-not-owner.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "login": "user" -} diff --git a/internal/push/push_test/api/user-is-owner.json b/internal/push/push_test/api/user-is-owner.json deleted file mode 100644 index f408fb6..0000000 --- a/internal/push/push_test/api/user-is-owner.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "login": "destination-repository-owner" -} diff --git a/test/test.go b/test/test.go index b886ebc..cb3fbdf 100644 --- a/test/test.go +++ b/test/test.go @@ -36,11 +36,8 @@ func GetTestHTTPServer(t *testing.T) (*mux.Router, string) { return mux, server.URL } -func ServeHTTPResponseFromFile(t *testing.T, statusCode int, path string, response http.ResponseWriter) { - data, err := ioutil.ReadFile(path) - require.NoError(t, err) - response.WriteHeader(statusCode) - _, err = response.Write(data) +func ServeHTTPResponseFromString(t *testing.T, content string, response http.ResponseWriter) { + _, err := response.Write([]byte(content)) require.NoError(t, err) } @@ -51,12 +48,10 @@ func ServeHTTPResponseFromObject(t *testing.T, object interface{}, response http require.NoError(t, err) } -func RequireFilesAreEqual(t *testing.T, expectedPath string, actualPath string) { - expectedData, err := ioutil.ReadFile(expectedPath) - require.NoError(t, err) +func RequireFileHasContent(t *testing.T, expectedContent string, actualPath string) { actualData, err := ioutil.ReadFile(actualPath) require.NoError(t, err) - require.Equal(t, expectedData, actualData) + require.Equal(t, []byte(expectedContent), actualData) } func CheckExpectedReferencesInRepository(t *testing.T, repositoryPath string, expectedReferences []string) {