Skip to content

Commit

Permalink
add TAP support
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemedes committed Dec 17, 2015
1 parent b669807 commit 74673c1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
34 changes: 25 additions & 9 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -54,7 +56,8 @@ var cli = meow([
],
boolean: [
'fail-fast',
'serial'
'serial',
'tap'
]
});

Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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('');
Expand Down
46 changes: 46 additions & 0 deletions lib/tap.js
Original file line number Diff line number Diff line change
@@ -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();
};

0 comments on commit 74673c1

Please sign in to comment.