-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
93 lines (82 loc) · 2.55 KB
/
Copy pathtest.js
File metadata and controls
93 lines (82 loc) · 2.55 KB
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
'use strict';
const { deepStrictEqual: deq, ok } = require('assert');
const path = require('path');
const os = require('os');
const pifall = require('pifall');
const { qdownloadAsync: qdownload } = pifall({ qdownload: require('./index.js') });
const fs = pifall(require('fs'));
const { execAsync: exec } = pifall(require('child_process'));
const test = require('pitesti')({ timeout: 60000 });
const qfastfsUrl = 'https://registry.npmjs.org/qfastfs/-/qfastfs-1.0.0.tgz';
const qfastfsSha = 'sha512-4FySBpNMnrDULzycXH6JmCImjWRWUTzqrnnqhkrbTExcmcn2Zt' +
'A9ZftAY8qYiVFGGkLBfrozCjEzAY3sB2Y84Q==';
const qfastfsTree = {
'LICENSE.txt': [0o100644, 1080],
'README.md': [0o100644, 810],
'index.js': [0o100755, 202],
lib: {
'cp.js': [0o100644, 1838],
'general.js': [0o100644, 1383],
'mkdirp.js': [0o100644, 794],
'util.js': [0o100644, 184]
},
'package.json': [0o100644, 1004]
};
test`cache only`(async () => {
const cacheDir = await getTestDir();
await qdownload(qfastfsUrl, qfastfsSha, cacheDir, null);
const cacheTree = await tree(cacheDir);
deq(qfastfsTree, cacheTree);
await exec(`rm -rf ${cacheDir}`);
});
test`dest only`(async () => {
const destDir = await getTestDir();
await qdownload(qfastfsUrl, qfastfsSha, null, destDir);
const destTree = await tree(destDir);
deq(qfastfsTree, destTree);
await exec(`rm -rf ${destDir}`);
});
test`both`(async () => {
const cacheDir = await getTestDir();
const destDir = await getTestDir();
await qdownload(qfastfsUrl, qfastfsSha, cacheDir, destDir);
const destTree = await tree(destDir);
deq(qfastfsTree, destTree);
const cacheTree = await tree(cacheDir);
deq(qfastfsTree, cacheTree);
await exec(`rm -rf ${destDir}`);
await exec(`rm -rf ${cacheDir}`);
});
test`integrity fail`(async () => {
const cacheDir = await getTestDir();
let err;
try {
await qdownload(qfastfsUrl, 'sha1-ReN/s56No/JbruP/U2niu18iAXo=', cacheDir, null);
} catch (e) {
err = e;
}
ok(err);
await exec(`rm -rf ${cacheDir}`);
});
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.mode, stats.size];
}
}
return result;
}
async function getTestDir () {
return fs.mkdtempAsync(path.join(os.tmpdir(), 'qdownload-'));
}