-
Notifications
You must be signed in to change notification settings - Fork 0
/
filever_test.go
350 lines (309 loc) · 9.01 KB
/
filever_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package filever
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/cyphrme/watchmod"
)
// Each test that writes files has its own directory, so test outputs can be
// pushed to git and used in explanations. Init calls clean before executing
// tests.
var dummySrc = "test/dummy/src"
var dummyDist = "test/dummy/dist"
var dummyNoSrc = "test/dummy_no/src"
var dummyNoDist = "test/dummy_no/dist"
var watchSrc = "test/watch/src"
var watchDist = "test/watch/dist"
var cleanDist = "test/clean" // For ExampleCleanVersionFiles. Uses dummySrc as src.
func init() {
clean()
}
func ExampleFileVerPathReg() {
ts :=
`// test_1~fv=00000000.js
import * as test2 from './test_2~fv=00000000.js';
import * as test3 from './subdir/test_3~fv=00000000.js';
import * as test4 from './subdir/test_4~fv=00000000.js';
`
c := &Config{Src: dummySrc, Dist: dummyDist}
genSrcReg(c)
matches := c.SrcReg.FindAllString(ts, -1)
fmt.Println(matches)
// Output:
//[test_1~fv=00000000.js /test_2~fv=00000000.js /subdir/test_3~fv=00000000.js /subdir/test_4~fv=00000000.js]
}
// Example VersionReplace with mid version with "dummy" input files.
// go test -run '^ExampleVersionReplace$'
func ExampleVersionReplace() {
c := &Config{Src: dummySrc, Dist: dummyDist}
err := VersionReplace(c)
if err != nil {
panic(err)
}
PrintPretty(c)
PrintFile(dummyDist + "/" + c.Info.VersionedFiles[0])
// Output:
// ***WARNING*** Digest empty or too small for test_3.js
// {
// "Src": "test/dummy/src",
// "SrcFiles": [
// "subdir/test_3~fv=00000000.js",
// "subdir/test_4~fv=00000000.js",
// "test_1~fv=00000000.js",
// "test_2~fv=00000000.js"
// ],
// "SrcReg": {},
// "Dist": "test/dummy/dist",
// "UseSAVR": false,
// "Info": {
// "PV": {
// "subdir/test_3.js": "_X83uO__",
// "subdir/test_4.js": "GJIrg6k1",
// "test_1.js": "vPCb4GVO",
// "test_2.js": "BOl7h9TM"
// },
// "SAVR": "",
// "VersionedFiles": [
// "subdir/test_3~fv=_X83uO__.js",
// "subdir/test_4~fv=GJIrg6k1.js",
// "test_1~fv=vPCb4GVO.js",
// "test_2~fv=BOl7h9TM.js"
// ],
// "Index": null,
// "TotalSourceReplaces": 15,
// "CheckedFilePaths": null,
// "UpdatedFilePaths": [
// "test/dummy/dist/subdir/test_3~fv=_X83uO__.js",
// "test/dummy/dist/subdir/test_4~fv=GJIrg6k1.js",
// "test/dummy/dist/test_1~fv=vPCb4GVO.js",
// "test/dummy/dist/test_2~fv=BOl7h9TM.js"
// ]
// }
// }
// File test/dummy/dist/subdir/test_3~fv=_X83uO__.js:
// ////////////////
// import * as test1 from '../test_1~fv=vPCb4GVO.js';
// import * as test2 from '../test_2~fv=BOl7h9TM.js';
// import * as test4 from '../subdir/test_4~fv=GJIrg6k1.js';
// ////////////////
}
// // TestVersionReplace VersionReplace with mid version with "dummy" input files.
// func TestVersionReplace(t *testing.T) {
// c := &Config{Src: dummySrc, Dist: dummyDist}
// err := VersionReplace(c)
// if err != nil {
// panic(err)
// }
// PrintPretty(c)
// PrintFile(dummyDist + "/" + c.Info.VersionedFiles[0])
// }
// Example_watchVersionAndReplace demonstrates using FileVer with the external
// program "watch". Uses the "mid version" format.
func Example_watchVersionAndReplace() {
// Set up and test with Watch. Normally (outside of testing) watch must call
// filever. For testing, filever will call watch so that `go test` works.
// Also see notes in `watch_src.sh`
watchmod.ParseFlags()
watchmod.FC.Daemon = false
watchmod.FC.ConfigPath = "test/watch.json5"
watchmod.Run()
// Normal FileVer setup.
c := &Config{Src: watchSrc, Dist: watchDist}
err := VersionReplace(c)
if err != nil {
panic(err)
}
PrintPretty(c)
PrintFile(watchDist + "/" + c.Info.VersionedFiles[0])
// Output:
// Flag `daemon` set to false. Running commands in config and exiting.
// Replace Config &{Src:test/watch/src SrcFiles:[subdir/test_3~fv=00000000.js subdir/test_4~fv=00000000.js test_1~fv=00000000.min.js test_2~fv=00000000.js] SrcReg:<nil> Dist:test/watch/dist UseSAVR:false Info:0xc0001dc5b0} Info: &{PV:map[subdir/test_3.js:gia0-_Z_ subdir/test_4.js:da1EKBXZ test_1.min.js:1sTEzePc test_2.js:qBbNrrTr] SAVR: VersionedFiles:[subdir/test_3~fv=gia0-_Z_.js subdir/test_4~fv=da1EKBXZ.js test_1~fv=1sTEzePc.min.js test_2~fv=qBbNrrTr.js] Index:map[] TotalSourceReplaces:0 UpdatedFilePaths:[] CurrentPath: CurrentMatches:0}
// ***WARNING*** Digest empty for test_3.js
// ***WARNING*** Digest empty for test_1.js
// {
// "Src": "test/watch/src",
// "SrcFiles": [
// "subdir/test_3~fv=00000000.js",
// "subdir/test_4~fv=00000000.js",
// "test_1~fv=00000000.min.js",
// "test_2~fv=00000000.js"
// ],
// "SrcReg": {},
// "Dist": "test/watch/dist",
// "UseSAVR": false,
// "Info": {
// "PV": {
// "subdir/test_3.js": "gia0-_Z_",
// "subdir/test_4.js": "da1EKBXZ",
// "test_1.min.js": "1sTEzePc",
// "test_2.js": "qBbNrrTr"
// },
// "SAVR": "",
// "VersionedFiles": [
// "subdir/test_3~fv=gia0-_Z_.js",
// "subdir/test_4~fv=da1EKBXZ.js",
// "test_1~fv=1sTEzePc.min.js",
// "test_2~fv=qBbNrrTr.js"
// ],
// "Index": null,
// "TotalSourceReplaces": 20,
// "UpdatedFilePaths": [
// "test/watch/dist/subdir/test_3~fv=gia0-_Z_.js",
// "test/watch/dist/subdir/test_4~fv=da1EKBXZ.js",
// "test/watch/dist/test_1.min.js.map",
// "test/watch/dist/test_1~fv=1sTEzePc.min.js",
// "test/watch/dist/test_2~fv=qBbNrrTr.js"
// ]
// }
// }
// File test/watch/dist/subdir/test_3~fv=gia0-_Z_.js:
// ////////////////
// import * as test1 from '../test_1~fv=1sTEzePc.min.js';
// import * as test2 from '../test_2~fv=qBbNrrTr.js';
// import * as test4 from '../subdir/test_4~fv=da1EKBXZ.js';
// // Comments referring to './test_1~fv=1sTEzePc.min.js' should be updated as well.
// ////////////////
}
// Example_noDummy demonstrates inputting "manually" enumerated files to be
// processes by FileVer, aka it does not use dummy file inputs. Does not do
// Replace().
func Example_noDummy() {
c := &Config{
Src: dummyNoSrc,
Dist: dummyNoDist,
SrcFiles: []string{
"test_1.js",
"test_2.js",
"subdir/test_3.js",
"subdir/test_4.js",
},
}
Version(c)
// TODO Replace does not appear to be working.
fmt.Println(c.Info.VersionedFiles)
// PrintPretty(c.Info)
// PrintFile(dummyNoDist + "/" + c.Info.VersionedFiles[0])
// Output:
// [test_1~fv=qlJgGoFM.js test_2~fv=8RBMUSqr.js subdir/test_3~fv=_X83uO__.js subdir/test_4~fv=2lfgwhXI.js]
}
// func Test_noDummy(t *testing.T) {
// c := &Config{
// Src: dummyNoSrc,
// Dist: dummyNoDist,
// SrcFiles: []string{
// "test_1.js",
// "test_2.js",
// "subdir/test_3.js",
// "subdir/test_4.js",
// },
// }
// Version(c)
// // TODO Replace does not appear to be working.
// fmt.Println(c.Info.VersionedFiles)
// }
func ExampleListFilesInPath() {
f, err := ListFilesInPath(dummyNoSrc)
if err != nil {
panic(err)
}
fmt.Println(f)
// Output:
// [test_1.js test_2.js unversioned_file.md]
}
func ExampleExistingVersionedFiles() {
// Mid Version format
files, err := ExistingVersionedFiles(dummySrc)
if err != nil {
panic(err)
}
fmt.Println(files)
// Output:
// [subdir/test_3~fv=00000000.js subdir/test_4~fv=00000000.js test_1~fv=00000000.js test_2~fv=00000000.js]
}
func ExampleCleanVersionFiles() {
// Generate test files.
c := &Config{Src: dummySrc, Dist: cleanDist}
err := VersionReplace(c)
if err != nil {
panic(err)
}
// Clean out generate test files.
CleanVersionFiles(cleanDist)
f, err := ListFilesInPath(cleanDist)
if err != nil {
panic(err)
}
fmt.Println(c.Info.VersionedFiles)
fmt.Println(f)
// Output:
// ***WARNING*** Digest empty or too small for test_3.js
// [subdir/test_3~fv=_X83uO__.js subdir/test_4~fv=GJIrg6k1.js test_1~fv=vPCb4GVO.js test_2~fv=BOl7h9TM.js]
// [not_versioned_example.txt]
}
func Test_clean(t *testing.T) {
clean()
}
// Clean removes versioned files from dist.
func clean() {
c := []string{
dummyDist,
dummyNoDist,
watchDist,
cleanDist,
}
for _, v := range c {
CleanVersionFiles(v)
}
}
func Test__nuke(t *testing.T) {
nukeAndRebuildTestDirs()
}
// Completely delete all test dirs and recreate.
func nukeAndRebuildTestDirs() {
c := []string{
dummyDist,
dummyNoDist,
watchDist,
cleanDist,
}
for _, v := range c {
// Recreate the dir via remove and create.
err := os.RemoveAll(v)
if err != nil {
panic(err)
}
err = os.Mkdir(v, 0777)
if err != nil {
panic(err)
}
d1 := []byte("This example file exists in `dist` directory and is not versioned.")
err = os.WriteFile(v+"/not_versioned_example.txt", d1, 0644)
if err != nil {
panic(err)
}
}
}
// PrintFile is a helper function.
func PrintFile(filePath string) {
// Print out the first file and verify contents
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer func() {
if err = file.Close(); err != nil {
panic(err)
}
}()
b, err := ioutil.ReadAll(file)
fmt.Printf("File %s:\n////////////////\n%s\n////////////////\n", filePath, b)
}
func PrintPretty(s any) {
json, err := json.MarshalIndent(s, "", "\t")
if err != nil {
panic(err)
}
fmt.Println(string(json))
}