-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_test.go
100 lines (94 loc) · 2.34 KB
/
file_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
package fir
import (
"embed"
"testing"
)
//go:embed testdata/public
var testdata embed.FS
func TestIsDir(t *testing.T) {
tests := []struct {
name string
path string
embedfs *embed.FS
expected bool
}{
{
name: "Embedded Directory",
path: "testdata/public",
embedfs: &testdata,
expected: true,
},
{
name: "Non-Embedded Directory",
path: "testdata/public",
embedfs: nil,
expected: true,
},
{
name: "Non-Existing Directory",
path: "/path/to/non-existing/directory",
embedfs: nil,
expected: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := isDir(test.path, test.embedfs)
if actual != test.expected {
t.Errorf("isDir(%s, %v) = %v, expected %v", test.path, test.embedfs, actual, test.expected)
}
})
}
}
func TestFind(t *testing.T) {
tests := []struct {
name string
path string
extensions []string
embedfs *embed.FS
expected []string
}{
{
name: "Embedded HTML Files",
path: "testdata/public",
extensions: []string{".html"},
embedfs: &testdata,
expected: []string{"testdata/public/index.html", "testdata/public/partials/header.html"},
},
{
name: "Non-Embedded HTML Files",
path: "testdata/public",
extensions: []string{".html"},
embedfs: nil,
expected: []string{"testdata/public/index.html", "testdata/public/partials/header.html"},
},
{
name: "Embedded CSS Files don't exist",
path: "testdata/public",
extensions: []string{".css"},
embedfs: &testdata,
expected: []string{},
},
{
name: "Embedded CSS Files don't exist",
path: "testdata/public",
extensions: []string{".css"},
embedfs: nil,
expected: []string{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := find(test.path, test.extensions, test.embedfs)
if len(actual) != len(test.expected) {
t.Errorf("find(%s, %v, %v) returned %v files, expected %v files", test.path, test.extensions, test.embedfs, len(actual), len(test.expected))
} else {
for i := range actual {
if actual[i] != test.expected[i] {
t.Errorf("find(%s, %v, %v) returned %s, expected %s", test.path, test.extensions, test.embedfs, actual[i], test.expected[i])
}
}
}
})
}
}