Skip to content

Commit

Permalink
Upgrade XO (#1080)
Browse files Browse the repository at this point in the history
* Avoid running XO on older Node.js versions

* xo@^0.17.0

* Split up cli.js

Move the actual implementation into lib/cli.js to avoid top-level
returns. xo@^0.17.0 cannot parse the file with those returns present.

Throw errors from lib/cli.js and write them to stderr in cli.js before
exiting. XO doesn't appreciate process.exit() calls in modules.

Note that the error for invalid Babel config is now written to stderr,
which seems like an improvement to me.

* Actually register loud-rejection
  • Loading branch information
novemberborn authored and sindresorhus committed Oct 23, 2016
1 parent 3ea2ba1 commit 257b34d
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 199 deletions.
185 changes: 7 additions & 178 deletions cli.js
Expand Up @@ -13,187 +13,16 @@ var localCLI = resolveCwd('ava/cli');
// see https://github.com/nodejs/node/issues/6624
if (localCLI && path.relative(localCLI, __filename) !== '') {
debug('Using local install of AVA');
require(localCLI);
return;
}

if (debug.enabled) {
require('time-require');
}

var updateNotifier = require('update-notifier');
var figures = require('figures');
var arrify = require('arrify');
var meow = require('meow');
var Promise = require('bluebird');
var pkgConf = require('pkg-conf');
var isCi = require('is-ci');
var hasFlag = require('has-flag');
var colors = require('./lib/colors');
var verboseReporter = require('./lib/reporters/verbose');
var miniReporter = require('./lib/reporters/mini');
var tapReporter = require('./lib/reporters/tap');
var Logger = require('./lib/logger');
var Watcher = require('./lib/watcher');
var babelConfig = require('./lib/babel-config');
var Api = require('./api');

// Bluebird specific
Promise.longStackTraces();

var conf = pkgConf.sync('ava');

var pkgDir = path.dirname(pkgConf.filepath(conf));

try {
conf.babel = babelConfig.validate(conf.babel);
} catch (err) {
console.log('\n ' + err.message);
process.exit(1);
}

var cli = meow([
'Usage',
' ava [<file|directory|glob> ...]',
'',
'Options',
' --init Add AVA to your project',
' --fail-fast Stop after first test failure',
' --serial, -s Run tests serially',
' --tap, -t Generate TAP output',
' --verbose, -v Enable verbose output',
' --no-cache Disable the transpiler cache',
' --no-power-assert Disable Power Assert',
' --match, -m Only run tests with matching title (Can be repeated)',
' --watch, -w Re-run tests when tests and source files change',
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)',
' --timeout, -T Set global timeout',
' --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)',
'',
'Examples',
' ava',
' ava test.js test2.js',
' ava test-*.js',
' ava test',
' ava --init',
' ava --init foo.js',
'',
'Default patterns when no arguments:',
'test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js'
], {
string: [
'_',
'timeout',
'source',
'match',
'concurrency'
],
boolean: [
'fail-fast',
'verbose',
'serial',
'tap',
'watch'
],
default: conf,
alias: {
t: 'tap',
v: 'verbose',
s: 'serial',
m: 'match',
w: 'watch',
S: 'source',
T: 'timeout',
c: 'concurrency'
}
});

updateNotifier({pkg: cli.pkg}).notify();

if (cli.flags.init) {
require('ava-init')();
return;
}

if (
((hasFlag('--watch') || hasFlag('-w')) && (hasFlag('--tap') || hasFlag('-t'))) ||
(conf.watch && conf.tap)
) {
console.error(' ' + colors.error(figures.cross) + ' The TAP reporter is not available when using watch mode.');
process.exit(1);
}

if (hasFlag('--require') || hasFlag('-r')) {
console.error(' ' + colors.error(figures.cross) + ' The --require and -r flags are deprecated. Requirements should be configured in package.json - see documentation.');
process.exit(1);
}

