github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/sharedaction/resource_test.go (about) 1 package sharedaction_test 2 3 import ( 4 "archive/zip" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 9 "code.cloudfoundry.org/ykk" 10 . "github.com/liamawhite/cli-with-i18n/actor/sharedaction" 11 "github.com/liamawhite/cli-with-i18n/actor/sharedaction/sharedactionfakes" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Resource Actions", func() { 17 var ( 18 fakeConfig *sharedactionfakes.FakeConfig 19 actor *Actor 20 srcDir string 21 ) 22 23 BeforeEach(func() { 24 fakeConfig = new(sharedactionfakes.FakeConfig) 25 actor = NewActor(fakeConfig) 26 27 var err error 28 srcDir, err = ioutil.TempDir("", "resource-actions-test") 29 Expect(err).ToNot(HaveOccurred()) 30 31 subDir := filepath.Join(srcDir, "level1", "level2") 32 err = os.MkdirAll(subDir, 0777) 33 Expect(err).ToNot(HaveOccurred()) 34 35 err = ioutil.WriteFile(filepath.Join(subDir, "tmpFile1"), []byte("why hello"), 0600) 36 Expect(err).ToNot(HaveOccurred()) 37 38 err = ioutil.WriteFile(filepath.Join(srcDir, "tmpFile2"), []byte("Hello, Binky"), 0600) 39 Expect(err).ToNot(HaveOccurred()) 40 41 err = ioutil.WriteFile(filepath.Join(srcDir, "tmpFile3"), []byte("Bananarama"), 0600) 42 Expect(err).ToNot(HaveOccurred()) 43 }) 44 45 AfterEach(func() { 46 Expect(os.RemoveAll(srcDir)).ToNot(HaveOccurred()) 47 }) 48 49 Describe("GatherArchiveResources", func() { 50 // tests are under resource_unix_test.go and resource_windows_test.go 51 }) 52 53 Describe("GatherDirectoryResources", func() { 54 // tests are under resource_unix_test.go and resource_windows_test.go 55 }) 56 57 Describe("ZipArchiveResources", func() { 58 var ( 59 archive string 60 resultZip string 61 resources []Resource 62 executeErr error 63 ) 64 65 BeforeEach(func() { 66 tmpfile, err := ioutil.TempFile("", "zip-archive-resources") 67 Expect(err).ToNot(HaveOccurred()) 68 defer tmpfile.Close() 69 archive = tmpfile.Name() 70 71 err = zipit(srcDir, archive, "") 72 Expect(err).ToNot(HaveOccurred()) 73 }) 74 75 JustBeforeEach(func() { 76 resultZip, executeErr = actor.ZipArchiveResources(archive, resources) 77 }) 78 79 AfterEach(func() { 80 Expect(os.RemoveAll(archive)).ToNot(HaveOccurred()) 81 Expect(os.RemoveAll(resultZip)).ToNot(HaveOccurred()) 82 }) 83 84 Context("when the files have not been changed since scanning them", func() { 85 BeforeEach(func() { 86 resources = []Resource{ 87 {Filename: "/"}, 88 {Filename: "/level1/"}, 89 {Filename: "/level1/level2/"}, 90 {Filename: "/level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4"}, 91 {Filename: "/tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95"}, 92 // Explicitly skipping /tmpFile3 93 } 94 }) 95 96 It("zips the file and returns a populated resources list", func() { 97 Expect(executeErr).ToNot(HaveOccurred()) 98 99 Expect(resultZip).ToNot(BeEmpty()) 100 zipFile, err := os.Open(resultZip) 101 Expect(err).ToNot(HaveOccurred()) 102 defer zipFile.Close() 103 104 zipInfo, err := zipFile.Stat() 105 Expect(err).ToNot(HaveOccurred()) 106 107 reader, err := ykk.NewReader(zipFile, zipInfo.Size()) 108 Expect(err).ToNot(HaveOccurred()) 109 110 Expect(reader.File).To(HaveLen(5)) 111 Expect(reader.File[0].Name).To(Equal("/")) 112 Expect(reader.File[1].Name).To(Equal("/level1/")) 113 Expect(reader.File[2].Name).To(Equal("/level1/level2/")) 114 Expect(reader.File[3].Name).To(Equal("/level1/level2/tmpFile1")) 115 Expect(reader.File[4].Name).To(Equal("/tmpFile2")) 116 117 expectFileContentsToEqual(reader.File[3], "why hello") 118 expectFileContentsToEqual(reader.File[4], "Hello, Binky") 119 120 for _, file := range reader.File { 121 Expect(file.Method).To(Equal(zip.Deflate)) 122 } 123 }) 124 }) 125 126 Context("when the files have changed since the scanning", func() { 127 BeforeEach(func() { 128 resources = []Resource{ 129 {Filename: "/"}, 130 {Filename: "/level1/"}, 131 {Filename: "/level1/level2/"}, 132 {Filename: "/level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4"}, 133 {Filename: "/tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95"}, 134 {Filename: "/tmpFile3", SHA1: "i dunno, 7?"}, 135 } 136 }) 137 138 It("returns an FileChangedError", func() { 139 Expect(executeErr).To(Equal(FileChangedError{Filename: "/tmpFile3"})) 140 }) 141 }) 142 }) 143 144 Describe("ZipDirectoryResources", func() { 145 var ( 146 resultZip string 147 resources []Resource 148 executeErr error 149 ) 150 151 JustBeforeEach(func() { 152 resultZip, executeErr = actor.ZipDirectoryResources(srcDir, resources) 153 }) 154 155 AfterEach(func() { 156 Expect(os.RemoveAll(resultZip)).ToNot(HaveOccurred()) 157 }) 158 159 Context("when the files have not been changed since scanning them", func() { 160 BeforeEach(func() { 161 resources = []Resource{ 162 {Filename: "level1"}, 163 {Filename: "level1/level2"}, 164 {Filename: "level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4"}, 165 {Filename: "tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95"}, 166 {Filename: "tmpFile3", SHA1: "f4c9ca85f3e084ffad3abbdabbd2a890c034c879"}, 167 } 168 }) 169 170 It("zips the file and returns a populated resources list", func() { 171 Expect(executeErr).ToNot(HaveOccurred()) 172 173 Expect(resultZip).ToNot(BeEmpty()) 174 zipFile, err := os.Open(resultZip) 175 Expect(err).ToNot(HaveOccurred()) 176 defer zipFile.Close() 177 178 zipInfo, err := zipFile.Stat() 179 Expect(err).ToNot(HaveOccurred()) 180 181 reader, err := ykk.NewReader(zipFile, zipInfo.Size()) 182 Expect(err).ToNot(HaveOccurred()) 183 184 Expect(reader.File).To(HaveLen(5)) 185 Expect(reader.File[0].Name).To(Equal("level1/")) 186 Expect(reader.File[1].Name).To(Equal("level1/level2/")) 187 Expect(reader.File[2].Name).To(Equal("level1/level2/tmpFile1")) 188 Expect(reader.File[3].Name).To(Equal("tmpFile2")) 189 Expect(reader.File[4].Name).To(Equal("tmpFile3")) 190 191 expectFileContentsToEqual(reader.File[2], "why hello") 192 expectFileContentsToEqual(reader.File[3], "Hello, Binky") 193 expectFileContentsToEqual(reader.File[4], "Bananarama") 194 195 for _, file := range reader.File { 196 Expect(file.Method).To(Equal(zip.Deflate)) 197 } 198 }) 199 }) 200 201 Context("when the files have changed since the scanning", func() { 202 BeforeEach(func() { 203 resources = []Resource{ 204 {Filename: "level1"}, 205 {Filename: "level1/level2"}, 206 {Filename: "level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4"}, 207 {Filename: "tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95"}, 208 {Filename: "tmpFile3", SHA1: "i dunno, 7?"}, 209 } 210 }) 211 212 It("returns an FileChangedError", func() { 213 Expect(executeErr).To(Equal(FileChangedError{Filename: filepath.Join(srcDir, "tmpFile3")})) 214 }) 215 }) 216 }) 217 }) 218 219 func expectFileContentsToEqual(file *zip.File, expectedContents string) { 220 reader, err := file.Open() 221 Expect(err).ToNot(HaveOccurred()) 222 defer reader.Close() 223 224 body, err := ioutil.ReadAll(reader) 225 Expect(err).ToNot(HaveOccurred()) 226 227 Expect(string(body)).To(Equal(expectedContents)) 228 }