From a8b0c204fbb6c65dd6be1b67956717665fdcd7e6 Mon Sep 17 00:00:00 2001 From: James Talmage Date: Fri, 27 Nov 2015 19:10:07 -0500 Subject: [PATCH] cli.js: wrap in IIFE to avoid `xo` parse error. This could also be fixed upstream in `xo`. Reference: https://github.com/eslint/eslint/issues/1158 --- cli.js | 439 +++++++++++++++++++++-------------------- lib/test-results.js | 22 +++ test/fixture/assert.js | 61 ++++++ 3 files changed, 304 insertions(+), 218 deletions(-) create mode 100644 lib/test-results.js create mode 100644 test/fixture/assert.js diff --git a/cli.js b/cli.js index 52d1df63a7..f2fa9a17e2 100755 --- a/cli.js +++ b/cli.js @@ -1,266 +1,269 @@ #!/usr/bin/env node 'use strict'; -// Prefer the local installation of AVA. -var resolveFrom = require('resolve-from'); -var localCLI; -try { - localCLI = resolveFrom('.', 'ava/cli'); -} catch (e) {} - -if (localCLI && localCLI !== __filename) { - var thisVersion = require('./package.json').version; - var localVersion = require(localCLI.replace(/cli\.js$/, 'package.json')).version; - var warningMessage = 'Using local install of AVA (v%s), which '; - warningMessage += thisVersion === localVersion ? 'matches' : 'differs from'; - warningMessage += ' global (v%s)'; - console.warn(require('chalk').yellow(warningMessage), localVersion, thisVersion); - require(localCLI); - return; -} - -var debug = require('debug')('ava'); - -if (debug.enabled) { - require('time-require'); -} - -var fs = require('fs'); -var path = require('path'); -var figures = require('figures'); -var flatten = require('arr-flatten'); -var globby = require('globby'); -var meow = require('meow'); -var updateNotifier = require('update-notifier'); -var chalk = require('chalk'); -var Promise = require('bluebird'); -var fork = require('./lib/fork'); -var log = require('./lib/logger'); - -// Bluebird specific -Promise.longStackTraces(); - -var cli = meow([ - 'Usage', - ' ava [ ...]', - '', - 'Options', - ' --init Add AVA to your project', - ' --fail-fast Stop after first test failure', - ' --serial Run tests serially', - '', - 'Examples', - ' ava', - ' ava test.js test2.js', - ' ava test-*.js', - ' ava --init', - ' ava --init foo.js', - '', - 'Default patterns when no arguments:', - 'test.js test-*.js test/*.js' -], { - string: ['_'], - boolean: [ - 'fail-fast', - 'serial' - ] -}); - -var rejectionCount = 0; -var exceptionCount = 0; -var testCount = 0; -var fileCount = 0; -var errors = []; - -function prefixTitle(file) { - var separator = ' ' + chalk.gray.dim(figures.pointerSmall) + ' '; - - var base = path.dirname(cli.input[0]); - - if (base === '.') { - base = cli.input[0] || 'test'; +(function () { + // Prefer the local installation of AVA. + var resolveFrom = require('resolve-from'); + var localCLI; + try { + localCLI = resolveFrom('.', 'ava/cli'); + } catch (e) { } - base += path.sep; + if (localCLI && localCLI !== __filename) { + var thisVersion = require('./package.json').version; + var localVersion = require(localCLI.replace(/cli\.js$/, 'package.json')).version; + var warningMessage = 'Using local install of AVA (v%s), which '; + warningMessage += thisVersion === localVersion ? 'matches' : 'differs from'; + warningMessage += ' global (v%s)'; + console.warn(require('chalk').yellow(warningMessage), localVersion, thisVersion); + require(localCLI); + return; + } - var prefix = path.relative('.', file) - .replace(base, '') - .replace(/\.spec/, '') - .replace(/test\-/g, '') - .replace(/\.js$/, '') - .split(path.sep) - .join(separator); + var debug = require('debug')('ava'); - if (prefix.length > 0) { - prefix += separator; + if (debug.enabled) { + require('time-require'); } - return prefix; -} + var fs = require('fs'); + var path = require('path'); + var figures = require('figures'); + var flatten = require('arr-flatten'); + var globby = require('globby'); + var meow = require('meow'); + var updateNotifier = require('update-notifier'); + var chalk = require('chalk'); + var Promise = require('bluebird'); + var fork = require('./lib/fork'); + var log = require('./lib/logger'); + +// Bluebird specific + Promise.longStackTraces(); + + var cli = meow([ + 'Usage', + ' ava [ ...]', + '', + 'Options', + ' --init Add AVA to your project', + ' --fail-fast Stop after first test failure', + ' --serial Run tests serially', + '', + 'Examples', + ' ava', + ' ava test.js test2.js', + ' ava test-*.js', + ' ava --init', + ' ava --init foo.js', + '', + 'Default patterns when no arguments:', + 'test.js test-*.js test/*.js' + ], { + string: ['_'], + boolean: [ + 'fail-fast', + 'serial' + ] + }); + + var rejectionCount = 0; + var exceptionCount = 0; + var testCount = 0; + var fileCount = 0; + var errors = []; + + function prefixTitle(file) { + var separator = ' ' + chalk.gray.dim(figures.pointerSmall) + ' '; + + var base = path.dirname(cli.input[0]); + + if (base === '.') { + base = cli.input[0] || 'test'; + } + + base += path.sep; -function stats(stats) { - testCount += stats.testCount; -} + var prefix = path.relative('.', file) + .replace(base, '') + .replace(/\.spec/, '') + .replace(/test\-/g, '') + .replace(/\.js$/, '') + .split(path.sep) + .join(separator); -function test(data) { - var isError = data.error.message; + if (prefix.length > 0) { + prefix += separator; + } - if (fileCount > 1) { - data.title = prefixTitle(data.file) + data.title; + return prefix; } - if (isError) { - log.error(data.title, chalk.red(data.error.message)); + function stats(stats) { + testCount += stats.testCount; + } - errors.push(data); - } else { - // don't log it if there's only one file and one anonymous test - if (fileCount === 1 && testCount === 1 && data.title === '[anonymous]') { - return; + function test(data) { + var isError = data.error.message; + + if (fileCount > 1) { + data.title = prefixTitle(data.file) + data.title; } - log.test(data); + if (isError) { + log.error(data.title, chalk.red(data.error.message)); + + errors.push(data); + } else { + // don't log it if there's only one file and one anonymous test + if (fileCount === 1 && testCount === 1 && data.title === '[anonymous]') { + return; + } + + log.test(data); + } } -} -function run(file) { - var args = [file]; + function run(file) { + var args = [file]; + + if (cli.flags.failFast) { + args.push('--fail-fast'); + } + + if (cli.flags.serial) { + args.push('--serial'); + } + + // Forward the `time-require` `--sorted` flag. + // Intended for internal optimization tests only. + if (cli.flags.sorted) { + args.push('--sorted'); + } - if (cli.flags.failFast) { - args.push('--fail-fast'); + return fork(args) + .on('stats', stats) + .on('test', test) + .on('unhandledRejections', handleRejections) + .on('uncaughtException', handleExceptions); } - if (cli.flags.serial) { - args.push('--serial'); + function handleRejections(data) { + log.unhandledRejections(data.file, data.rejections); + rejectionCount += data.rejections.length; } - // Forward the `time-require` `--sorted` flag. - // Intended for internal optimization tests only. - if (cli.flags.sorted) { - args.push('--sorted'); + function handleExceptions(data) { + log.uncaughtException(data.file, data.exception); + exceptionCount++; } - return fork(args) - .on('stats', stats) - .on('test', test) - .on('unhandledRejections', handleRejections) - .on('uncaughtException', handleExceptions); -} + function sum(arr, key) { + var result = 0; -function handleRejections(data) { - log.unhandledRejections(data.file, data.rejections); - rejectionCount += data.rejections.length; -} + arr.forEach(function (item) { + result += item[key]; + }); -function handleExceptions(data) { - log.uncaughtException(data.file, data.exception); - exceptionCount++; -} + return result; + } -function sum(arr, key) { - var result = 0; + function exit(results) { + // assemble stats from all tests + var stats = results.map(function (result) { + return result.stats; + }); - arr.forEach(function (item) { - result += item[key]; - }); + var tests = results.map(function (result) { + return result.tests; + }); - return result; -} + var passed = sum(stats, 'passCount'); + var failed = sum(stats, 'failCount'); -function exit(results) { - // assemble stats from all tests - var stats = results.map(function (result) { - return result.stats; - }); + log.write(); + log.report(passed, failed, rejectionCount, exceptionCount); + log.write(); - var tests = results.map(function (result) { - return result.tests; - }); + if (failed > 0) { + log.errors(flatten(tests)); + } + + process.stdout.write(''); - var passed = sum(stats, 'passCount'); - var failed = sum(stats, 'failCount'); + flushIoAndExit(failed > 0 || rejectionCount > 0 || exceptionCount > 0 ? 1 : 0); + } - log.write(); - log.report(passed, failed, rejectionCount, exceptionCount); - log.write(); + function flushIoAndExit(code) { + // TODO: figure out why this needs to be here to + // correctly flush the output when multiple test files + process.stdout.write(''); + process.stderr.write(''); - if (failed > 0) { - log.errors(flatten(tests)); + // timeout required to correctly flush IO on Node.js 0.10 on Windows + setTimeout(function () { + process.exit(code); + }, process.env.AVA_APPVEYOR ? 500 : 0); } - process.stdout.write(''); - - flushIoAndExit(failed > 0 || rejectionCount > 0 || exceptionCount > 0 ? 1 : 0); -} - -function flushIoAndExit(code) { - // TODO: figure out why this needs to be here to - // correctly flush the output when multiple test files - process.stdout.write(''); - process.stderr.write(''); - - // timeout required to correctly flush IO on Node.js 0.10 on Windows - setTimeout(function () { - process.exit(code); - }, process.env.AVA_APPVEYOR ? 500 : 0); -} - -function init(files) { - log.write(); - - return handlePaths(files) - .map(function (file) { - return path.resolve(file); - }) - .then(function (files) { - if (files.length === 0) { - log.error('Couldn\'t find any files to test\n'); - process.exit(1); - } + function init(files) { + log.write(); - fileCount = files.length; + return handlePaths(files) + .map(function (file) { + return path.resolve(file); + }) + .then(function (files) { + if (files.length === 0) { + log.error('Couldn\'t find any files to test\n'); + process.exit(1); + } - var tests = files.map(run); + fileCount = files.length; - return Promise.all(tests); - }); -} - -function handlePaths(files) { - if (files.length === 0) { - files = [ - 'test.js', - 'test-*.js', - 'test/*.js' - ]; + var tests = files.map(run); + + return Promise.all(tests); + }); } - files.push('!**/node_modules/**'); + function handlePaths(files) { + if (files.length === 0) { + files = [ + 'test.js', + 'test-*.js', + 'test/*.js' + ]; + } - // convert pinkie-promise to Bluebird promise - files = Promise.resolve(globby(files)); + files.push('!**/node_modules/**'); - return files - .map(function (file) { - if (fs.statSync(file).isDirectory()) { - return handlePaths([path.join(file, '*.js')]); - } + // convert pinkie-promise to Bluebird promise + files = Promise.resolve(globby(files)); - return file; - }) - .then(flatten) - .filter(function (file) { - return path.extname(file) === '.js' && path.basename(file)[0] !== '_'; - }); -} + return files + .map(function (file) { + if (fs.statSync(file).isDirectory()) { + return handlePaths([path.join(file, '*.js')]); + } -updateNotifier({pkg: cli.pkg}).notify(); + return file; + }) + .then(flatten) + .filter(function (file) { + return path.extname(file) === '.js' && path.basename(file)[0] !== '_'; + }); + } -if (cli.flags.init) { - require('ava-init')(); -} else { - init(cli.input).then(exit).catch(function (err) { - console.error(err.stack); - flushIoAndExit(1); - }); -} + updateNotifier({pkg: cli.pkg}).notify(); + + if (cli.flags.init) { + require('ava-init')(); + } else { + init(cli.input).then(exit).catch(function (err) { + console.error(err.stack); + flushIoAndExit(1); + }); + } +})(); diff --git a/lib/test-results.js b/lib/test-results.js new file mode 100644 index 0000000000..425370d09d --- /dev/null +++ b/lib/test-results.js @@ -0,0 +1,22 @@ +'use strict'; + +function FileResult(filePath) { + this.filePath = filePath; + this.testResults = []; +} + +FileResult.prototype.addTest = function addTest(test) { + this.testResults.push(test); +}; + +function TestResult(title, type) { + this.title = title; + this.type = type; + this.assertions = []; + this.errors = []; + this.duration = -1; +} + +TestResult.prototype.addAssertion = function addAssertion(context) { + this.assertions.push(context); +}; diff --git a/test/fixture/assert.js b/test/fixture/assert.js new file mode 100644 index 0000000000..e82e657787 --- /dev/null +++ b/test/fixture/assert.js @@ -0,0 +1,61 @@ +'use strict'; +const test = require('../../'); + /* +test('pass', t => { + t.pass(); + t.pass('pass message'); + t.end(); +}); + +test('fail', t => { + t.fail(); + t.fail('fail message'); + t.end(); +}); + +test('ok', t => { + t.ok('ok'); + t.ok('truthy value', 'ok message'); + t.end(); +}); + +test('notOk', t => { + t.notOk(!'notOk'); + t.notOk(!'notOk', 'notOk message'); + t.end(); +}); + +test('true', t => { + t.true(true); + t.end(); +}); + +test('false', t => { + t.false(false); + t.end(); +}); + +test('is', t => { + t.is(2 + 2, 4); + t.end(); +}); + +test('not', t => { + t.not(2 + 2, 5); + t.end(); +}); + +test('same', t => { + t.same({a: 'b'}, {a: 'b'}); + t.end(); +}); */ + +test('notSame', t=> { + t.notSame({a: 'b'}, {c: 'd'}); + t.end(); +}); + +// test('throws') +// test('doesNotThrow') +// test('regexTest') +// test('ifError')