var api = new Api({
failFast: cli.flags.failFast,
serial: cli.flags.serial,
require: arrify(conf.require),
cacheEnabled: cli.flags.cache !== false,
powerAssert: cli.flags.powerAssert !== false,
explicitTitles: cli.flags.watch,
match: arrify(cli.flags.match),
babelConfig: conf.babel,
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(),
pkgDir: pkgDir,
timeout: cli.flags.timeout,
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0
});

var reporter;

if (cli.flags.tap && !cli.flags.watch) {
reporter = tapReporter();
} else if (cli.flags.verbose || isCi) {
reporter = verboseReporter();
require(localCLI); // eslint-disable-line import/no-dynamic-require
} else {
reporter = miniReporter({watching: cli.flags.watch});
}

reporter.api = api;
var logger = new Logger(reporter);

logger.start();

api.on('test-run', function (runStatus) {
reporter.api = runStatus;
runStatus.on('test', logger.test);
runStatus.on('error', logger.unhandledError);

runStatus.on('stdout', logger.stdout);
runStatus.on('stderr', logger.stderr);
});

var files = cli.input.length ? cli.input : arrify(conf.files);
if (debug.enabled) {
require('time-require'); // eslint-disable-line import/no-unassigned-import
}

if (cli.flags.watch) {
try {
var watcher = new Watcher(logger, api, files, arrify(cli.flags.source));
watcher.observeStdin(process.stdin);
require('./lib/cli').run();
} catch (err) {
if (err.name === 'AvaError') {
// An AvaError may be thrown if chokidar is not installed. Log it nicely.
console.error(' ' + colors.error(figures.cross) + ' ' + err.message);
logger.exit(1);
} else {
// Rethrow so it becomes an uncaught exception.
throw err;
}
console.error('\n ' + err.message);
process.exit(1);
}
} else {
api.run(files)
.then(function (runStatus) {
logger.finish(runStatus);
logger.exit(runStatus.failCount > 0 || runStatus.rejectionCount > 0 || runStatus.exceptionCount > 0 ? 1 : 0);
})
.catch(function (err) {
// Don't swallow exceptions. Note that any expected error should already
// have been logged.
setImmediate(function () {
throw err;
});
});
}
170 changes: 170 additions & 0 deletions lib/cli.js
@@ -0,0 +1,170 @@
'use strict';
var path = require('path');
var updateNotifier = require('update-notifier');
var figures = require('figures');
var arrify = require('arrify');
var meow = require('meow');
var Promise = require('bluebird');
var pkgConf = require('pkg-conf');
var isCi = require('is-ci');
var hasFlag = require('has-flag');
var Api = require('../api');
var colors = require('./colors');
var verboseReporter = require('./reporters/verbose');
var miniReporter = require('./reporters/mini');
var tapReporter = require('./reporters/tap');
var Logger = require('./logger');
var Watcher = require('./watcher');
var babelConfig = require('./babel-config');

// Bluebird specific
Promise.longStackTraces();

exports.run = function () {
var conf = pkgConf.sync('ava');
var pkgDir = path.dirname(pkgConf.filepath(conf));

var cli = meow([
'Usage',
' ava [<file|directory|glob> ...]',
'',
'Options',
' --init Add AVA to your project',
' --fail-fast Stop after first test failure',
' --serial, -s Run tests serially',
' --tap, -t Generate TAP output',
' --verbose, -v Enable verbose output',
' --no-cache Disable the transpiler cache',
' --no-power-assert Disable Power Assert',
' --match, -m Only run tests with matching title (Can be repeated)',
' --watch, -w Re-run tests when tests and source files change',
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)',
' --timeout, -T Set global timeout',
' --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)',
'',
'Examples',
' ava',
' ava test.js test2.js',
' ava test-*.js',
' ava test',
' ava --init',
' ava --init foo.js',
'',
'Default patterns when no arguments:',
'test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js'
], {
string: [
'_',
'timeout',
'source',
'match',
'concurrency'
],
boolean: [
'fail-fast',
'verbose',
'serial',
'tap',
'watch'
],
default: conf,
alias: {
t: 'tap',
v: 'verbose',
s: 'serial',
m: 'match',
w: 'watch',
S: 'source',
T: 'timeout',
c: 'concurrency'
}
});

