From 1e629eac5199ce8475397e1c10d96999bf76d279 Mon Sep 17 00:00:00 2001 From: James Halliday Date: Thu, 26 Dec 2013 10:03:01 -0800 Subject: [PATCH] hacks for mocha tap formatting --- bin/cmd.js | 8 ++++++-- index.js | 13 +++++++++++++ package.json | 3 ++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index c609e24..203affb 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -1,14 +1,19 @@ #!/usr/bin/env node var faucet = require('../'); var minimist = require('minimist'); +var defined = require('defined'); var tapeCmd = require.resolve('tape/bin/tape'); var spawn = require('child_process').spawn; var fs = require('fs'); var path = require('path'); +var argv = minimist(process.argv.slice(2)); var tap = faucet({ - width: process.stdout.isTTY ? process.stdout.columns : 0 + width: defined(argv.w, argv.width, process.stdout.isTTY + ? process.stdout.columns - 5 + : 0 + ) }); process.on('exit', function (code) { if (code === 0 && tap.exitCode !== 0) { @@ -16,7 +21,6 @@ process.on('exit', function (code) { } }); -var argv = minimist(process.argv.slice(2)); if (!process.stdin.isTTY || argv._[0] === '-') { process.stdin.pipe(tap).pipe(process.stdout); return; diff --git a/index.js b/index.js index cea80e9..c135230 100644 --- a/index.js +++ b/index.js @@ -31,6 +31,19 @@ module.exports = function (opts) { tap.on('assert', function (res) { var ok = res.ok ? 'ok' : 'not ok'; var c = res.ok ? 32 : 31; + if (!test) { + // mocha produces TAP results this way, whatever + var s = res.name.trim(); + if (opts.width && s.length > opts.width - 2) { + s = s.slice(0, opts.width - 5) + '...'; + } + out.push(sprintf( + '\x1b[1m\x1b[' + c + 'm%s %s\x1b[0m\n', + res.ok ? '✓' : '⨯', s + )); + return; + } + var fmt = '\r %s \x1b[1m\x1b[' + c + 'm%d\x1b[0m %s\x1b[K'; var str = sprintf(fmt, ok, res.number, res.name); diff --git a/package.json b/package.json index 33323db..5557f86 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "duplexer": "~0.1.1", "sprintf": "~0.1.3", "tape": "~2.3.2", - "minimist": "0.0.5" + "minimist": "0.0.5", + "defined": "0.0.0" }, "scripts": { "test": "tape test/*.js"