-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-utils.js
executable file
·109 lines (100 loc) · 3.1 KB
/
test-utils.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
const os = require('os');
const fsp = require('fs').promises;
const mkdir = fsp.mkdir;
const writeFile = fsp.writeFile;
const del = require('del');
const path = require('path');
const ulid = require('ulid').ulid;
const cuid = require('cuid');
const uuid = require('uuid');
const data = 'The quick brown fox jumps over the lazy dog';
module.exports.data = data;
module.exports.mkBlobDir = mkBlobDir;
async function mkBlobDir(blobRoot, ...dirs) {
const fullPath = path.join(blobRoot, ...dirs);
return mkdir(fullPath, { recursive: true });
}
module.exports.rmBlobDir = rmBlobDir;
async function rmBlobDir(blobRoot, ...parts) {
const fullPath = path.join(blobRoot, ...parts);
return fullPath.startsWith(os.tmpdir()) && del(fullPath, { force: true });
}
module.exports.genBlobStoreRoot = genBlobStoreRoot;
function genBlobStoreRoot(name) {
let rootPath = path.join(os.tmpdir(), 'blobs', name);
return rootPath;
}
module.exports.mkBlobFile = mkBlobFile;
async function mkBlobFile(blobRoot, ...pathPart) {
pathPart.length > 1 && (await mkBlobDir(blobRoot, ...pathPart.slice(0, pathPart.length - 1)));
let fullPath = path.join(blobRoot, ...pathPart);
return writeFile(fullPath, data);
}
module.exports.delay = delay;
function delay(ms) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
});
}
module.exports.generateUlids = generateUlids;
function generateUlids(total) {
const ulids = [];
for (let i = 0; i < total; i++) {
ulids.push(ulid());
}
return ulids;
}
module.exports.generateCuids = generateCuids;
function generateCuids(total) {
const cuids = [];
for (let i = 0; i < total; i++) {
cuids.push(cuid());
}
return cuids;
}
module.exports.generateUuids = generateUuids;
function generateUuids(total) {
const uuids = [];
for (let i = 0; i < total; i++) {
uuids.push(uuid.v4());
}
return uuids;
}
module.exports.buildTestFs = buildTestFs;
async function buildTestFs(blobStoreRoot) {
try {
await del(blobStoreRoot, { force: true });
await mkBlobFile(blobStoreRoot, '01a', '01b', '01c', '01d');
await delay(200);
await mkBlobFile(blobStoreRoot, '02a', '02b', '02c', '02d');
await delay(200);
await mkBlobFile(blobStoreRoot, '03a', '03b', '03c', '03d');
await delay(200);
await mkBlobFile(blobStoreRoot, '04a');
await delay(200);
await mkBlobFile(blobStoreRoot, '05a');
await delay(200);
await mkBlobFile(blobStoreRoot, '06a');
await delay(200);
await mkBlobFile(blobStoreRoot, '07a', '07b', '07c', '07d');
await delay(200);
await mkBlobFile(blobStoreRoot, '08a', '08b', '08c', '08d');
await delay(200);
await mkBlobFile(blobStoreRoot, '09a', '09b', '09c', '09d');
await delay(200);
await mkBlobFile(blobStoreRoot, '10a');
await delay(200);
await mkBlobFile(blobStoreRoot, '11a');
await delay(200);
await mkBlobFile(blobStoreRoot, '12a');
} catch (err) {
console.log('Error in buildTestFs ');
console.error(err);
}
return {
firstBlobDir: '/01a/01b/01c',
firstBlobPath: '/01a/01b/01c/01d',
latestBlobDir: '/09a/09b/09c',
latestBlobPath: '/09a/09b/09c/09d',
};
}