-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
zip_test.go
150 lines (124 loc) · 3.04 KB
/
zip_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Gulu - Golang common utilities for everyone.
// Copyright (c) 2019-present, b3log.org
//
// Gulu is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package gulu
import (
"os"
"path/filepath"
"testing"
)
var testdataDir = "testdata"
var zipDirPath = filepath.Join(testdataDir, "test_zip")
func TestCreate(t *testing.T) {
zipFile, err := Zip.Create(zipDirPath + ".zip")
if nil != err {
t.Error(err)
return
}
zipFile.AddDirectory(".", testdataDir, func(filename string) {
t.Logf("zipped %s", filename)
})
if nil != err {
t.Error(err)
return
}
err = zipFile.Close()
if nil != err {
t.Error(err)
return
}
err = Zip.Unzip(zipDirPath+".zip", zipDirPath, func(filename string) {
t.Logf("unzipped %s", filename)
})
if nil != err {
t.Error(err)
return
}
f1, _ := os.Stat(filepath.Join(testdataDir, "README.md"))
f1ModTime := f1.ModTime()
f2, _ := os.Stat(filepath.Join(zipDirPath, "README.md"))
f2ModTime := f2.ModTime()
if f1ModTime.Unix() != f2ModTime.Unix() {
t.Error("ModTime error")
return
}
}
func TestUnzip(t *testing.T) {
err := Zip.Unzip(zipDirPath+".zip", zipDirPath)
if nil != err {
t.Error(err)
return
}
}
func _TestEmptyDir(t *testing.T) {
dir1 := "/dir/subDir1"
dir2 := "/dir/subDir2"
err := os.MkdirAll(zipDirPath+dir1, os.ModeDir)
if nil != err {
t.Error(err)
return
}
err = os.MkdirAll(zipDirPath+dir2, os.ModeDir)
if nil != err {
t.Error(err)
return
}
f, err := os.Create(zipDirPath + dir2 + "/file")
if nil != err {
t.Error(err)
return
}
f.Close()
zipFile, err := Zip.Create(zipDirPath + "/dir.zip")
if nil != err {
t.Error(err)
return
}
zipFile.AddDirectory("dir", zipDirPath+"/dir")
if nil != err {
t.Error(err)
return
}
err = zipFile.Close()
if nil != err {
t.Error(err)
return
}
err = Zip.Unzip(zipDirPath+"/dir.zip", zipDirPath+"/unzipDir")
if nil != err {
t.Error(err)
return
}
if !File.IsExist(zipDirPath+"/unzipDir") || !File.IsDir(zipDirPath+"/unzipDir") {
t.Error("Unzip failed")
return
}
if !File.IsExist(zipDirPath+"/unzipDir"+dir1) || !File.IsDir(zipDirPath+"/unzipDir"+dir1) {
t.Error("Unzip failed")
return
}
if !File.IsExist(zipDirPath+"/unzipDir"+dir2) || !File.IsDir(zipDirPath+"/unzipDir"+dir2) {
t.Error("Unzip failed")
return
}
if !File.IsExist(zipDirPath+"/unzipDir"+dir2+"/file") || File.IsDir(zipDirPath+"/unzipDir"+dir2+"/file") {
t.Error("Unzip failed")
return
}
}
func TestMain(m *testing.M) {
retCode := m.Run()
// clean test data
os.RemoveAll(zipDirPath + ".zip")
os.RemoveAll(zipDirPath)
os.RemoveAll(tarDirPath + ".tar")
os.RemoveAll(tarDirPath)
os.RemoveAll(untarDirPath)
os.Exit(retCode)
}