From fd92752d84e45aaa96a132a30cbbb6b26cd6e873 Mon Sep 17 00:00:00 2001 From: Kris Hicks Date: Wed, 28 Jun 2017 16:56:27 -0700 Subject: [PATCH] Refactor some in_command tests and code This will make adding the unpack feature easier to do. --- in/in_command.go | 97 ++++++++++++++----------------------------- in/in_command_test.go | 20 ++++----- 2 files changed, 42 insertions(+), 75 deletions(-) diff --git a/in/in_command.go b/in/in_command.go index df8ec203..f5c28d80 100644 --- a/in/in_command.go +++ b/in/in_command.go @@ -45,32 +45,35 @@ func (command *InCommand) Run(destinationDir string, request InRequest) (InRespo return InResponse{}, errors.New(message) } - err := command.createDirectory(destinationDir) + err := os.MkdirAll(destinationDir, 0755) if err != nil { return InResponse{}, err } - if request.Source.Regexp != "" { - return command.inByRegex(destinationDir, request) - } else { - return command.inByVersionedFile(destinationDir, request) - } -} + var remotePath string + var versionNumber string + var versionID string -func (command *InCommand) inByRegex(destinationDir string, request InRequest) (InResponse, error) { - if request.Version.Path == "" { - return InResponse{}, ErrMissingPath - } + if request.Source.Regexp != "" { + if request.Version.Path == "" { + return InResponse{}, ErrMissingPath + } - remotePath := request.Version.Path + remotePath = request.Version.Path - extraction, ok := versions.Extract(remotePath, request.Source.Regexp) - if !ok { - return InResponse{}, fmt.Errorf("regex does not match provided version: %#v", request.Version) + extraction, ok := versions.Extract(remotePath, request.Source.Regexp) + if !ok { + return InResponse{}, fmt.Errorf("regex does not match provided version: %#v", request.Version) + } + versionNumber = extraction.VersionNumber + } else { + remotePath = request.Source.VersionedFile + versionNumber = request.Version.VersionID + versionID = request.Version.VersionID } - err := command.writeVersionFile(extraction.VersionNumber, destinationDir) + err = command.writeVersionFile(versionNumber, destinationDir) if err != nil { return InResponse{}, err } @@ -78,73 +81,37 @@ func (command *InCommand) inByRegex(destinationDir string, request InRequest) (I err = command.downloadFile( request.Source.Bucket, remotePath, - "", + versionID, destinationDir, path.Base(remotePath), ) - if err != nil { - return InResponse{}, err - } - url := command.urlProvider.GetURL(request, remotePath) - err = command.writeURLFile( - destinationDir, - url, - ) if err != nil { return InResponse{}, err } - return InResponse{ - Version: s3resource.Version{ - Path: remotePath, - }, - Metadata: command.metadata(remotePath, request.Source.Private, url), - }, nil -} - -func (command *InCommand) inByVersionedFile(destinationDir string, request InRequest) (InResponse, error) { - - err := command.writeVersionFile(request.Version.VersionID, destinationDir) - if err != nil { - return InResponse{}, err - } - - remotePath := request.Source.VersionedFile - - err = command.downloadFile( - request.Source.Bucket, - remotePath, - request.Version.VersionID, - destinationDir, - path.Base(remotePath), - ) - - if err != nil { + url := command.urlProvider.GetURL(request, remotePath) + if err = command.writeURLFile(destinationDir, url); err != nil { return InResponse{}, err } - url := command.urlProvider.GetURL(request, remotePath) - err = command.writeURLFile( - destinationDir, - url, - ) + metadata := command.metadata(remotePath, request.Source.Private, url) - if err != nil { - return InResponse{}, err + if versionID == "" { + return InResponse{ + Version: s3resource.Version{ + Path: remotePath, + }, + Metadata: metadata, + }, nil } return InResponse{ Version: s3resource.Version{ - VersionID: request.Version.VersionID, + VersionID: versionID, }, - Metadata: command.metadata(remotePath, request.Source.Private, url), + Metadata: metadata, }, nil - -} - -func (command *InCommand) createDirectory(destDir string) error { - return os.MkdirAll(destDir, 0755) } func (command *InCommand) writeURLFile(destDir string, url string) error { diff --git a/in/in_command_test.go b/in/in_command_test.go index ac7db481..69196845 100644 --- a/in/in_command_test.go +++ b/in/in_command_test.go @@ -34,10 +34,10 @@ var _ = Describe("In Command", func() { request = InRequest{ Source: s3resource.Source{ Bucket: "bucket-name", - Regexp: "files/a-file-(.*).tgz", + Regexp: "files/a-file-(.*)", }, Version: s3resource.Version{ - Path: "files/a-file-1.3.tgz", + Path: "files/a-file-1.3", }, } @@ -74,7 +74,7 @@ var _ = Describe("In Command", func() { Context("when there is an existing version in the request", func() { BeforeEach(func() { - request.Version.Path = "files/a-file-1.3.tgz" + request.Version.Path = "files/a-file-1.3" }) It("downloads the existing version of the file", func() { @@ -85,9 +85,9 @@ var _ = Describe("In Command", func() { bucketName, remotePath, versionID, localPath := s3client.DownloadFileArgsForCall(0) Ω(bucketName).Should(Equal("bucket-name")) - Ω(remotePath).Should(Equal("files/a-file-1.3.tgz")) + Ω(remotePath).Should(Equal("files/a-file-1.3")) Ω(versionID).Should(BeEmpty()) - Ω(localPath).Should(Equal(filepath.Join(destDir, "a-file-1.3.tgz"))) + Ω(localPath).Should(Equal(filepath.Join(destDir, "a-file-1.3"))) }) It("creates a 'url' file that contains the URL", func() { @@ -104,7 +104,7 @@ var _ = Describe("In Command", func() { bucketName, remotePath, private, versionID := s3client.URLArgsForCall(0) Ω(bucketName).Should(Equal("bucket-name")) - Ω(remotePath).Should(Equal("files/a-file-1.3.tgz")) + Ω(remotePath).Should(Equal("files/a-file-1.3")) Ω(private).Should(Equal(false)) Ω(versionID).Should(BeEmpty()) }) @@ -129,7 +129,7 @@ var _ = Describe("In Command", func() { Ω(s3client.URLCallCount()).Should(Equal(1)) bucketName, remotePath, private, versionID := s3client.URLArgsForCall(0) Ω(bucketName).Should(Equal("bucket-name")) - Ω(remotePath).Should(Equal("files/a-file-1.3.tgz")) + Ω(remotePath).Should(Equal("files/a-file-1.3")) Ω(private).Should(Equal(true)) Ω(versionID).Should(BeEmpty()) }) @@ -153,7 +153,7 @@ var _ = Describe("In Command", func() { response, err := command.Run(destDir, request) Ω(err).ShouldNot(HaveOccurred()) - Ω(response.Version.Path).Should(Equal("files/a-file-1.3.tgz")) + Ω(response.Version.Path).Should(Equal("files/a-file-1.3")) }) It("has metadata about the file", func() { @@ -161,7 +161,7 @@ var _ = Describe("In Command", func() { Ω(err).ShouldNot(HaveOccurred()) Ω(response.Metadata[0].Name).Should(Equal("filename")) - Ω(response.Metadata[0].Value).Should(Equal("a-file-1.3.tgz")) + Ω(response.Metadata[0].Value).Should(Equal("a-file-1.3")) Ω(response.Metadata[1].Name).Should(Equal("url")) Ω(response.Metadata[1].Value).Should(Equal("http://google.com")) @@ -192,7 +192,7 @@ var _ = Describe("In Command", func() { _, err := command.Run(destDir, request) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("regex does not match provided version")) - Expect(err.Error()).To(ContainSubstring("files/a-file-1.3.tgz")) + Expect(err.Error()).To(ContainSubstring("files/a-file-1.3")) }) }) })