Skip to content

Instantly share code, notes, and snippets.

@alber70g
Last active July 11, 2017 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alber70g/87fc4163a5e27f1d5d98cfa0953a161c to your computer and use it in GitHub Desktop.
Save alber70g/87fc4163a5e27f1d5d98cfa0953a161c to your computer and use it in GitHub Desktop.
Run all suites separately.
/**
* npm install lodash yargs shell
*/
const shell = require('./shell');
const _ = require('lodash');
const argv = require('yargs')
.usage('Usage: node $0 [options]')
.option('all', {
alias: 'a',
boolean: true,
describe: 'Run all test suites (ignores + and - prefixes)'
})
.option('wct', {
type: 'string',
describe: 'Provide wct arguments'
})
.example('node $0 --all --wct="--skip-selenium-install --persistent"', 'Runs all test stuites, without installing selenium and keeps browser open')
.help()
.alias('help', 'h')
.argv;
const wct = require('../wct.conf.js'); // Change this to your wct.conf.js
function startCommands(commands) {
const options = {
cwd: `${__dirname}/../` // Change this to your rootpath
};
// execute multiple commands in series
shell.series(
commands,
options,
err => {
console.log(err);
},
err => {
console.log('Finished ', commands.map(x => x + '\n'));
}
);
}
function removePlusMinus(suite) {
const splitter = /^[+|-]/;
if (suite.match(splitter)) {
return suite.split(splitter)[1];
}
return suite;
}
/**
* Returns active suites
* Rules:
* - If there's *one* starting with `+` it only returns suites with `+`
* - Otherwise it will run all except those starting with `-`
*/
function getActiveSuites(suites) {
const plusFound = _.find(suites, suite => suite.startsWith('+'));
if (plusFound) {
return _.filter(suites, suite => suite.startsWith('+')).map(removePlusMinus);
}
return _.filter(suites, suite => !suite.startsWith('-')).map(removePlusMinus);
}
function createCommands(suites, wctArguments) {
let commands = [];
let wct = wctArguments + ' ' || '';
suites.forEach(suite => {
commands.push(`wct ${wct}${suite}`);
});
return commands;
}
function launch(argv) {
let activeSuites = [];
if (argv.all) {
activeSuites = wct.suites.map(removePlusMinus);
} else {
activeSuites = getActiveSuites(wct.suites);
}
console.log('Suites to run: ');
activeSuites.map(suite => console.log(suite));
console.log('');
const commands = createCommands(activeSuites, argv.wct);
startCommands(commands);
}
launch(argv);
const exec = require('child_process').execSync;
// spawn a child process and execute shell command
// borrowed from https://github.com/mout/mout/ build script
// author Miller Medeiros
// released under MIT License
// version: 0.1.0 (2013/02/01)
var shell = {};
// execute a single shell command where "cmd" is a string
shell.exec = function(cmd, options, cb) {
options.stdio = 'inherit';
// this would be way easier on a shell/bash script :P
var child_process = require('child_process');
var parts = cmd.split(/\s+/g);
var p = child_process.spawn(parts[0], parts.slice(1), options);
p.on('exit', function(code) {
var err = null;
if (code) {
err = new Error('command "' + cmd + '" exited with wrong status code "' + code + '"');
err.code = code;
err.cmd = cmd;
}
if (cb) cb(err);
});
};
// execute multiple commands in series
// this could be replaced by any flow control lib
shell.series = function(cmds, options, cb, finalCb) {
var execNext = function() {
var elo = cmds.shift();
console.log('running', elo, Math.round(Math.random() * 1000 * 3), 'hell');
shell.exec(elo, options, function(err) {
if (err) {
cb(err);
execNext();
} else {
if (cmds.length) execNext();
else finalCb(null);
}
});
};
execNext();
};
module.exports = shell;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment