-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
80 lines (69 loc) · 1.69 KB
/
util_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
package presskit
import (
"archive/zip"
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)
func TestJoin(t *testing.T) {
parts := []string{"this", "is", "my", "path"}
j := join(parts...)
fj := filepath.Join(parts...)
if j != fj {
t.Error("Join doesn't work properly")
}
}
func TestZipFiles(t *testing.T) {
basepath := "./test/zipfiles"
testArchive := filepath.Join(basepath, "archive.zip")
// return an error on not existing file
_, err := zipFiles(basepath, []string{"notexisting"})
if err == nil {
t.Error("zipFiles should return an error on a nonexistant file")
}
// create and save zip
zipData, err := zipFiles(basepath, []string{"file1.txt", "file2.txt"})
if err != nil {
t.Error("error in zipFiles:" + err.Error())
}
err = ioutil.WriteFile(testArchive, zipData, 0644)
if err != nil {
t.Error("error writing test archive: " + err.Error())
}
// read the file
r, err := zip.OpenReader(testArchive)
if err != nil {
t.Error("error opening test archive: " + err.Error())
}
defer r.Close()
for _, f := range r.File {
// get content of zipped file
var zipContent bytes.Buffer
fileZipped, err := f.Open()
if err != nil {
t.Error(err)
}
_, err = io.Copy(&zipContent, fileZipped)
if err != nil {
t.Error(err)
}
fileZipped.Close()
// get content of original file
originalContent, err := ioutil.ReadFile(filepath.Join(basepath, f.Name))
if err != nil {
t.Error(err)
}
// compare
if !reflect.DeepEqual(zipContent.Bytes(), originalContent) {
t.Error("zipped content doesn't equal original content")
}
}
// cleanup
if err := os.Remove(testArchive); err != nil {
t.Error("error removing test archive: " + err.Error())
}
}