This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcompressor_test.go
224 lines (182 loc) · 6.05 KB
/
compressor_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package parcello_test
import (
"archive/zip"
"bytes"
"fmt"
"path/filepath"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/phogolabs/parcello"
"github.com/phogolabs/parcello/fake"
)
var _ = Describe("ZipCompressor", func() {
var (
compressor *parcello.ZipCompressor
)
BeforeEach(func() {
compressor = &parcello.ZipCompressor{
Config: &parcello.CompressorConfig{
Logger: GinkgoWriter,
Filename: "bundle",
Recurive: true,
},
}
})
It("compresses a given hierarchy", func() {
fileSystem := parcello.Dir("./fixture")
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
}
bundle, err := compressor.Compress(ctx)
Expect(err).To(BeNil())
Expect(bundle).NotTo(BeNil())
Expect(bundle.Name).To(Equal("bundle"))
reader, err := zip.NewReader(bytes.NewReader(bundle.Body), int64(len(bundle.Body)))
Expect(err).To(BeNil())
Expect(reader.File).To(HaveLen(4))
Expect(reader.File[0].Name).To(Equal("resource/reports/2018.txt"))
Expect(reader.File[1].Name).To(Equal("resource/scripts/schema.sql"))
Expect(reader.File[2].Name).To(Equal("resource/templates/html/index.html"))
Expect(reader.File[3].Name).To(Equal("resource/templates/yml/schema.yml"))
})
Context("when the offset is provided", func() {
It("compresses a given hierarchy", func() {
fileSystem := parcello.Dir("./fixture")
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
Offset: 1,
}
bundle, err := compressor.Compress(ctx)
Expect(err).To(BeNil())
Expect(bundle).NotTo(BeNil())
})
})
Context("whene ingore pattern is provided", func() {
It("ignores that files", func() {
compressor.Config.IgnorePatterns = []string{"*/**/*.txt"}
fileSystem := parcello.Dir("./fixture")
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
}
bundle, err := compressor.Compress(ctx)
Expect(err).To(BeNil())
Expect(bundle).NotTo(BeNil())
Expect(bundle.Name).To(Equal("bundle"))
reader, err := zip.NewReader(bytes.NewReader(bundle.Body), int64(len(bundle.Body)))
Expect(err).To(BeNil())
Expect(reader.File).To(HaveLen(3))
Expect(reader.File[0].Name).To(Equal("resource/scripts/schema.sql"))
Expect(reader.File[1].Name).To(Equal("resource/templates/html/index.html"))
Expect(reader.File[2].Name).To(Equal("resource/templates/yml/schema.yml"))
})
Context("when the pattern is directory", func() {
It("ignores the directory and its files", func() {
compressor.Config.IgnorePatterns = []string{"resource/templates/**/*"}
fileSystem := parcello.Dir("./fixture")
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
}
bundle, err := compressor.Compress(ctx)
Expect(err).To(BeNil())
Expect(bundle).NotTo(BeNil())
Expect(bundle.Name).To(Equal("bundle"))
reader, err := zip.NewReader(bytes.NewReader(bundle.Body), int64(len(bundle.Body)))
Expect(err).To(BeNil())
Expect(reader.File[0].Name).To(Equal("resource/reports/2018.txt"))
Expect(reader.File[1].Name).To(Equal("resource/scripts/schema.sql"))
})
It("ignores the whole directory", func() {
compressor.Config.IgnorePatterns = []string{"resource/templates"}
fileSystem := parcello.Dir("./fixture")
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
}
bundle, err := compressor.Compress(ctx)
Expect(err).To(BeNil())
Expect(bundle).NotTo(BeNil())
Expect(bundle.Name).To(Equal("bundle"))
reader, err := zip.NewReader(bytes.NewReader(bundle.Body), int64(len(bundle.Body)))
Expect(err).To(BeNil())
Expect(reader.File[0].Name).To(Equal("resource/reports/2018.txt"))
Expect(reader.File[1].Name).To(Equal("resource/scripts/schema.sql"))
})
})
})
Context("when the pattern is invalid", func() {
It("returns an error", func() {
compressor.Config.IgnorePatterns = []string{"[*"}
fileSystem := parcello.Dir("./fixture")
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
}
bundle, err := compressor.Compress(ctx)
Expect(err).To(MatchError("syntax error in pattern"))
Expect(bundle).To(BeNil())
})
})
Context("when the recursion is disabled", func() {
It("does not go through the hierarchy", func() {
compressor.Config.Recurive = false
fileSystem := parcello.Dir("./fixture")
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
}
bundle, err := compressor.Compress(ctx)
Expect(err).To(BeNil())
Expect(bundle).To(BeNil())
})
})
Context("when opening file fails", func() {
It("return the error", func() {
fileSystem := &fake.FileSystem{}
fileSystem.WalkStub = parcello.Dir("./fixture").Walk
fileSystem.OpenFileReturns(nil, fmt.Errorf("Oh no!"))
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
}
binary, err := compressor.Compress(ctx)
Expect(err).To(MatchError("Oh no!"))
Expect(binary).To(BeNil())
})
})
Context("when the walker returns an nil file info", func() {
It("return the error", func() {
fileSystem := &fake.FileSystem{}
fileSystem.WalkStub = func(dir string, fn filepath.WalkFunc) error {
return fn("/", nil, nil)
}
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
}
bundle, err := compressor.Compress(ctx)
Expect(err).To(BeNil())
Expect(bundle).To(BeNil())
})
})
Context("when the walker callback has an error", func() {
It("return the error", func() {
fileSystem := &fake.FileSystem{}
fileSystem.WalkStub = func(dir string, fn filepath.WalkFunc) error {
return fn("path", nil, fmt.Errorf("Oh no!"))
}
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
}
bundle, err := compressor.Compress(ctx)
Expect(err).To(MatchError("Oh no!"))
Expect(bundle).To(BeNil())
})
})
Context("when the traversing fails", func() {
It("return the error", func() {
fileSystem := &fake.FileSystem{}
fileSystem.WalkReturns(fmt.Errorf("Oh no!"))
ctx := &parcello.CompressorContext{
FileSystem: fileSystem,
}
bundle, err := compressor.Compress(ctx)
Expect(err).To(MatchError("Oh no!"))
Expect(bundle).To(BeNil())
})
})
})