-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_test.go
164 lines (149 loc) · 3.05 KB
/
fs_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
package hashembed
import (
"embed"
"testing"
"github.com/stretchr/testify/assert"
)
//go:embed testdata/*
var testEmbed embed.FS
var testHashEmbed *HashedFS
var testHashEmbedOptions *HashedFS
func init() {
testHashEmbed, _ = Generate(testEmbed)
testHashEmbedOptions, _ = Generate(testEmbed, Config{
Hasher: Crc32Hasher,
Renamer: FullNameRenamer,
})
}
func TestGetHashedPath(t *testing.T) {
assert.Equal(
t,
"testdata/7f2cded6.css",
testHashEmbedOptions.GetHashedPath("testdata/test.css"),
"GetHashedPath path should be calculated correctly",
)
assert.Equal(
t,
"testdata/test.txt",
testHashEmbedOptions.GetHashedPath("testdata/test.txt"),
"GetHashedPath path should remain unchanged when file not hashed",
)
}
func TestGetIntegrity(t *testing.T) {
assert.Equal(
t,
"jXfwTDviq81VTyYhMLpsMPJ3MY5mWItqDZX0dsSufEg=",
testHashEmbedOptions.GetIntegrity("testdata/test.css"),
"GetIntegrity should be calculated correctly",
)
assert.Equal(
t,
"",
testHashEmbedOptions.GetIntegrity("testdata/fake.txt"),
"GetIntegrity be blank when file does not exist",
)
}
func TestGetActualPath(t *testing.T) {
assert.Equal(
t,
"testdata/test.css",
testHashEmbedOptions.GetActualPath("testdata/7f2cded6.css"),
"GetActualPath path should be calculated correctly",
)
assert.Equal(
t,
"testdata/test.txt",
testHashEmbedOptions.GetActualPath("testdata/test.txt"),
"GetActualPath path should remain unchanged when file not hashed",
)
}
func TestReadFile(t *testing.T) {
data, err := testHashEmbedOptions.ReadFile("testdata/7f2cded6.css")
assert.Equal(
t,
nil,
err,
"no error should occur reading the file",
)
assert.Equal(
t,
"body { width: 100%; }\n",
string(data[:]),
"file should have correct data",
)
}
func TestReadFileNested(t *testing.T) {
data, err := testHashEmbed.ReadFile("testdata/folder/test2_123@#%(!.css")
assert.Equal(
t,
nil,
err,
"no error should occur reading the nested file",
)
assert.Equal(
t,
"body { width: 5000px; }\n",
string(data[:]),
"nested file should have correct data",
)
}
func TestReadDir(t *testing.T) {
data, err := testHashEmbed.ReadDir("testdata")
assert.Equal(
t,
nil,
err,
"no error should occur reading a directory",
)
assert.Equal(
t,
"folder",
data[0].Name(),
"folder should be in directory",
)
assert.Equal(
t,
true,
data[0].IsDir(),
"folder should be a directory",
)
assert.Equal(
t,
"test.css",
data[1].Name(),
"test.css should be in directory",
)
assert.Equal(
t,
"test.txt",
data[2].Name(),
"test.txt should be in directory",
)
}
func TestOpenFile(t *testing.T) {
f, err := testHashEmbed.Open("testdata/test.txt")
assert.Equal(
t,
nil,
err,
"no error should occur opening a file",
)
stat, err := f.Stat()
assert.Equal(
t,
nil,
err,
"no error should occur when get the file info for an opened file",
)
assert.Equal(
t,
"test.txt",
stat.Name(),
"text.txt should be the name of the opened file",
)
}
func BenchmarkGenerate(b *testing.B) {
for n := 0; n < b.N; n++ {
Generate(testEmbed)
}
}