-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathutils_test.go
389 lines (317 loc) · 10.7 KB
/
utils_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package core
import (
"errors"
"os"
"path/filepath"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/afero"
"github.com/spf13/afero/mem"
"github.com/miniscruff/changie/shared"
. "github.com/miniscruff/changie/testutils"
)
var _ = Describe("Utils", func() {
It("append file appends two files", func() {
memFs := afero.NewMemMapFs()
afs := afero.Afero{Fs: memFs}
rootFile, err := memFs.Create("root.txt")
Expect(err).To(BeNil())
_, err = rootFile.WriteString("root")
Expect(err).To(BeNil())
err = afs.WriteFile("append.txt", []byte(" append"), CreateFileMode)
Expect(err).To(BeNil())
err = AppendFile(afs.Open, rootFile, "append.txt")
Expect(err).To(BeNil())
rootFile.Close()
bytes, err := afero.ReadFile(memFs, "root.txt")
Expect(err).To(BeNil())
Expect(string(bytes)).To(Equal("root append"))
})
It("append file fails if open fails", func() {
mockError := errors.New("bad open")
mockFs := NewMockFS()
mockFs.MockOpen = func(filename string) (afero.File, error) {
return nil, mockError
}
rootFile := NewMockFile(mockFs, "root.txt")
defer rootFile.Close()
err := AppendFile(mockFs.Open, rootFile, "dummy.txt")
Expect(err).To(Equal(mockError))
})
It("get all versions returns all versions", func() {
config := Config{
ChangesDir: "changes",
HeaderPath: "header.md",
}
fs := NewMockFS()
afs := afero.Afero{Fs: fs}
headerFile, err := afs.Create(filepath.Join("changes", "header.md"))
Expect(err).To(BeNil())
file1, err := afs.Create(filepath.Join("changes", "v0.1.0.md"))
Expect(err).To(BeNil())
file2, err := afs.Create(filepath.Join("changes", "v0.2.0.md"))
Expect(err).To(BeNil())
file3, err := afs.Create(filepath.Join("changes", "not-sem-ver.md"))
Expect(err).To(BeNil())
mockRead := func(dirname string) ([]os.FileInfo, error) {
return []os.FileInfo{
headerFile.(*mem.File).Info(),
file1.(*mem.File).Info(),
file2.(*mem.File).Info(),
file3.(*mem.File).Info(),
}, nil
}
vers, err := GetAllVersions(mockRead, config, false)
Expect(err).To(BeNil())
Expect(vers[0].Original()).To(Equal("v0.2.0"))
Expect(vers[1].Original()).To(Equal("v0.1.0"))
})
It("get all versions returns error on bad read dir", func() {
config := Config{}
mockError := errors.New("bad stuff")
mockRead := func(dirname string) ([]os.FileInfo, error) {
return []os.FileInfo{}, mockError
}
vers, err := GetAllVersions(mockRead, config, false)
Expect(vers).To(BeEmpty())
Expect(err).To(Equal(mockError))
})
createVersions := func(latestVersion string) (shared.ReadDirer, Config) {
config := Config{
ChangesDir: "changes",
HeaderPath: "header.md",
}
fs := NewMockFS()
afs := afero.Afero{Fs: fs}
headerFile, err := afs.Create(filepath.Join("changes", "header.md"))
Expect(err).To(BeNil())
file1, err := afs.Create(filepath.Join("changes", "v0.1.0.md"))
Expect(err).To(BeNil())
file2, err := afs.Create(filepath.Join("changes", latestVersion+".md"))
Expect(err).To(BeNil())
file3, err := afs.Create(filepath.Join("changes", "not-sem-ver.md"))
Expect(err).To(BeNil())
mockRead := func(dirname string) ([]os.FileInfo, error) {
return []os.FileInfo{
headerFile.(*mem.File).Info(),
file1.(*mem.File).Info(),
file2.(*mem.File).Info(),
file3.(*mem.File).Info(),
}, nil
}
return mockRead, config
}
It("get latest version returns most recent version", func() {
mockRead, config := createVersions("v0.2.0")
ver, err := GetLatestVersion(mockRead, config, false)
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v0.2.0"))
})
It("get latest version with prerelease", func() {
mockRead, config := createVersions("v0.2.0-rc1")
vers, err := GetLatestVersion(mockRead, config, false)
Expect(err).To(BeNil())
Expect(vers.Original()).To(Equal("v0.2.0-rc1"))
})
It("get latest version with prerelease skipped", func() {
mockRead, config := createVersions("v0.2.0-rc1")
vers, err := GetLatestVersion(mockRead, config, true)
Expect(err).To(BeNil())
Expect(vers.Original()).To(Equal("v0.1.0"))
})
It("get latest version returns v0.0.0 if no versions exist", func() {
config := Config{
ChangesDir: "changes",
HeaderPath: "header.md",
}
mockRead := func(dirname string) ([]os.FileInfo, error) {
return []os.FileInfo{}, nil
}
ver, err := GetLatestVersion(mockRead, config, false)
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v0.0.0"))
})
It("get latest version returns error if get all version errors", func() {
config := Config{}
mockError := errors.New("bad stuff")
mockRead := func(dirname string) ([]os.FileInfo, error) {
return []os.FileInfo{}, mockError
}
vers, err := GetLatestVersion(mockRead, config, false)
Expect(vers).To(BeNil())
Expect(err).To(Equal(mockError))
})
It("get next version returns error if get next version errors", func() {
config := Config{}
mockError := errors.New("bad stuff")
mockRead := func(dirname string) ([]os.FileInfo, error) {
return []os.FileInfo{}, mockError
}
vers, err := GetNextVersion(mockRead, config, "major", nil, nil)
Expect(vers).To(BeNil())
Expect(err).To(Equal(mockError))
})
It("get next version returns error if invalid version", func() {
mockRead, config := createVersions("v0.2.0")
vers, err := GetNextVersion(mockRead, config, "a", []string{}, []string{})
Expect(vers).To(BeNil())
Expect(err).To(Equal(ErrBadVersionOrPart))
})
It("get next version works on brand new version", func() {
mockRead, config := createVersions("v0.2.0")
ver, err := GetNextVersion(mockRead, config, "v0.4.0", nil, nil)
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v0.4.0"))
})
It("get next version works on only major version", func() {
mockRead, config := createVersions("v0.2.0")
ver, err := GetNextVersion(mockRead, config, "v2", nil, nil)
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v2"))
})
It("get next version works on major and minor versions", func() {
mockRead, config := createVersions("v0.2.0")
ver, err := GetNextVersion(mockRead, config, "v1.2", nil, nil)
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v1.2"))
})
It("get next version with short semver and prerelease", func() {
mockRead, config := createVersions("v0.2.0")
ver, err := GetNextVersion(mockRead, config, "v1.5", []string{"rc2"}, nil)
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v1.5.0-rc2"))
})
It("get next version works on major version", func() {
mockRead, config := createVersions("v0.2.0")
ver, err := GetNextVersion(mockRead, config, "major", nil, nil)
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v1.0.0"))
})
It("get next version works on minor version", func() {
mockRead, config := createVersions("v0.2.2")
ver, err := GetNextVersion(mockRead, config, "minor", nil, nil)
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v0.3.0"))
})
It("get next version works on patch version", func() {
mockRead, config := createVersions("v0.2.5")
ver, err := GetNextVersion(mockRead, config, "patch", nil, nil)
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v0.2.6"))
})
It("get next version with prerelease", func() {
mockRead, config := createVersions("v0.2.5")
ver, err := GetNextVersion(mockRead, config, "patch", []string{"b1", "amd64"}, nil)
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v0.2.6-b1.amd64"))
})
It("get next version with meta", func() {
mockRead, config := createVersions("v0.2.5")
ver, err := GetNextVersion(mockRead, config, "patch", nil, []string{"githash", "amd64"})
Expect(err).To(BeNil())
Expect(ver.Original()).To(Equal("v0.2.6+githash.amd64"))
})
It("get next version error with bad prerelease", func() {
mockRead, config := createVersions("v0.2.5")
_, err := GetNextVersion(mockRead, config, "patch", []string{"0005"}, nil)
Expect(err).NotTo(BeNil())
})
It("get next version error with bad meta", func() {
mockRead, config := createVersions("v0.2.5")
_, err := GetNextVersion(mockRead, config, "patch", nil, []string{"&*(&*(&"})
Expect(err).NotTo(BeNil())
})
It("can find change files", func() {
memFs := afero.NewMemMapFs()
afs := afero.Afero{Fs: memFs}
config := Config{
ChangesDir: ".chng",
UnreleasedDir: "unrel",
}
_, _ = memFs.Create(".chng/unrel/a.yaml")
_, _ = memFs.Create(".chng/unrel/b.yaml")
_, _ = memFs.Create(".chng/alpha/c.yaml")
_, _ = memFs.Create(".chng/alpha/d.yaml")
_, _ = memFs.Create(".chng/beta/e.yaml")
_, _ = memFs.Create(".chng/beta/f.yaml")
_, _ = memFs.Create(".chng/beta/ignored.md")
_, _ = memFs.Create(".chng/ignored/g.yaml")
_, _ = memFs.Create(".chng/h.md") // ignored
files, err := FindChangeFiles(config, afs.ReadDir, []string{"alpha", "beta"})
Expect(err).To(BeNil())
Expect(files).To(Equal([]string{
filepath.Join(".chng", "alpha", "c.yaml"),
filepath.Join(".chng", "alpha", "d.yaml"),
filepath.Join(".chng", "beta", "e.yaml"),
filepath.Join(".chng", "beta", "f.yaml"),
filepath.Join(".chng", "unrel", "a.yaml"),
filepath.Join(".chng", "unrel", "b.yaml"),
}))
})
It("change files fails if dir is not accessible", func() {
memFs := afero.NewMemMapFs()
afs := afero.Afero{Fs: memFs}
config := Config{
ChangesDir: ".chng",
UnreleasedDir: "unrel",
}
_, _ = memFs.Create(".chng/unrel/a.yaml")
_, err := FindChangeFiles(config, afs.ReadDir, []string{"../../invalid"})
Expect(err).NotTo(BeNil())
})
It("can write newlines", func() {
var writer strings.Builder
err := WriteNewlines(&writer, 3)
Expect(err).To(BeNil())
Expect(writer.String()).To(Equal("\n\n\n"))
})
It("skips newlines if lines is 0", func() {
var writer strings.Builder
err := WriteNewlines(&writer, 0)
Expect(err).To(BeNil())
Expect(writer.String()).To(Equal(""))
})
It("returns error on bad write newlines", func() {
errMock := errors.New("mock error")
writer := BadWriter{Err: errMock}
err := WriteNewlines(&writer, 3)
Expect(err).To(Equal(errMock))
})
It("can split env vars", func() {
vars := []string{
"A=b",
"B=5+5=10",
"CHANGIE_NAME=some_name",
}
Expect(EnvVarMap(vars)).To(Equal(map[string]string{
"A": "b",
"B": "5+5=10",
"CHANGIE_NAME": "some_name",
}))
})
It("does not load any env vars if no config", func() {
cfg := Config{}
vars := []string{
"A=b",
"B=5+5=10",
"CHANGIE_NAME=some_name",
}
envs := LoadEnvVars(&cfg, vars)
Expect(envs).To(BeEmpty())
})
It("loads envs with prefix", func() {
cfg := Config{
EnvPrefix: "CHANGIE_",
}
vars := []string{
"A=b",
"B=5+5=10",
"CHANGIE_NAME=some_name",
}
envs := LoadEnvVars(&cfg, vars)
Expect(envs).To(Equal(map[string]string{
"NAME": "some_name",
}))
})
})