/
replace.spec.test.js
474 lines (375 loc) · 14.2 KB
/
replace.spec.test.js
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
const tap = require('tap')
const tmp = require('tmp-promise')
const fs = require('fs')
const path = require('path')
const glob = require('fast-glob')
const replace = require('../index.js')
const tmpPrefixes = {
input: 'frs-replace-replace-in',
output: 'frs-replace-replace-out'
}
const content = `aąbcćdeęfg%hi
jklmn
oópqr,stuvwxyZ`
const needle = /^[adjox]/gm
const replacement = 'ą|'
const replaceFn = () => replacement
const defaults = {
inputReadOptions: 'utf8',
outputWriteOptions: 'utf8',
outputJoinString: '\n'
}
let output, dir
let input, input2
const cleanInputs = (done) => {
input2 && input2.cleanup()
input2 = undefined
input && input.cleanup()
input = undefined
done && done() // to be runned either by node-tap or manually
}
const createInputs = async () => {
await tmp.file({ prefix: tmpPrefixes.input, keep: true, dir })
.then(
async f => {
input = f
return new Promise(
(resolve) => fs.appendFile(f.path, content, { encoding: defaults.inputReadOptions }, resolve)
)
})
await tmp.file({ prefix: tmpPrefixes.input, keep: true, dir })
.then(
async f => {
input2 = f
return new Promise(
(resolve) => fs.appendFile(f.path, content, { encoding: defaults.inputReadOptions }, resolve)
)
})
}
{ // removing all files similar our tmp
const dirObj = tmp.dirSync()
dir = dirObj.name
glob.sync(
[
path.join(dir, tmpPrefixes.input),
path.join(dir, tmpPrefixes.output)
].map(v => v + '*')
)
.forEach(fs.unlinkSync)
}
tap.Test.prototype.addAssert('arrayContaining', 2, arrayContainingAssert)
tap.Test.prototype.addAssert('throwsMessageObj', 2, throwsMessageObjAssert)
tap.afterEach((done) => {
fs.existsSync(output) && (fs.lstatSync(output).isDirectory()
? fs.rmdirSync(output)
: fs.unlinkSync(output))
done()
})
tap.test('check required fields', async t => {
await t.throwsMessageObj({}, 'frs-replace :: at least one input source must be defined!', 'if both stdin & input arguments missing')
t.end()
})
tap.test('check api', async t => {
let expectedOutput
let testInput
t.beforeEach(async () => {
testInput = {
needle,
replacement
}
expectedOutput = [['', content.replace(needle, replacement)]]
})
await t.test('content', async t => {
testInput.content = content
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly'])
await t.test('with strategy = "flatten"', async t => {
testInput.content = content
testInput.strategy = 'flatten'
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly'])
t.end()
})
await t.test('strategy = "preserve-structure"', async t => {
testInput.content = content
testInput.strategy = 'flatten'
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly'])
t.end()
})
t.end()
})
await t.test('input', async t => {
t.beforeEach(async () => {
cleanInputs()
await createInputs()
})
t.afterEach(cleanInputs)
await t.test('as single file path', async t => {
testInput.input = input.path
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly'])
await t.test('with inputReadOptions as object', async t => {
testInput.input = input.path
testInput.inputReadOptions = { encoding: defaults.inputReadOptions }
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly'])
t.end()
})
t.end()
})
await t.test('as array of file paths', async t => {
testInput.input = [input.path, input2.path]
await checkSyncAsync(t, 'looseEqual', [testInput, [[
'',
expectedOutput[0][1] + defaults.outputJoinString + expectedOutput[0][1]
]], 'replaced correctly'])
await t.test('with outputJoinString changed', async t => {
testInput.input = [input.path, input2.path]
testInput.outputJoinString = 'someCustomString\n\t'
await checkSyncAsync(t, 'looseEqual', [testInput, [[
'',
expectedOutput[0][1] + testInput.outputJoinString + expectedOutput[0][1]
]], 'replaced correctly'])
t.end()
})
await t.test('with strategy = "flatten"', async t => {
testInput.input = [input.path, input2.path]
testInput.strategy = 'flatten'
await checkSyncAsync(t, 'looseEqual', [testInput, [
[input.path.substring(input.path.lastIndexOf('/')), expectedOutput[0][1]],
[input2.path.substring(input.path.lastIndexOf('/')), expectedOutput[0][1]]
], 'replaced correctly with proper filepaths'])
t.end()
})
await t.test('with strategy = "preserve-structure"', async t => {
testInput.input = [input.path, input2.path]
testInput.strategy = 'preserve-structure'
await checkSyncAsync(t, 'looseEqual', [testInput, [
[input.path, expectedOutput[0][1]],
[input2.path, expectedOutput[0][1]]
], 'replaced correctly with proper filepaths'])
t.end()
})
t.end()
})
await t.test('as glob pattern', async t => {
testInput.input = `${dir}/${tmpPrefixes.input}*`
await checkSyncAsync(t, 'looseEqual', [testInput, [[
'',
expectedOutput[0][1] + defaults.outputJoinString + expectedOutput[0][1]
]], 'replaced correctly'])
await t.test('with inputGlobOptions', async t => {
testInput.input = `${dir}/${tmpPrefixes.input}*`
testInput.inputGlobOptions = { onlyDirectories: true }
await checkSyncAsync(t, 'looseEqual', [testInput, [['', '']], 'replaced correctly'])
t.end()
})
await t.test('with strategy = "flatten"', async t => {
const message = 'replaced correctly with proper filepaths'
testInput.input = `${dir}/${tmpPrefixes.input}*`
testInput.strategy = 'flatten'
expectedOutput = [
[input.path.substring(input.path.lastIndexOf(path.sep)), expectedOutput[0][1]],
[input2.path.substring(input.path.lastIndexOf(path.sep)), expectedOutput[0][1]]
]
t.arrayContaining(await Promise.all(await replace.async(testInput)), expectedOutput, `async :: ${message}`)
t.arrayContaining(replace.sync(testInput), expectedOutput, `sync :: ${message}`)
t.end()
})
await t.test('with strategy = "preserve-structure"', async t => {
const message = 'replaced correctly with proper filepaths'
testInput.input = `${dir}/${tmpPrefixes.input}*`
testInput.strategy = 'preserve-structure'
expectedOutput = [
[input.path, expectedOutput[0][1]],
[input2.path, expectedOutput[0][1]]
]
t.arrayContaining(await Promise.all(await replace.async(testInput)), expectedOutput, `async :: ${message}`)
t.arrayContaining(replace.sync(testInput), expectedOutput, `sync :: ${message}`)
t.end()
})
t.end()
})
t.end()
})
await t.test('strategy', async t => {
await t.test('if unsupported strategy used', async t => {
await t.throwsMessageObj({ content: 'qwe', strategy: 'whatever' }, 'frs-replace :: unsupported strategy used! Possible values are: "join", "preserve-structure" or "flatten"')
t.end()
})
t.end()
})
await t.test('output', async t => {
testInput.content = content
output = testInput.output = tmp.tmpNameSync({ prefix: tmpPrefixes.output, dir })
expectedOutput = [[
output,
expectedOutput[0][1]
]]
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly'])
t.ok(fs.existsSync(testInput.output), 'output file exists')
const outputFileContent = fs.readFileSync(testInput.output).toString()
t.is(outputFileContent, expectedOutput[0][1], 'expected output saved to file')
t.beforeEach(async () => {
cleanInputs()
await createInputs()
})
t.afterEach(cleanInputs)
await t.test('with strategy = "flatten"', async t => {
testInput.input = [input.path, input2.path]
output = testInput.output = path.join(dir, tmpPrefixes.output, 'flatten')
testInput.strategy = 'flatten'
expectedOutput = [
[
path.join(dir, tmpPrefixes.output, 'flatten') + input.path.substring(input.path.lastIndexOf('/')),
expectedOutput[0][1]
],
[
path.join(dir, tmpPrefixes.output, 'flatten') + input2.path.substring(input.path.lastIndexOf('/')),
expectedOutput[0][1]
]
]
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly with proper filepaths'])
t.ok(fs.existsSync(expectedOutput[0][0]), 'output file exists')
t.ok(fs.existsSync(expectedOutput[1][0]), 'output file 2 exists')
fs.unlinkSync(expectedOutput[0][0])
fs.unlinkSync(expectedOutput[1][0])
t.end()
})
await t.test('with strategy = "preserve-structure"', async t => {
testInput.input = [input.path, input2.path]
output = testInput.output = path.join(dir, tmpPrefixes.output, 'preserve-structure')
testInput.strategy = 'preserve-structure'
expectedOutput = [
[
path.join(dir, tmpPrefixes.output, 'preserve-structure', input.path),
expectedOutput[0][1]
],
[
path.join(dir, tmpPrefixes.output, 'preserve-structure', input2.path),
expectedOutput[0][1]
]
]
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly with proper filepaths'])
t.ok(fs.existsSync(expectedOutput[0][0]), 'output file exists')
t.ok(fs.existsSync(expectedOutput[1][0]), 'output file 2 exists')
fs.unlinkSync(expectedOutput[0][0])
fs.unlinkSync(expectedOutput[1][0])
deleteFolderRecursive(output)
t.end()
})
t.end()
})
await t.test('outputWriteOptions as object', async t => {
testInput.content = content
output = testInput.output = tmp.tmpNameSync({ prefix: tmpPrefixes.output, dir })
testInput.outputWriteOptions = { encoding: defaults.outputWriteOptions }
expectedOutput[0][0] = output
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly'])
t.ok(fs.existsSync(testInput.output), 'output file exists')
const outputFileContent = fs.readFileSync(testInput.output).toString()
t.is(outputFileContent, expectedOutput[0][1], 'expected output saved to file')
t.end()
})
await t.test('replacement as function', async t => {
testInput.content = content
output = testInput.output = tmp.tmpNameSync({ prefix: tmpPrefixes.output, dir })
testInput.replacement = replaceFn
expectedOutput[0] = [output, content.replace(needle, replaceFn)]
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly'])
t.ok(fs.existsSync(testInput.output), 'output file exists')
const outputFileContent = fs.readFileSync(testInput.output).toString()
t.is(outputFileContent, expectedOutput[0][1], 'expected output saved to file')
t.end()
})
await t.test('needle as string', async t => {
testInput.needle = 'a'
testInput.content = content
expectedOutput[0][1] = content.replace(testInput.needle, replacement)
await checkSyncAsync(t, 'looseEqual', [testInput, expectedOutput, 'replaced correctly'])
t.end()
})
t.end()
})
async function checkSyncAsync (t, method, argsSync, isFirstArgFn = false) {
if (!(argsSync[0] instanceof Array)) { argsSync[0] = [argsSync[0]] }
let messageIndex = argsSync.length - 1
while (typeof argsSync[messageIndex] !== 'string') { --messageIndex }
const argsAsync = [...argsSync]
argsAsync[0] = bindArray(replace.async, argsAsync[0])
argsSync[0] = bindArray(replace.sync, argsSync[0])
if (!isFirstArgFn) {
argsAsync[0] = await Promise.all(await argsAsync[0]())
argsSync[0] = argsSync[0]()
}
argsAsync[messageIndex] = `async :: ${argsAsync[messageIndex]}`
argsSync[messageIndex] = `sync :: ${argsSync[messageIndex]}`
await t[method].apply(t, argsAsync)
t[method].apply(t, argsSync)
}
function bindArray (fn, args) {
return () => fn.apply(fn, args)
}
function handleRejection (promise) {
promise.catch(() => {}) // to silent Node "PromiseRejectionHandledWarning:" error
return promise
}
function throwsMessageObjAssert (args, errorMessage, message) {
const error = { message: errorMessage }
this.throws(() => replace.sync(args), error, 'sync :: should throw ' + message)
return this.rejects(handleRejection(replace.async(args)), error, 'async :: should reject promise ' + message)
}
function arrayContainingAssert (array, equalArray, message, extra) {
message = message || 'should contain items'
let type;
[type, message] = arrayContaining(array, equalArray, message)
return this[type ? 'pass' : 'fail'](message, extra)
}
function arrayContaining (array, equalArray, message) {
if (!Array.isArray(array) || !Array.isArray(equalArray) || array.length < equalArray.length) {
return [false, message]
}
for (let i = 0; i < equalArray.length; ++i) {
const item = equalArray[i]
if (typeof item === 'string') {
if (!array.includes(item)) {
return [false,
`${message}
Expected:
${JSON.stringify(item)}
To be contained within:
${JSON.stringify(array)}
`
]
}
} else if (Array.isArray(item)) {
for (let j = 0; j < array.length; ++j) {
const arrayItem = array[j]
const [checkResult] = arrayContaining(arrayItem, item)
if (checkResult !== false) break
else if (j === array.length - 1) {
return [false,
`${message}
Expected:
${JSON.stringify(item)}
To be contained within:
${JSON.stringify(array)}
`
]
}
}
} else {
return [false, `${message} "arrayContainingAssert" unsupported type`]
}
}
return [true, message]
}
function deleteFolderRecursive (path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file) {
const curPath = path + '/' + file
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath)
} else { // delete file
fs.unlinkSync(curPath)
}
})
fs.rmdirSync(path)
}
};