From 95ab1bda22960dab31b3a7216bd5ee22917f9f6c Mon Sep 17 00:00:00 2001 From: Chris Sun Date: Wed, 21 Oct 2015 13:59:42 -0700 Subject: [PATCH] Backfill tests for downloading source tarball/zip [#104040604] Signed-off-by: Evan Short --- in_command_test.go | 127 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/in_command_test.go b/in_command_test.go index a4b1128..e6d754c 100644 --- a/in_command_test.go +++ b/in_command_test.go @@ -4,12 +4,14 @@ import ( "bytes" "errors" "io/ioutil" + "net/url" "os" "path" "path/filepath" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/onsi/gomega/ghttp" "github.com/zachgersh/go-github/github" @@ -21,6 +23,7 @@ var _ = Describe("In Command", func() { var ( command *resource.InCommand githubClient *fakes.FakeGitHub + githubServer *ghttp.Server inRequest resource.InRequest @@ -35,6 +38,7 @@ var _ = Describe("In Command", func() { var err error githubClient = &fakes.FakeGitHub{} + githubServer = ghttp.NewServer() command = resource.NewInCommand(githubClient, ioutil.Discard) tmpDir, err = ioutil.TempDir("", "github-release") @@ -111,6 +115,7 @@ var _ = Describe("In Command", func() { }) It("downloads only the files that match the globs", func() { + Expect(githubClient.DownloadReleaseAssetCallCount()).To(Equal(2)) Ω(githubClient.DownloadReleaseAssetArgsForCall(0)).Should(Equal(buildAsset(0, "example.txt"))) Ω(githubClient.DownloadReleaseAssetArgsForCall(1)).Should(Equal(buildAsset(1, "example.rtf"))) }) @@ -126,6 +131,128 @@ var _ = Describe("In Command", func() { }) }) + Context("when valid asset filename globs are given and include_source_tarball is true", func() { + BeforeEach(func() { + inRequest.Params = resource.InParams{ + Globs: []string{"*.txt", "*.rtf"}, + } + inRequest.Params.IncludeSourceTarball = true + + tarballUrl, _ := url.Parse(githubServer.URL()) + tarballUrl.Path = "/gimme-a-tarball/" + githubClient.GetTarballLinkReturns(tarballUrl, nil) + githubServer.AppendHandlers( + ghttp.CombineHandlers( + ghttp.VerifyRequest("GET", tarballUrl.Path), + ghttp.RespondWith(200, "source-tar-file-contents"), + ), + ) + + inResponse, inErr = command.Run(destDir, inRequest) + }) + + It("succeeds", func() { + Ω(inErr).ShouldNot(HaveOccurred()) + }) + + It("downloads only the files that match the globs", func() { + Expect(githubClient.DownloadReleaseAssetCallCount()).To(Equal(2)) + Ω(githubClient.DownloadReleaseAssetArgsForCall(0)).Should(Equal(buildAsset(0, "example.txt"))) + Ω(githubClient.DownloadReleaseAssetArgsForCall(1)).Should(Equal(buildAsset(1, "example.rtf"))) + }) + + It("downloads the source tarball", func() { + Expect(githubServer.ReceivedRequests()).To(HaveLen(1)) + }) + + It("saves the source tarball in the destination directory", func() { + fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "source.tar.gz")) + fContents := string(fileContents) + Expect(err).NotTo(HaveOccurred()) + Expect(fContents).To(Equal("source-tar-file-contents")) + }) + }) + + Context("when include_source_tarball is true and no globs are specified", func() { + BeforeEach(func() { + inRequest.Params = resource.InParams{} + inRequest.Params.IncludeSourceTarball = true + + tarballUrl, _ := url.Parse(githubServer.URL()) + tarballUrl.Path = "/gimme-a-tarball/" + githubClient.GetTarballLinkReturns(tarballUrl, nil) + githubServer.AppendHandlers( + ghttp.CombineHandlers( + ghttp.VerifyRequest("GET", tarballUrl.Path), + ghttp.RespondWith(200, "source-tar-file-contents"), + ), + ) + + inResponse, inErr = command.Run(destDir, inRequest) + }) + + It("succeeds", func() { + Ω(inErr).ShouldNot(HaveOccurred()) + }) + + It("downloads all the assets", func() { + Expect(githubClient.DownloadReleaseAssetCallCount()).To(Equal(3)) + Expect(githubClient.DownloadReleaseAssetArgsForCall(0)).To(Equal(buildAsset(0, "example.txt"))) + Expect(githubClient.DownloadReleaseAssetArgsForCall(1)).To(Equal(buildAsset(1, "example.rtf"))) + Expect(githubClient.DownloadReleaseAssetArgsForCall(2)).To(Equal(buildAsset(2, "example.wtf"))) + }) + + It("downloads the source tarball", func() { + Expect(githubServer.ReceivedRequests()).To(HaveLen(1)) + }) + + It("saves the source tarball in the destination directory", func() { + fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "source.tar.gz")) + Expect(err).NotTo(HaveOccurred()) + Expect(string(fileContents)).To(Equal("source-tar-file-contents")) + }) + }) + + Context("when include_source_zip is true and no globs are specified", func() { + BeforeEach(func() { + inRequest.Params = resource.InParams{} + inRequest.Params.IncludeSourceZip = true + + tarballUrl, _ := url.Parse(githubServer.URL()) + tarballUrl.Path = "/gimme-a-zip/" + githubClient.GetZipballLinkReturns(tarballUrl, nil) + githubServer.AppendHandlers( + ghttp.CombineHandlers( + ghttp.VerifyRequest("GET", tarballUrl.Path), + ghttp.RespondWith(200, "source-zip-file-contents"), + ), + ) + + inResponse, inErr = command.Run(destDir, inRequest) + }) + + It("succeeds", func() { + Ω(inErr).ShouldNot(HaveOccurred()) + }) + + It("downloads all the assets", func() { + Expect(githubClient.DownloadReleaseAssetCallCount()).To(Equal(3)) + Expect(githubClient.DownloadReleaseAssetArgsForCall(0)).To(Equal(buildAsset(0, "example.txt"))) + Expect(githubClient.DownloadReleaseAssetArgsForCall(1)).To(Equal(buildAsset(1, "example.rtf"))) + Expect(githubClient.DownloadReleaseAssetArgsForCall(2)).To(Equal(buildAsset(2, "example.wtf"))) + }) + + It("downloads the source zip", func() { + Expect(githubServer.ReceivedRequests()).To(HaveLen(1)) + }) + + It("saves the source zip in the destination directory", func() { + fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "source.zip")) + Expect(err).NotTo(HaveOccurred()) + Expect(string(fileContents)).To(Equal("source-zip-file-contents")) + }) + }) + Context("when an invalid asset filename glob is given", func() { BeforeEach(func() { inRequest.Params = resource.InParams{