updateNotifier({pkg: cli.pkg}).notify();

if (cli.flags.init) {
require('ava-init')();
return;
}

if (
((hasFlag('--watch') || hasFlag('-w')) && (hasFlag('--tap') || hasFlag('-t'))) ||
(conf.watch && conf.tap)
) {
throw new Error(colors.error(figures.cross) + ' The TAP reporter is not available when using watch mode.');
}

if (hasFlag('--require') || hasFlag('-r')) {
throw new Error(colors.error(figures.cross) + ' The --require and -r flags are deprecated. Requirements should be configured in package.json - see documentation.');
}

var api = new Api({
failFast: cli.flags.failFast,
serial: cli.flags.serial,
require: arrify(conf.require),
cacheEnabled: cli.flags.cache !== false,
powerAssert: cli.flags.powerAssert !== false,
explicitTitles: cli.flags.watch,
match: arrify(cli.flags.match),
babelConfig: babelConfig.validate(conf.babel),
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(),
pkgDir: pkgDir,
timeout: cli.flags.timeout,
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0
});

var reporter;

if (cli.flags.tap && !cli.flags.watch) {
reporter = tapReporter();
} else if (cli.flags.verbose || isCi) {
reporter = verboseReporter();
} else {
reporter = miniReporter({watching: cli.flags.watch});
}

reporter.api = api;
var logger = new Logger(reporter);

logger.start();

api.on('test-run', function (runStatus) {
reporter.api = runStatus;
runStatus.on('test', logger.test);
runStatus.on('error', logger.unhandledError);

runStatus.on('stdout', logger.stdout);
runStatus.on('stderr', logger.stderr);
});

var files = cli.input.length ? cli.input : arrify(conf.files);

if (cli.flags.watch) {
try {
var watcher = new Watcher(logger, api, files, arrify(cli.flags.source));
watcher.observeStdin(process.stdin);
} catch (err) {
if (err.name === 'AvaError') {
// An AvaError may be thrown if chokidar is not installed. Log it nicely.
console.error(' ' + colors.error(figures.cross) + ' ' + err.message);
logger.exit(1);
} else {
// Rethrow so it becomes an uncaught exception.
throw err;
}
}
} else {
api.run(files)
.then(function (runStatus) {
logger.finish(runStatus);
logger.exit(runStatus.failCount > 0 || runStatus.rejectionCount > 0 || runStatus.exceptionCount > 0 ? 1 : 0);
})
.catch(function (err) {
// Don't swallow exceptions. Note that any expected error should already
// have been logged.
setImmediate(function () {
throw err;
});
});
}
};
2 changes: 1 addition & 1 deletion lib/logger.js
Expand Up @@ -98,6 +98,6 @@ Logger.prototype.exit = function (code) {

// timeout required to correctly flush IO on Node.js 0.10 on Windows
setTimeout(function () {
process.exit(code); // eslint-disable-line xo/no-process-exit
process.exit(code); // eslint-disable-line unicorn/no-process-exit
}, process.env.AVA_APPVEYOR ? 500 : 0);
};
2 changes: 1 addition & 1 deletion lib/process-adapter.js
Expand Up @@ -16,7 +16,7 @@ if (!isForked) {
console.log();
console.error('Test files must be run with the AVA CLI:\n\n ' + chalk.grey.dim('$') + ' ' + chalk.cyan('ava ' + fp) + '\n');

process.exit(1); // eslint-disable-line xo/no-process-exit
process.exit(1); // eslint-disable-line unicorn/no-process-exit
}

exports.send = function (name, data) {
Expand Down

0 comments on commit 257b34d

Please sign in to comment.