forked from jesec/flood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestsetup.js
71 lines (57 loc) · 1.99 KB
/
testsetup.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
const chalk = require('chalk');
const crypto = require('crypto');
const fs = require('fs');
const os = require('os');
const path = require('path');
const {spawn} = require('child_process');
const temporaryRuntimeDirectory = path.resolve(os.tmpdir(), `flood.test.${crypto.randomBytes(12).toString('hex')}`);
console.log(chalk.cyan(`Temporary runtime directory: ${temporaryRuntimeDirectory}\n`));
const rTorrentSession = path.join(temporaryRuntimeDirectory, '.session');
const rTorrentSocket = path.join(temporaryRuntimeDirectory, 'rtorrent.sock');
fs.mkdirSync(rTorrentSession, {recursive: true});
fs.writeFileSync(
`${temporaryRuntimeDirectory}/rtorrent.rc`,
`
execute.nothrow = rm,-rf,${rTorrentSession}/rtorrent.lock
directory.default.set = "${temporaryRuntimeDirectory}"
session.path.set = "${rTorrentSession}"
network.scgi.open_local = "${rTorrentSocket}"
`,
);
let argv = [];
argv.push('--rundir', temporaryRuntimeDirectory);
argv.push('--auth', 'none');
argv.push('--rtsocket', rTorrentSocket);
argv.push('--allowedpath', temporaryRuntimeDirectory);
argv.push('--rtorrent');
argv.push('--rtconfig', `${temporaryRuntimeDirectory}/rtorrent.rc`);
argv.push('--test');
argv = argv.concat(process.argv.slice(2));
let floodProcess;
const startFlood = () => {
if (floodProcess != null) {
return;
}
floodProcess = spawn('npm', ['run', 'start:development:server', '--'].concat(argv), {
cwd: path.join(__dirname, '..'),
stdio: 'inherit',
});
};
const closeProcesses = () => {
floodProcess.on('close', () => {
if (process.env.CI !== 'true') {
// TODO: This leads to test flakiness caused by ENOENT error
// NeDB provides no method to close database connection
fs.rmdirSync(temporaryRuntimeDirectory, {recursive: true});
}
});
floodProcess.kill('SIGTERM');
process.kill(Number(fs.readFileSync(`${temporaryRuntimeDirectory}/rtorrent.pid`).toString()));
};
startFlood();
process.on('SIGINT', () => {
process.exit();
});
process.on('exit', () => {
closeProcesses();
});