Skip to content

Commit

Permalink
[Robustness] use string.prototype.trim, array.prototype.foreach, …
Browse files Browse the repository at this point in the history
…`array.prototype.push`
  • Loading branch information
ljharb committed Sep 22, 2022
1 parent 0d4aa82 commit edcea74
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
37 changes: 20 additions & 17 deletions index.js
Expand Up @@ -4,20 +4,23 @@ var through2 = require('through2');
var duplexer = require('duplexer');
var parser = require('tap-parser');
var sprintf = require('sprintf-js').sprintf;
var forEach = require('array.prototype.foreach');
var push = require('array.prototype.push');
var trim = require('string.prototype.trim');

module.exports = function (opts) {
var tap = parser();
var out = through2();

function trim(s) {
function trimWidth(s) {
if (opts && opts.width && s.length > opts.width - 2) {
return s.slice(0, opts.width - 5) + '...';
}
return s;
}

function updateName(y, str, c) {
return '\x1b[' + y + 'A\x1b[1G\x1b[1m\x1b[' + c + 'm' + trim(str) + '\x1b[0m\x1b[' + y + 'B\x1b[1G';
return '\x1b[' + y + 'A\x1b[1G\x1b[1m\x1b[' + c + 'm' + trimWidth(str) + '\x1b[0m\x1b[' + y + 'B\x1b[1G';
}

var test;
Expand All @@ -31,10 +34,10 @@ module.exports = function (opts) {
&& test.assertions.length === 0
&& (/^(tests|pass)\s+\d+$/).test(test.name)
) {
out.push('\r' + trim(test.name));
push(out, '\r' + trimWidth(test.name));
} else if (test && test.ok) {
var s = updateName(test.offset + 1, '✓ ' + test.name, 32);
out.push('\r' + s);
push(out, '\r' + s);
}

test = {
Expand All @@ -43,18 +46,18 @@ module.exports = function (opts) {
offset: 0,
ok: true
};
out.push('\r' + trim('# ' + comment) + '\x1b[K\n');
push(out, '\r' + trimWidth('# ' + comment) + '\x1b[K\n');
});

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 = trim(res.name.trim());
out.push(sprintf(
var s = trimWidth(trim(res.name));
push(out, sprintf(
'\x1b[1m\x1b[' + c + 'm%s\x1b[0m\n',
trim((res.ok ? '✓' : '⨯') + ' ' + s)
trimWidth((res.ok ? '✓' : '⨯') + ' ' + s)
));
return;
}
Expand All @@ -70,15 +73,15 @@ module.exports = function (opts) {
}
test.ok = false;
}
out.push(str);
test.assertions.push(res);
push(out, str);
push(test.assertions, res);
});

tap.on('extra', function (extra) {
if (!test || test.assertions.length === 0) { return; }
var last = test.assertions[test.assertions.length - 1];
if (!last.ok) {
out.push(extra.split('\n').map(function (line) {
push(out, extra.split('\n').map(function (line) {
return ' ' + line;
}).join('\n') + '\n');
}
Expand All @@ -88,27 +91,27 @@ module.exports = function (opts) {

tap.on('results', function (res) {
if (test && (/^fail\s+\d+$/).test(test.name)) {
out.push(updateName(test.offset + 1, '⨯ ' + test.name, 31));
push(out, updateName(test.offset + 1, '⨯ ' + test.name, 31));
} else if (test && test.ok) {
out.push(updateName(test.offset + 1, '✓ ' + test.name, 32));
push(out, updateName(test.offset + 1, '✓ ' + test.name, 32));
}

res.errors.forEach(function (err, ix) {
out.push(sprintf(
forEach(res.errors, function (err, ix) {
push(out, sprintf(
'not ok \x1b[1m\x1b[31m%d\x1b[0m %s\n',
ix + 1 + res.asserts.length,
err.message
));
});

if (!res.ok && !(/^fail\s+\d+$/).test(test && test.name)) {
out.push(sprintf(
push(out, sprintf(
'\r\x1b[1m\x1b[31m⨯ fail %s\x1b[0m\x1b[K\n',
(res.errors.length + res.fail.length) || ''
));
}

out.push(null);
push(out, null);

dup.emit('results', res);
if (!res.ok) { dup.emit('fail'); }
Expand Down
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -7,15 +7,18 @@
"faucet": "bin/cmd.js"
},
"scripts": {
"lint": "eslint --ext=js,mjs .",
"pretest": "npm run lint"
"lint": "eslint --ext=js,mjs .",
"pretest": "npm run lint"
},
"dependencies": {
"array.prototype.foreach": "^1.0.2",
"array.prototype.push": "^1.0.2",
"defined": "^0.0.0",
"duplexer": "^0.1.2",
"minimist": "^1.2.6",
"npm-which": "^1.0.2",
"sprintf-js": "^1.1.2",
"string.prototype.trim": "^1.2.6",
"tap-parser": "^0.4.3",
"tape": "^5.6.1",
"through2": "^0.2.3"
Expand Down

0 comments on commit edcea74

Please sign in to comment.