-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_test.go
135 lines (125 loc) · 3.33 KB
/
hash_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
package static
import (
"os"
"regexp"
"testing"
)
var testsFilenameBaseHash = []struct {
base string
want string
}{
{"one.edbc4f9728a8e311b55e081b27e3caff", "edbc4f9728a8e311b55e081b27e3caff"},
{"one.edbc4f9728a8e311", ""},
{"one.edbc4f(728a8e&11b55!081b$7e3caff", ""},
}
type argsHashSplitFilepath struct{ prefix, hash, ext string }
var testsHashSplitFilepath = []struct {
path string
want argsHashSplitFilepath
}{
{"main", argsHashSplitFilepath{
"main",
"",
"",
}}, {"main.js", argsHashSplitFilepath{
"main",
"",
".js",
}}, {"main.js.", argsHashSplitFilepath{
"main.js",
"",
".",
}}, {"main.edbc4f9728a8e311b55e081b27e3caff.", argsHashSplitFilepath{
"main",
"edbc4f9728a8e311b55e081b27e3caff",
".",
}}, {"main.min.js", argsHashSplitFilepath{
"main.min",
"",
".js",
}}, {"main.min.edbc4f9728a8e311b55e081b27e3caff.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", argsHashSplitFilepath{
"main.min",
"edbc4f9728a8e311b55e081b27e3caff",
".xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
}}, {"edbc4f9728a8e311b55e081b27e3caff", argsHashSplitFilepath{
"edbc4f9728a8e311b55e081b27e3caff",
"",
"",
}}, {"edbc4f9728a8e311b55e081b27e3caff.js", argsHashSplitFilepath{
"edbc4f9728a8e311b55e081b27e3caff",
"",
".js",
}}, {"edbc4f9728a8e311b55e081b27e3caff.min.js", argsHashSplitFilepath{
"edbc4f9728a8e311b55e081b27e3caff.min",
"",
".js",
}}, {"main-extra.min.edbc4f9728a8e311b55e081b27e3caff", argsHashSplitFilepath{
"main-extra.min",
"",
".edbc4f9728a8e311b55e081b27e3caff",
}}, {"main-extra.min.edbc4f9728a8e311b55e081b27e3caff.js", argsHashSplitFilepath{
"main-extra.min",
"edbc4f9728a8e311b55e081b27e3caff",
".js",
}}, {"/styles/main-extra.min.edbc4f9728a8e311b55e081b27e3caff.js", argsHashSplitFilepath{
"/styles/main-extra.min",
"edbc4f9728a8e311b55e081b27e3caff",
".js",
}}, {"main.edbc4f9728a8e311.js", argsHashSplitFilepath{
"main.edbc4f9728a8e311",
"",
".js",
}}, {"main.edbc4f(728a8e&11b55!081b$7e3caff.js", argsHashSplitFilepath{
"main.edbc4f(728a8e&11b55!081b$7e3caff",
"",
".js",
}}, {"main.edbc4f9728a8e311b55e081b27e3caff.min.js", argsHashSplitFilepath{
"main.edbc4f9728a8e311b55e081b27e3caff.min",
"",
".js",
}},
}
var testsFileHash = []struct {
path string
want string
}{
{"LICENSE", "edbc4f9728a8e311b55e081b27e3caff"},
{"unknown", ""},
}
func TestHashSplitFilepath(t *testing.T) {
for _, tt := range testsHashSplitFilepath {
prefix, hash, ext := hashSplitFilepath(tt.path)
if prefix != tt.want.prefix || hash != tt.want.hash || ext != tt.want.ext {
t.Errorf("%q:\nwant: %q %q %q\ngot: %q %q %q", tt.path, tt.want.prefix, tt.want.hash, tt.want.ext, prefix, hash, ext)
}
}
}
func TestFileHash(t *testing.T) {
for _, tt := range testsFileHash {
got, err := fileHash(tt.path)
if err != nil && !os.IsNotExist(err) {
panic(err)
}
if got != tt.want {
t.Errorf("%q: want %q, got %q", tt.path, tt.want, got)
}
}
}
func BenchmarkHashSplitFilepath(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tt := range testsHashSplitFilepath {
hashSplitFilepath(tt.path)
}
}
}
func BenchmarkIsHashRegexp(b *testing.B) {
re := regexp.MustCompile(`^[a-f0-9]{32}$`)
for i := 0; i < b.N; i++ {
re.FindStringSubmatch("edbc4f9728a8e311b55e081b27e3caff")
}
}
func BenchmarkIsHashCustom(b *testing.B) {
for i := 0; i < b.N; i++ {
isHash("edbc4f9728a8e311b55e081b27e3caff")
}
}