-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
114 lines (101 loc) · 2.72 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict';
const {
ok,
strictEqual: eq,
deepStrictEqual: deq
} = require('assert');
const pifall = require('pifall');
const fs = pifall(require('fs'));
const { execAsync: exec } = pifall(require('child_process'));
const path = require('path');
const fsBinding = process.binding('fs');
const FSWrap = fsBinding.FSReqWrap || fsBinding.FSReqCallback;
const {
isdirAsync,
copyAsync,
mkdirAsync,
readdirAsync,
hasTypes,
Wrap,
cprAsync,
mkdirpAsync
} = pifall(require('./index.js'));
const test = require('pitesti')({ timeout: 60000 });
test`isdir`(async () => {
ok(await isdirAsync(__dirname));
ok(!(await isdirAsync(__filename)));
ok(!(await isdirAsync(path.join(__dirname, 'qafasdfasdf'))));
});
test`copy`(async () => {
const dst = path.join(__dirname, 'copytest');
await copyAsync(__filename, dst);
eq(
await fs.readFileAsync(__filename, 'utf8'),
await fs.readFileAsync(dst, 'utf8')
);
await fs.unlinkAsync(dst);
});
test`mkdir`(async () => {
const newDir = path.join(__dirname, 'testdir');
ok(!(await isdirAsync(newDir)));
await mkdirAsync(newDir);
ok(await isdirAsync(newDir));
await fs.rmdirAsync(newDir);
});
test`readdir`(async () => {
deq(
await fs.readdirAsync(__dirname, { withFileTypes: true }),
await readdirAsync(__dirname)
);
});
test`hasTypes`(async () => {
eq(
typeof (await readdirAsync(__dirname))[0] === 'object',
hasTypes
);
});
test`Wrap`(() => {
const fn = () => {};
const wrap = new Wrap(fn);
eq(wrap.oncomplete, fn);
ok(wrap instanceof FSWrap);
});
test`mkdirp`(async () => {
const newDir = path.join(__dirname, 'testdir1', 'testdir2', 'testdir3');
ok(!(await isdirAsync(newDir)));
await mkdirpAsync(newDir);
ok(await isdirAsync(newDir));
await mkdirpAsync(newDir); // should work on already-made directories
ok(await isdirAsync(newDir));
await mkdirpAsync(newDir); // should work on existing directories
await exec(`rm -rf ${newDir}`);
});
test`cpr`(async () => {
const srcDir = path.join(__dirname, 'node_modules');
const destDir = '/tmp/qfastfsdestdir';
await exec(`rm -rf ${destDir}`);
const srcTree = await tree(srcDir);
await cprAsync(srcDir, destDir);
const destTree = await tree(destDir);
deq(srcTree, destTree);
await exec(`rm -rf ${destDir}`);
});
test();
async function tree (dir) {
const contents = await fs.readdirAsync(dir);
const result = {};
for (let i = 0; i < contents.length; i++) {
const item = contents[i];
if (item.startsWith('.')) {
continue;
}
const fullItem = dir + '/' + item;
const stats = await fs.statAsync(fullItem);
if (stats.isDirectory()) {
result[item] = await tree(fullItem);
} else {
result[item] = stats.size;
}
}
return result;
}