github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/cae/zip/zip_test.go (about) 1 // Copyright 2013 Unknwon 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package zip 16 17 import ( 18 "fmt" 19 "os" 20 "path" 21 "strings" 22 "testing" 23 24 . "github.com/insionng/yougam/libraries/smartystreets/goconvey/convey" 25 "github.com/insionng/yougam/libraries/Unknwon/cae" 26 "github.com/insionng/yougam/libraries/Unknwon/com" 27 ) 28 29 func TestCreate(t *testing.T) { 30 Convey("Create a zip file", t, func() { 31 _, err := Create(path.Join(os.TempDir(), "testdata/TestCreate.zip")) 32 So(err, ShouldBeNil) 33 }) 34 } 35 36 func TestOpen(t *testing.T) { 37 Convey("Open a zip file normally with read-only flag", t, func() { 38 z, err := Open("testdata/test.zip") 39 So(err, ShouldBeNil) 40 So(z.FileName, ShouldEqual, "testdata/test.zip") 41 So(z.Comment, ShouldEqual, "This is the comment for test.zip") 42 So(z.NumFiles, ShouldEqual, 5) 43 So(z.Flag, ShouldEqual, os.O_RDONLY) 44 So(z.Permission, ShouldEqual, 0) 45 }) 46 47 Convey("Open a zip file that does not exist", t, func() { 48 _, err := Open("testdata/404.zip") 49 So(err, ShouldNotBeNil) 50 }) 51 52 Convey("Open a file that is not a zip file", t, func() { 53 _, err := Open("testdata/readme.notzip") 54 So(err, ShouldNotBeNil) 55 }) 56 } 57 58 func TestList(t *testing.T) { 59 Convey("Open a zip file and get list of file/dir name", t, func() { 60 z, err := Open("testdata/test.zip") 61 So(err, ShouldBeNil) 62 63 Convey("List without prefix", func() { 64 So(strings.Join(z.List(), " "), ShouldEqual, 65 "dir/ dir/bar dir/empty/ hello readonly") 66 }) 67 68 Convey("List with prefix", func() { 69 So(strings.Join(z.List("h"), " "), ShouldEqual, "hello") 70 }) 71 }) 72 } 73 74 func TestAddEmptyDir(t *testing.T) { 75 Convey("Open a zip file and add empty dirs", t, func() { 76 z, err := Create(path.Join(os.TempDir(), "testdata/TestAddEmptyDir.zip")) 77 So(err, ShouldBeNil) 78 79 Convey("Add dir that does not exist and then add again", func() { 80 So(z.AddEmptyDir("level1"), ShouldBeTrue) 81 So(!z.AddEmptyDir("level1"), ShouldBeTrue) 82 }) 83 84 Convey("Add multiple-level dir", func() { 85 z.AddEmptyDir("level1/level2/level3/level4") 86 So(strings.Join(z.List(), " "), ShouldEqual, 87 "level1/ level1/level2/ level1/level2/level3/ level1/level2/level3/level4/") 88 }) 89 }) 90 } 91 92 func TestAddDir(t *testing.T) { 93 Convey("Open a zip file and add dir with files", t, func() { 94 z, err := Create(path.Join(os.TempDir(), "testdata/TestAddDir.zip")) 95 So(err, ShouldBeNil) 96 97 Convey("Add a dir that does exist", func() { 98 So(z.AddDir("testdata/testdir", "testdata/testdir"), ShouldBeNil) 99 So(strings.Join(z.List(), " "), ShouldEqual, 100 "testdata/ testdata/testdir/ testdata/testdir/gophercolor16x16.png "+ 101 "testdata/testdir/level1/ testdata/testdir/level1/README.txt") 102 }) 103 104 Convey("Add a dir that does not exist", func() { 105 So(z.AddDir("testdata/testdir", "testdata/testdir404"), ShouldNotBeNil) 106 }) 107 108 Convey("Add a dir that is a file", func() { 109 So(z.AddDir("testdata/testdir", "testdata/README.txt"), ShouldNotBeNil) 110 }) 111 }) 112 } 113 114 func TestAddFile(t *testing.T) { 115 Convey("Open a zip file and add files", t, func() { 116 z, err := Create(path.Join(os.TempDir(), "testdata/TestAddFile.zip")) 117 So(err, ShouldBeNil) 118 119 Convey("Add a file that does exist", func() { 120 So(z.AddFile("testdata/README.txt", "testdata/gophercolor16x16.png"), ShouldBeNil) 121 So(strings.Join(z.List(), " "), ShouldEqual, "testdata/ testdata/README.txt") 122 }) 123 124 Convey("Add a file that does not exist", func() { 125 So(z.AddFile("testdata/README.txt", "testdata/README_404.txt"), ShouldNotBeNil) 126 }) 127 128 Convey("Add a file that is exist in list", func() { 129 So(z.AddFile("testdata/README.txt", "testdata/README.txt"), ShouldBeNil) 130 So(strings.Join(z.List(), " "), ShouldEqual, 131 "testdata/ testdata/README.txt") 132 }) 133 }) 134 } 135 136 func TestExtractTo(t *testing.T) { 137 Convey("Extract a zip file to given path", t, func() { 138 z, err := Open("testdata/test.zip") 139 So(err, ShouldBeNil) 140 141 Convey("Extract the zip file without entries", func() { 142 os.RemoveAll(path.Join(os.TempDir(), "testdata/test1")) 143 So(z.ExtractTo(path.Join(os.TempDir(), "testdata/test1")), ShouldBeNil) 144 list, err := com.StatDir(path.Join(os.TempDir(), "testdata/test1"), true) 145 So(err, ShouldBeNil) 146 So(com.CompareSliceStrU(list, 147 strings.Split("dir/ dir/bar dir/empty/ hello readonly", " ")), ShouldBeTrue) 148 }) 149 150 Convey("Extract the zip file with entries", func() { 151 os.RemoveAll(path.Join(os.TempDir(), "testdata/test2")) 152 So(z.ExtractTo( 153 path.Join(os.TempDir(), "testdata/test2"), 154 "dir/", "dir/bar", "readonly"), ShouldBeNil) 155 list, err := com.StatDir(path.Join(os.TempDir(), "testdata/test2"), true) 156 So(err, ShouldBeNil) 157 So(com.CompareSliceStrU( 158 list, strings.Split("dir/ dir/bar readonly", " ")), ShouldBeTrue) 159 }) 160 }) 161 } 162 163 func TestFlush(t *testing.T) { 164 Convey("Do some operations and flush to file system", t, func() { 165 z, err := Create(path.Join(os.TempDir(), "testdata/TestFlush.zip")) 166 So(err, ShouldBeNil) 167 168 z.AddEmptyDir("level1/level2/level3/level4") 169 So(z.AddFile("testdata/README.txt", "testdata/README.txt"), ShouldBeNil) 170 171 // Add symbolic links. 172 So(z.AddFile("testdata/test.lnk", "testdata/test.lnk"), ShouldBeNil) 173 So(z.AddFile("testdata/testdir.lnk", "testdata/testdir.lnk"), ShouldBeNil) 174 175 fmt.Println("Flushing to local file system...") 176 // So(z.Flush(), ShouldBeNil) 177 }) 178 179 Convey("Do some operation and flush to io.Writer", t, func() { 180 f, err := os.Create(path.Join(os.TempDir(), "testdata/TestFlush2.zip")) 181 So(err, ShouldBeNil) 182 So(f, ShouldNotBeNil) 183 184 z := New(f) 185 z.AddEmptyDir("level1/level2/level3/level4") 186 So(z.AddFile("testdata/README.txt", "testdata/README.txt"), ShouldBeNil) 187 188 fmt.Println("Flushing to local io.Writer...") 189 So(z.Flush(), ShouldBeNil) 190 So(z.Flush(), ShouldBeNil) // In case first time is lucky. 191 }) 192 } 193 194 func TestPackTo(t *testing.T) { 195 Convey("Pack a dir or file to zip file", t, func() { 196 Convey("Pack a dir that does exist and includir root dir", func() { 197 So(PackTo("testdata/testdir", 198 path.Join(os.TempDir(), "testdata/testdir1.zip"), true), ShouldBeNil) 199 }) 200 201 Convey("Pack a dir that does exist and does not includir root dir", func() { 202 So(PackTo("testdata/testdir", 203 path.Join(os.TempDir(), "testdata/testdir2.zip")), ShouldBeNil) 204 }) 205 206 Convey("Pack a dir that does not exist and does not includir root dir", func() { 207 So(PackTo("testdata/testdir404", 208 path.Join(os.TempDir(), "testdata/testdir3.zip")), ShouldNotBeNil) 209 }) 210 211 Convey("Pack a file that does exist", func() { 212 So(PackTo("testdata/README.txt", 213 path.Join(os.TempDir(), "testdata/testdir4.zip")), ShouldBeNil) 214 }) 215 216 Convey("Pack a file that does not exist", func() { 217 So(PackTo("testdata/README404.txt", 218 path.Join(os.TempDir(), "testdata/testdir5.zip")), ShouldNotBeNil) 219 }) 220 }) 221 } 222 223 func TestClose(t *testing.T) { 224 Convey("Close the zip file currently operating", t, func() { 225 z, err := Open("testdata/test.zip") 226 So(err, ShouldBeNil) 227 So(z.Close(), ShouldBeNil) 228 }) 229 } 230 231 func TestDeleteIndex(t *testing.T) { 232 Convey("Delete an entry with given index", t, func() { 233 z, err := Create(path.Join(os.TempDir(), "testdata/TestDeleteIndex.zip")) 234 So(err, ShouldBeNil) 235 236 z.AddEmptyDir("level1/level2/level3/level4") 237 238 Convey("Delete an entry with valid index", func() { 239 So(z.DeleteIndex(3), ShouldBeNil) 240 So(strings.Join(z.List(), " "), ShouldEqual, 241 "level1/ level1/level2/ level1/level2/level3/") 242 }) 243 244 Convey("Delete an entry with invalid index", func() { 245 So(z.DeleteIndex(5), ShouldNotBeNil) 246 }) 247 248 Convey("Test after flush", func() { 249 So(z.Flush(), ShouldBeNil) 250 So(strings.Join(z.List(), " "), ShouldEqual, 251 "level1/ level1/level2/ level1/level2/level3/ level1/level2/level3/level4/") 252 }) 253 }) 254 } 255 256 func TestDeleteName(t *testing.T) { 257 Convey("Delete an entry with given name", t, func() { 258 z, err := Create(path.Join(os.TempDir(), "testdata/TestDeleteName.zip")) 259 So(err, ShouldBeNil) 260 261 z.AddEmptyDir("level1/level2/level3/level4") 262 263 Convey("Delete an entry with valid name", func() { 264 So(z.DeleteName("level1/level2/level3/level4/"), ShouldBeNil) 265 So(strings.Join(z.List(), " "), ShouldEqual, 266 "level1/ level1/level2/ level1/level2/level3/") 267 }) 268 269 Convey("Delete an entry with invalid name", func() { 270 So(z.DeleteName("level1/level2/level3/level"), ShouldNotBeNil) 271 }) 272 273 Convey("Test after flush", func() { 274 So(z.Flush(), ShouldBeNil) 275 So(strings.Join(z.List(), " "), ShouldEqual, 276 "level1/ level1/level2/ level1/level2/level3/ level1/level2/level3/level4/") 277 }) 278 }) 279 } 280 281 func TestCopy(t *testing.T) { 282 Convey("Copy file from A to B", t, func() { 283 Convey("Copy a file that does exist", func() { 284 tmpPath := path.Join(os.TempDir(), "testdata/README.txt") 285 So(cae.Copy(tmpPath, "testdata/README.txt"), ShouldBeNil) 286 So(cae.IsExist(tmpPath), ShouldBeTrue) 287 }) 288 289 Convey("Copy a file that does not exist", func() { 290 So(cae.Copy( 291 path.Join(os.TempDir(), "testdata/README.txt"), 292 "testdata/404.txt"), ShouldNotBeNil) 293 }) 294 }) 295 }