-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy patharchive_test.go
170 lines (153 loc) · 4.03 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package lambroll_test
import (
"archive/zip"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"sort"
"testing"
"time"
"github.com/fujiwara/lambroll"
"github.com/google/go-cmp/cmp"
)
type zipTestSuite struct {
WorkingDir string
SrcDir string
Expected []string
KeepSymlink bool
}
func (s zipTestSuite) String() string {
return fmt.Sprintf("%s_src_%s", s.WorkingDir, s.SrcDir)
}
var createZipArchives = []zipTestSuite{
{
WorkingDir: ".",
SrcDir: "test/src",
Expected: []string{"dir/sub.txt", "ext-hello.txt", "hello.symlink", "hello.txt", "index.js", "world"},
KeepSymlink: false,
},
{
WorkingDir: "test/src/dir",
SrcDir: "../",
Expected: []string{"dir/sub.txt", "ext-hello.txt", "hello.symlink", "hello.txt", "index.js", "world"},
KeepSymlink: false,
},
{
WorkingDir: ".",
SrcDir: "test/src",
Expected: []string{"dir/sub.txt", "dir.symlink", "ext-hello.txt", "hello.symlink", "hello.txt", "index.js", "world"},
KeepSymlink: true,
},
}
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, s.KeepSymlink)
if err != nil {
t.Error("failed 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) != len(s.Expected) {
t.Errorf("unexpected included files num %d expect %d", len(zr.File), len(s.Expected))
}
zipFiles := []string{}
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,
)
zipFiles = append(zipFiles, h.Name)
}
slices.Sort(zipFiles)
slices.Sort(s.Expected)
if diff := cmp.Diff(zipFiles, s.Expected); diff != "" {
t.Errorf("unexpected included files %s", diff)
}
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)
}
func TestUnzip(t *testing.T) {
ctx := context.TODO()
dest := t.TempDir()
if err := lambroll.Unzip(ctx, "test/src.zip", dest, false); err != nil {
t.Error("failed to Unzip", err)
}
unzipEntries := []string{}
err := filepath.WalkDir(dest, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
rel, _ := filepath.Rel(dest, path)
unzipEntries = append(unzipEntries, rel)
return nil
})
if err != nil {
t.Error("failed to walk", err)
}
sort.Strings(unzipEntries)
expected := []string{"dir.symlink", "dir/sub.txt", "hello.symlink", "hello.txt", "world"}
if diff := cmp.Diff(unzipEntries, expected); diff != "" {
t.Errorf("unexpected unzip entries %s", diff)
}
// debug
o, _ := exec.Command("ls", "-lR", dest).Output()
t.Log(string(o))
// check symlink
fi, err := os.Lstat(filepath.Join(dest, "hello.symlink"))
if err != nil {
t.Error("failed to stat hello.symlink", err)
}
if fi.Mode()&os.ModeSymlink == 0 {
t.Error("hello.symlink must be symlink", fi.Mode())
}
linkTarget, err := os.Readlink(filepath.Join(dest, "hello.symlink"))
if err != nil {
t.Error("failed to readlink hello.symlink", err)
}
if diff := cmp.Diff(linkTarget, "hello.txt"); diff != "" {
t.Errorf("unexpected symlink target %s", diff)
}
}