-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathological_test.go
237 lines (229 loc) · 7.26 KB
/
pathological_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
package pathologize
import (
"testing"
)
func Test_truncateFilename(t *testing.T) {
tests := []struct {
name string
file string
want string
}{
{"empty", "", ""},
{"short", "foo", "foo"},
{"long",
"foobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbaz",
"foobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoo",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := truncateFilename(tt.file); got != tt.want {
t.Errorf("truncateFilename() = %v, want %v", got, tt.want)
}
})
}
}
func Test_removeSurroundingSpaces(t *testing.T) {
tests := []struct {
name string
file string
want string
}{
{"empty", "", ""},
{"short", "foo", "foo"},
{"long", "foo bar baz", "foo bar baz"},
{"leading", " foo", "foo"},
{"trailing", "foo ", "foo"},
{"both", " foo ", "foo"},
{"multiple", " foo ", "foo"},
{"multiple2", " foo bar ", "foo bar"},
{"multiple3", " foo bar baz ", "foo bar baz"},
{"multiple4", " foo bar baz qux ", "foo bar baz qux"},
{"multiple5", " foo bar baz qux quux ", "foo bar baz qux quux"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := removeLeadingSpaces(tt.file); got != tt.want {
t.Errorf("removeSurroundingSpaces() = %v, want %v", got, tt.want)
}
})
}
}
func Test_removeTrailingDots(t *testing.T) {
tests := []struct {
name string
filename string
want string
}{
{"empty", "", ""},
{"short", "foo", "foo"},
{"long", "foo.bar.baz", "foo.bar.baz"},
{"trailing", "foo.", "foo"},
{"multiple", "foo..", "foo"},
{"multiple2", "foo...", "foo"},
{"leading", ".foo", ".foo"},
{"both", ".foo.", ".foo"},
{"space dot space", ".foo . ", ".foo"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := removeTrailing(tt.filename); got != tt.want {
t.Errorf("removeTrailingDots() = %v, want %v", got, tt.want)
}
})
}
}
func Test_removeReservedNames(t *testing.T) {
tests := []struct {
name string
filename string
want string
}{
{"empty", "", ""},
{"short", "foo", "foo"},
{"long", "foo.bar.baz", "foo.bar.baz"},
{"reserved", "CON", "CON_"},
{"reserved2", "PRN", "PRN_"},
{"reserved3", "AUX", "AUX_"},
{"reserved4", "NUL", "NUL_"},
{"reserved5", "COM1", "COM1_"},
{"reserved6", "COM2", "COM2_"},
{"reserved7", "COM3", "COM3_"},
{"reserved8", "COM4", "COM4_"},
{"reserved9", "COM5", "COM5_"},
{"reserved10", "COM6", "COM6_"},
{"reserved11", "COM7", "COM7_"},
{"reserved12", "COM8", "COM8_"},
{"reserved13", "COM9", "COM9_"},
{"reserved14", "LPT1", "LPT1_"},
{"reserved15", "LPT2", "LPT2_"},
{"reserved16", "LPT3", "LPT3_"},
{"reserved17", "LPT4", "LPT4_"},
{"reserved18", "LPT5", "LPT5_"},
{"reserved19", "LPT6", "LPT6_"},
{"reserved20", "LPT7", "LPT7_"},
{"reserved21", "LPT8", "LPT8_"},
{"reserved22", "LPT9", "LPT9_"},
{"windows reserved", "$Mft", "$Mft_"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := removeReservedNames(tt.filename); got != tt.want {
t.Errorf("removeReservedNames() = %v, want %v", got, tt.want)
}
})
}
}
func Test_CleanFilename(t *testing.T) {
tests := []struct {
name string
filename string
want string
}{
{"empty", "", defaultName},
{"trailing period", "CON.", "CON_"},
{"trailing space", "PRN ", "PRN_"},
{"leading space", " PRN", "PRN_"},
{"space period", " AUX.", "AUX_"},
{"period space", ".NUL ", ".NUL"},
{"double period", "CON..", "CON_"},
{"triple period", "CON...", "CON_"},
{"triple period space", "CON... ", "CON_"},
{"triple period space period", "CON... .", "CON_"},
{"triple period space period space", "CON... . ", "CON_"},
{"windows reserved", "$Mft", "$Mft_"},
{"windows reserved period", "$Mft.", "$Mft_"},
{"reserved with extension", "$Mft.txt", "$Mft_.txt"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Clean(tt.filename); got != tt.want {
t.Errorf("removeReservedNames() = %v, want %v", got, tt.want)
}
})
}
}
func Test_paths(t *testing.T) {
tests := []struct {
name string
filename string
want string
}{
{"empty", "", "file"},
{"short", "foo", "foo"},
{"long", "foo.bar.baz", "foo.bar.baz"},
{"several dirs", "foo/bar/baz", "foo/bar/baz"},
{"several dirs2", "foo\\bar\\baz", "foobarbaz"},
{"dirs with spaces", "foo bar/baz", "foo bar/baz"},
{"dirs with bad chars", "foo*bar/baz", "foobar/baz"},
{"example", "foo/bar:baz*qux", "foo/barbazqux"},
{"readme", "C:/Users/dir:e*c?t<o>r|y/CON..", "C/Users/directory/CON_"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CleanPath(tt.filename); got != tt.want {
t.Errorf("paths() = %v, want %v", got, tt.want)
}
})
}
}
func Test_removeInvalidCharacters(t *testing.T) {
tests := []struct {
name string
filename string
want string
}{
{"empty", "", ""},
{"short", "foo", "foo"},
{"emoji", "👍", "👍"},
{"long", "foo.bar.baz", "foo.bar.baz"},
{"invalid", "foo/bar", "foobar"},
{"invalidx01", "\x01foo/bar", "foobar"},
{"invalid2", "foo\\bar", "foobar"},
{"invalid3", "foo:bar", "foobar"},
{"invalid4", "foo*bar", "foobar"},
{"invalid5", "foo?bar", "foobar"},
{"invalid6", "foo\"bar", "foobar"},
{"invalid7", "foo<bar", "foobar"},
{"invalid8", "foo>bar", "foobar"},
{"invalid9", "foo|bar", "foobar"},
{"invalid10", "foo/bar/baz", "foobarbaz"},
{"invalid11", "foo\\bar\\baz", "foobarbaz"},
{"invalid12", "foo:bar:baz", "foobarbaz"},
{"invalid13", "foo*bar*baz", "foobarbaz"},
{"invalid@", "foo@bar", "foobar"},
{"invalid!", "foo!bar", "foobar"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := removeInvalidCharacters(tt.filename); got != tt.want {
t.Errorf("removeInvalidCharacters() = %v, want %v", got, tt.want)
}
})
}
}
func Test_characterFilter(t *testing.T) {
tests := []struct {
name string
filename string
want string
}{
{"empty", "", ""},
{"valid characters", "filename", "filename"},
{"invalid characters", "file:name", "filename"},
{"multiple invalid characters", "file:name*with?invalid|chars", "filenamewithinvalidchars"},
{"leading invalid characters", ":*?<>|filename", "filename"},
{"trailing invalid characters", "filename:*?<>|", "filename"},
{"mixed valid and invalid characters", "file:name*with?valid|chars", "filenamewithvalidchars"},
{"only invalid characters", ":*?<>|", ""},
{"invalid characters with spaces", "file name*with?invalid|chars", "file namewithinvalidchars"},
{"invalid characters with unicode", "file👍name*with?invalid|chars", "file👍namewithinvalidchars"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CharacterFilterRegex.ReplaceAllString(tt.filename, ""); got != tt.want {
t.Errorf("characterFilter() = %v, want %v", got, tt.want)
}
})
}
}