-
Notifications
You must be signed in to change notification settings - Fork 6
/
gozip_test.go
78 lines (62 loc) · 1.59 KB
/
gozip_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gozip
import (
"io/ioutil"
"os"
"path"
"testing"
"time"
)
func TestZip(t *testing.T) {
zippath := "test.zip"
ioutil.WriteFile(zippath, []byte("<possibly an exefile>"), 0644)
os.MkdirAll("files/emptydir", 0755)
ioutil.WriteFile("hello.txt", []byte("Hello World"), 0777)
ioutil.WriteFile("files/hello.tpl", []byte("<h1>Hello World</h1>"), 0644)
testfileheader := "hello.txt"
info, err := os.Stat(testfileheader)
if err != nil {
t.Error(err)
}
actualperms := info.Mode().Perm()
filetime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
err = os.Chtimes(testfileheader, filetime, filetime)
if err != nil {
t.Error(err)
}
Zip(zippath, []string{"files", "hello.txt"})
if !IsZip(zippath) {
t.Error("zip test failed")
}
os.RemoveAll("files")
os.Remove("hello.txt")
list, err := UnzipList(zippath)
if err != nil || len(list) != 4 {
t.Error("unzip list failed")
}
if err := Unzip(zippath, "extract"); err != nil {
t.Error("unzip failed")
}
if _, err := os.Stat("extract/files/hello.tpl"); os.IsNotExist(err) {
t.Error("unzip didn't work")
}
if _, err := os.Stat("extract/files/emptydir"); os.IsNotExist(err) {
t.Error("unzip didn't create empty dir")
}
info, err = os.Stat(path.Join("extract", testfileheader))
if err != nil {
t.Error(err)
t.FailNow()
}
if info.Mode().Perm() != actualperms {
t.Error("unzip didn't set file perms")
}
if !info.ModTime().Equal(filetime) {
t.Error("unzip didn't set file modtime")
}
if !t.Failed() {
os.Remove("hello.txt")
os.Remove("test.zip")
os.RemoveAll("files")
os.RemoveAll("extract")
}
}