2019-03-02 15:19:08 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
var program = require('commander');
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var fs = require('fs');
|
2019-03-03 22:51:52 +00:00
|
|
|
var ListSuites = require('./utils/ListSuites');
|
2019-03-02 15:19:08 +00:00
|
|
|
|
|
|
|
let suite;
|
|
|
|
|
|
|
|
program
|
|
|
|
.description('Run the tests for the current suite or the provided one.')
|
|
|
|
.arguments('[suite]')
|
|
|
|
.option('--headless', 'Run in headless mode.')
|
2019-03-03 22:51:52 +00:00
|
|
|
.option('--forbid-only', 'Forbid only and pending filters.')
|
2019-03-02 15:19:08 +00:00
|
|
|
.action((suiteArg) => suite = suiteArg)
|
|
|
|
.parse(process.argv);
|
|
|
|
|
2019-03-03 22:51:52 +00:00
|
|
|
async function runTests(suite, withEnv) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let mochaArgs = ['--exit', '--colors', '--require', 'ts-node/register', 'test/suites/' + suite + '/test.ts']
|
|
|
|
if (program.forbidOnly) {
|
|
|
|
mochaArgs = ['--forbid-only', '--forbid-pending'] + mochaArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mochaCommand = './node_modules/.bin/mocha ' + mochaArgs.join(' ');
|
|
|
|
let testProcess;
|
|
|
|
if (!withEnv) {
|
|
|
|
testProcess = spawn(mochaCommand, {
|
|
|
|
shell: true,
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
TS_NODE_PROJECT: 'test/tsconfig.json',
|
|
|
|
HEADLESS: (program.headless) ? 'y' : 'n'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
testProcess = spawn('./node_modules/.bin/ts-node',
|
|
|
|
['-P', 'test/tsconfig.json', '--', './scripts/run-environment.ts', suite, mochaCommand], {
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
TS_NODE_PROJECT: 'test/tsconfig.json',
|
|
|
|
HEADLESS: (program.headless) ? 'y' : 'n'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
testProcess.stdout.pipe(process.stdout);
|
|
|
|
testProcess.stderr.pipe(process.stderr);
|
|
|
|
|
|
|
|
testProcess.on('exit', function(statusCode) {
|
|
|
|
if (statusCode == 0) resolve();
|
|
|
|
reject(new Error('Tests exited with status ' + statusCode));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runAllTests() {
|
|
|
|
const suites = ListSuites();
|
|
|
|
let failure = false;
|
|
|
|
for(var s in suites) {
|
|
|
|
try {
|
|
|
|
console.log('Running suite %s', suites[s]);
|
|
|
|
await runTests(suites[s], true);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
failure = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (failure) {
|
|
|
|
throw new Error('Some tests failed.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 15:19:08 +00:00
|
|
|
const ENVIRONMENT_FILENAME = '.suite'; // This file is created by the start command.
|
2019-03-03 22:51:52 +00:00
|
|
|
const suiteFileExists = fs.existsSync(ENVIRONMENT_FILENAME);
|
2019-03-02 15:19:08 +00:00
|
|
|
|
|
|
|
if (suite) {
|
2019-03-03 22:51:52 +00:00
|
|
|
if (suiteFileExists && suite != fs.readFileSync(ENVIRONMENT_FILENAME)) {
|
2019-03-02 15:19:08 +00:00
|
|
|
console.error('You cannot test a suite while another one is running. If you want to test the current suite, ' +
|
|
|
|
'do not provide the suite argument. Otherwise, stop the current suite and run the command again.');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
console.log('Suite %s provided. Running test related to this suite against built version of Authelia.', suite);
|
2019-03-03 22:51:52 +00:00
|
|
|
runTests(suite, !suiteFileExists)
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
2019-03-02 15:19:08 +00:00
|
|
|
}
|
2019-03-03 22:51:52 +00:00
|
|
|
else if (suiteFileExists) {
|
2019-03-02 15:19:08 +00:00
|
|
|
suite = fs.readFileSync(ENVIRONMENT_FILENAME);
|
|
|
|
console.log('Suite %s detected from dev env. Running test related to this suite.', suite);
|
2019-03-03 22:51:52 +00:00
|
|
|
runTests(suite, false)
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
2019-03-02 15:19:08 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('No suite provided therefore all suites will be tested.');
|
2019-03-03 22:51:52 +00:00
|
|
|
runAllTests(suite)
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
2019-03-02 15:19:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 22:51:52 +00:00
|
|
|
// Sometime SIGINT is received twice, we make sure with this variable that we cleanup
|
|
|
|
// only once.
|
|
|
|
var alreadyCleaningUp = false;
|
|
|
|
|
|
|
|
// Properly cleanup server and client if ctrl-c is hit
|
|
|
|
process.on('SIGINT', function() {
|
|
|
|
if (alreadyCleaningUp) return;
|
|
|
|
alreadyCleaningUp = true;
|
|
|
|
console.log('Cleanup procedure is running...');
|
2019-03-02 15:19:08 +00:00
|
|
|
});
|
|
|
|
|