-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles_test.go
107 lines (99 loc) · 2.74 KB
/
files_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
package alligotor
import (
"io"
"os"
"path"
"strings"
"testing/fstest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("files", func() {
Describe("loadFiles", func() {
var (
nilGlobF = func(_ string) ([]string, error) {
return nil, nil
}
globF = func(pattern string) ([]string, error) {
return []string{pattern}, nil
}
openF = func(path string) (io.Reader, error) {
return strings.NewReader(path), nil
}
)
Context("no globs", func() {
It("returns empty slice", func() {
readers, err := loadFiles(nil, globF, openF)
Expect(err).ToNot(HaveOccurred())
Expect(readers).To(HaveLen(0))
})
})
Context("no matches found for globs", func() {
It("returns empty slice", func() {
readers, err := loadFiles([]string{"test1", "test2"}, nilGlobF, openF)
Expect(err).ToNot(HaveOccurred())
Expect(readers).To(HaveLen(0))
})
})
Context("existing matches", func() {
It("returns readers for all matches", func() {
input := []string{"test1", "test2"}
readers, err := loadFiles(input, globF, openF)
Expect(err).ToNot(HaveOccurred())
Expect(readers).To(HaveLen(2))
var contents []string
for _, reader := range readers {
content, err := io.ReadAll(reader)
Expect(err).NotTo(HaveOccurred())
contents = append(contents, string(content))
}
Expect(input).To(Equal(contents))
})
})
})
Describe("NewFSFilesSource", func() {
var s *FilesSource
BeforeEach(func() {
s = NewFSFilesSource(
fstest.MapFS(map[string]*fstest.MapFile{
"test.json": {Data: []byte(`{"test":"json"}`)},
"test.yml": {Data: []byte("test: yml")},
}),
"test.*",
)
})
It("loads the files fileMaps correctly", func() {
Expect(s.Init(nil)).To(Succeed())
Expect(s.fileMaps).To(Equal([]*ciMap{
{m: map[string]interface{}{"test": "json"}},
{m: map[string]interface{}{"test": "yml"}},
}))
})
})
Describe("NewFilesSource", func() {
var (
s *FilesSource
tmpDir string
)
BeforeEach(func() {
var err error
tmpDir, err = os.MkdirTemp("", "tests*")
Expect(err).ToNot(HaveOccurred())
jsonContent := []byte(`{"test":"json"}`)
ymlContent := []byte(`test: "yml"`)
Expect(os.WriteFile(path.Join(tmpDir, "test.json"), jsonContent, 0600)).To(Succeed())
Expect(os.WriteFile(path.Join(tmpDir, "test.yml"), ymlContent, 0600)).To(Succeed())
s = NewFilesSource(path.Join(tmpDir, "test.*"))
})
AfterEach(func() {
Expect(os.RemoveAll(tmpDir)).To(Succeed())
})
It("loads the files fileMaps correctly", func() {
Expect(s.Init(nil)).To(Succeed())
Expect(s.fileMaps).To(Equal([]*ciMap{
{m: map[string]interface{}{"test": "json"}},
{m: map[string]interface{}{"test": "yml"}},
}))
})
})
})