-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
archive_test.go
89 lines (77 loc) · 1.84 KB
/
archive_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
package lambroll_test
import (
"archive/zip"
"fmt"
"os"
"testing"
"time"
"github.com/fujiwara/lambroll"
)
type zipTestSuite struct {
WorkingDir string
SrcDir string
}
func (s zipTestSuite) String() string {
return fmt.Sprintf("%s_src_%s", s.WorkingDir, s.SrcDir)
}
var createZipArchives = []zipTestSuite{
{".", "test/src"},
{"test/src/dir", "../"},
}
func TestCreateZipArchive(t *testing.T) {
for _, s := range createZipArchives {
t.Run(s.String(), func(t *testing.T) {
testCreateZipArchive(t, s)
})
}
}
func testCreateZipArchive(t *testing.T, s zipTestSuite) {
cwd, _ := os.Getwd()
os.Chdir(s.WorkingDir)
defer os.Chdir(cwd)
excludes := []string{}
excludes = append(excludes, lambroll.DefaultExcludes...)
excludes = append(excludes, []string{"*.bin", "skip/*"}...)
r, info, err := lambroll.CreateZipArchive(s.SrcDir, excludes)
if err != nil {
t.Error("faile to CreateZipArchive", err)
}
defer r.Close()
defer os.Remove(r.Name())
zr, err := zip.OpenReader(r.Name())
if err != nil {
t.Error("failed to new zip reader", err)
}
if len(zr.File) != 4 {
t.Errorf("unexpected included files num %d expect %d", len(zr.File), 3)
}
for _, f := range zr.File {
h := f.FileHeader
t.Logf("%s %10d %s %s",
h.Mode(),
h.UncompressedSize64,
h.Modified.Format(time.RFC3339),
h.Name,
)
}
if info.Size() < 100 {
t.Errorf("too small file got %d bytes", info.Size())
}
}
func TestLoadZipArchive(t *testing.T) {
r, info, err := lambroll.LoadZipArchive("test/src.zip")
if err != nil {
t.Error("failed to LoadZipArchive", err)
}
defer r.Close()
if info.Size() < 100 {
t.Errorf("too small file got %d bytes", info.Size())
}
}
func TestLoadNotZipArchive(t *testing.T) {
_, _, err := lambroll.LoadZipArchive("test/src/hello.txt")
if err == nil {
t.Error("must be failed to load not a zip file")
}
t.Log(err)
}