From 74673c138180aa353c9e4bc8164ddfd3daf323cb Mon Sep 17 00:00:00 2001 From: vdemedes Date: Thu, 17 Dec 2015 08:09:28 +0100 Subject: [PATCH] add TAP support --- cli.js | 34 +++++++++++++++++++++++++--------- lib/tap.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 lib/tap.js diff --git a/cli.js b/cli.js index d72109484..3a2577d48 100755 --- a/cli.js +++ b/cli.js @@ -23,6 +23,7 @@ var updateNotifier = require('update-notifier'); var chalk = require('chalk'); var Promise = require('bluebird'); var log = require('./lib/logger'); +var tap = require('./lib/tap'); var Api = require('./api'); // Bluebird specific @@ -36,7 +37,8 @@ var cli = meow([ ' --init Add AVA to your project', ' --fail-fast Stop after first test failure', ' --serial Run tests serially', - ' --require Module to preload (Can be repeated)', + ' --require Module to preload (Can be repeated)',, + ' --tap Generate TAP output', '', 'Examples', ' ava', @@ -54,7 +56,8 @@ var cli = meow([ ], boolean: [ 'fail-fast', - 'serial' + 'serial', + 'tap' ] }); @@ -65,7 +68,11 @@ if (cli.flags.init) { return; } -log.write(); +if (cli.flags.tap) { + tap.start(); +} else { + log.write(); +} var api = new Api(cli.input, { failFast: cli.flags.failFast, @@ -74,6 +81,11 @@ var api = new Api(cli.input, { }); api.on('test', function (test) { + if (cli.flags.tap) { + tap.test(test); + return; + } + if (test.error) { log.error(test.title, chalk.red(test.error.message)); } else { @@ -92,12 +104,16 @@ api.on('error', function (data) { api.run() .then(function () { - log.write(); - log.report(api.passCount, api.failCount, api.rejectionCount, api.exceptionCount); - log.write(); - - if (api.failCount > 0) { - log.errors(api.errors); + if (cli.flags.tap) { + tap.finish(api.passCount, api.failCount); + } else { + log.write(); + log.report(api.passCount, api.failCount, api.rejectionCount, api.exceptionCount); + log.write(); + + if (api.failCount > 0) { + log.errors(api.errors); + } } process.stdout.write(''); diff --git a/lib/tap.js b/lib/tap.js new file mode 100644 index 000000000..3cdc06990 --- /dev/null +++ b/lib/tap.js @@ -0,0 +1,46 @@ +'use strict'; +var format = require('util').format; + +// Parses stack trace and extracts original function name, file name and line. +function getSourceFromStack(stack) { + return stack + .split('\n') + .slice(2, 3) + .join('') + .replace(/^\s+at /, ''); +} + +exports.start = function () { + console.log('TAP version 13'); +}; + +var i = 0; + +exports.test = function (test) { + var output = []; + + output.push('# ' + test.title); + + if (test.error) { + output.push(format('not ok %d - %s', ++i, test.error.message)); + output.push(' ---'); + output.push(' operator: ' + test.error.operator); + output.push(' expected: ' + test.error.expected); + output.push(' actual: ' + test.error.actual); + output.push(' at: ' + getSourceFromStack(test.error.stack)); + output.push(' ...'); + } else { + output.push(format('ok %d - %s', ++i, test.title)); + } + + console.log(output.join('\n')); +}; + +exports.finish = function (passCount, failCount) { + console.log(); + console.log('1..' + (passCount + failCount)); + console.log('# tests ' + (passCount + failCount)); + console.log('# pass ' + passCount); + console.log('# fail ' + failCount); + console.log(); +};