-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
94 lines (76 loc) · 2.26 KB
/
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
const path = require('path');
const fs = require('fs-extra');
const pathExists = require('path-exists');
const tempWrite = require('temp-write');
const tempfile = require('tempfile');
import test from 'ava';
import skrub from './';
function prependPath(t, files) {
return files.map(file => {
return path.join(t.context.tmp, file);
});
}
function exists(t, files) {
[].concat(files).forEach(file => t.true(pathExists.sync(path.join(t.context.tmp, file))));
}
function notExists(t, files) {
[].concat(files).forEach(file => t.false(pathExists.sync(path.join(t.context.tmp, file))));
}
function deArr(arr) {
if (arr.length === 1) {
return arr[0];
}
return arr;
}
const fixtures = [
'1.tmp',
'2.tmp',
'3.tmp',
'4.tmp',
'.dot.tmp'
];
test.beforeEach(t => {
t.context.tmp = tempfile();
fixtures.forEach(fixture => fs.ensureFileSync(path.join(t.context.tmp, fixture)));
});
test('skrub - invalid args', t => {
t.throws(() => {
skrub();
}, TypeError);
});
test('skrub.flood - invalid args', t => {
t.throws(skrub.floodFile('doesNotExist.txt'), Error);
t.throws(skrub.floodFile(false), Error);
});
test('skrub.flood - floods file', async t => {
const initialContents = '12345';
const file = tempWrite.sync(initialContents);
await skrub.floodFile(file)
.then(resp => {
t.is(deArr(resp), file);
});
const finalContents = fs.readFileSync(file);
t.true(pathExists.sync(file));
t.not(initialContents, finalContents);
t.is(initialContents.length, finalContents.length);
t.deepEqual(finalContents, Buffer.alloc(5));
});
test('skrub - dryrun does not remove files', async t => {
await skrub(['*.tmp', '!1*'], {
cwd: t.context.tmp,
dryRun: true
}).then(files => {
t.deepEqual(files, prependPath(t, ['2.tmp', '3.tmp', '4.tmp']));
});
exists(t, ['1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']);
});
test('skrub - removes files undefiend iterations', async t => {
await skrub(['*.tmp', '!1*'], {cwd: t.context.tmp});
exists(t, ['1.tmp', '.dot.tmp']);
notExists(t, ['2.tmp', '3.tmp', '4.tmp']);
});
test('skrub - removes files 1 iterations', async t => {
await skrub(['*.tmp', '!1*'], {iterations: 1, cwd: t.context.tmp});
exists(t, ['1.tmp', '.dot.tmp']);
notExists(t, ['2.tmp', '3.tmp', '4.tmp']);
});