From ef2db544be687f1cb737662632d38fc0b0b6049d Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Wed, 2 Dec 2020 20:29:17 +0800 Subject: [PATCH] scripts: a script to quickly fire a fresh Flood-rTorrent env npm run start:test:rtorrent --- package.json | 2 +- scripts/testsetup.js | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 scripts/testsetup.js diff --git a/package.json b/package.json index 923c2f25..9c77f33d 100644 --- a/package.json +++ b/package.json @@ -52,9 +52,9 @@ "lint": "NODE_ENV=development eslint --max-warnings 0 . --ext .js --ext .jsx --ext .ts --ext .tsx", "prepack": "rm -rf dist && npm run build", "start": "node --use_strict dist/index.js", - "start:development": "UPDATED_SCRIPT=start:development:server npm run deprecated-warning && npm run start:development:server", "start:development:client": "node client/scripts/start.js", "start:development:server": "NODE_ENV=development TS_NODE_PROJECT=server/tsconfig.json ts-node-dev --transpile-only server/bin/start.ts", + "start:test:rtorrent": "node scripts/testsetup.js", "test": "jest --forceExit", "test:watch": "jest --watchAll --forceExit", "test:client": "FLOOD_OPTION_port=4200 start-server-and-test start 4200 'cypress run'" diff --git a/scripts/testsetup.js b/scripts/testsetup.js new file mode 100644 index 00000000..aa86e4ae --- /dev/null +++ b/scripts/testsetup.js @@ -0,0 +1,60 @@ +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')}`); + +const rTorrentSession = path.join(temporaryRuntimeDirectory, '.session'); +const rTorrentSocket = path.join(temporaryRuntimeDirectory, 'rtorrent.sock'); + +fs.mkdirSync(rTorrentSession, {recursive: true}); + +fs.writeFileSync( + `${temporaryRuntimeDirectory}/rtorrent.rc`, + ` +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(`${path.join(__dirname, '../dist/index.js')}`, argv, {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('exit', () => { + closeProcesses(); +});