Skip to content

Commit f88d449

Browse files
prantlfljharb
authored andcommitted
Trim too long assert messages to prevent line overflow
Failing messages need 4 more characters cut.
1 parent 418c79e commit f88d449

5 files changed

Lines changed: 94 additions & 5 deletions

File tree

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ module.exports = function (opts) {
1616
var tap = parser();
1717
var out = through2();
1818

19-
function trimWidth(s) {
19+
function trimWidth(s, ok) {
2020
if (opts && opts.width && s.length > opts.width - 2) {
21-
return s.slice(0, opts.width - 5) + '...';
21+
var more = ok ? 0 : 4;
22+
return s.slice(0, opts.width - 5 - more) + '...';
2223
}
2324
return s;
2425
}
@@ -61,13 +62,13 @@ module.exports = function (opts) {
6162
var s = trimWidth(trim(res.name));
6263
push(out, sprintf(
6364
'\x1b[1m\x1b[' + c + 'm%s\x1b[0m\n',
64-
trimWidth((res.ok ? '✓' : '⨯') + ' ' + s)
65+
trimWidth((res.ok ? '✓' : '⨯') + ' ' + s, res.ok)
6566
));
6667
return;
6768
}
6869

6970
var fmt = '\r %s \x1b[1m\x1b[' + c + 'm%d\x1b[0m %s\x1b[K';
70-
var str = sprintf(fmt, ok, res.number, res.name);
71+
var str = sprintf(fmt, ok, res.number, trimWidth(res.name, res.ok));
7172

7273
if (!res.ok) {
7374
var y = ++test.offset + 1;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
},
99
"scripts": {
1010
"lint": "eslint --ext=js,mjs .",
11-
"pretest": "npm run lint"
11+
"pretest": "npm run lint",
12+
"tests-only": "tape test/**/*.js | node bin/cmd",
13+
"test": "npm run tests-only"
1214
},
1315
"dependencies": {
1416
"array.prototype.foreach": "^1.0.2",

test/long-message/failed.tap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
TAP version 13
2+
# Resolving an unknown dependency fails.
3+
not ok 1 The message "Unrecognised dependency: "dummy"." should start with "Unrecognised dependency:".
4+
---
5+
operator: ok
6+
expected: true
7+
actual: false
8+
at: Test.<anonymous> (/Users/ferdipr/Sources.localized/smartui/gitlab/csui-dependency-installer/test/dependencies.test.js:20:12)
9+
stack: |-
10+
Error: The message "Unrecognised dependency: "dummy"." should start with "Unrecognised dependency:".
11+
at Test.bound [as run] (/Users/ferdipr/Sources/csui-dependency-installer/node_modules/tape/lib/test.js:77:32)
12+
at Immediate.next (/Users/ferdipr/Sources/csui-dependency-installer/node_modules/tape/lib/results.js:81:19)
13+
at runCallback (timers.js:810:20)
14+
...
15+
16+
1..1
17+
# tests 1
18+
# pass 0
19+
# fail 1
20+

test/long-message/index.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
var test = require('tape');
4+
var stream = require('stream');
5+
var path = require('path');
6+
var fs = require('fs');
7+
var faucet = require('../..');
8+
9+
function createFormatter() {
10+
return faucet({ width: 80 });
11+
}
12+
13+
function streamifyString(string) {
14+
return new stream.Readable({
15+
read: function () {
16+
this.push(string);
17+
this.push(null);
18+
}
19+
});
20+
}
21+
22+
function stringifyStream(theStream, callback) {
23+
var output = '';
24+
theStream.on('data', function (chunk) {
25+
output += chunk;
26+
}).on('end', function () {
27+
callback(output);
28+
});
29+
}
30+
31+
function getFilePath(name) {
32+
return path.join(__dirname, name + '.tap');
33+
}
34+
35+
function checkFormatting(t, name, expectedExcerpt) {
36+
var tapString = fs.readFileSync(getFilePath(name), 'utf-8');
37+
var formattedStream = streamifyString(tapString).pipe(createFormatter());
38+
stringifyStream(formattedStream, function (actualOutput) {
39+
var contains = actualOutput.indexOf(expectedExcerpt) > 0;
40+
41+
t.ok(contains, 'Looking for "' + expectedExcerpt + '" in the "' + name + '" input.');
42+
43+
t.end();
44+
});
45+
}
46+
47+
test('A long successful assertion message should be cut.', function (t) {
48+
checkFormatting(t, 'succeeded', 'should start with "Unrecogn...');
49+
});
50+
51+
test('A long failed assertion message should be cut.', function (t) {
52+
checkFormatting(t, 'failed', 'should start with "Unre...');
53+
});

test/long-message/succeeded.tap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
TAP version 13
2+
# Exported dependency names can be resolved.
3+
ok 1 The dependency "csui" should be resolvable.
4+
ok 2 The dependency "esoc" should be resolvable.
5+
# Resolving an unknown dependency fails.
6+
ok 3 The message "Unrecognised dependency: "dummy"." should start with "Unrecognised dependency:".
7+
8+
1..3
9+
# tests 3
10+
# pass 3
11+
12+
# ok
13+

0 commit comments

Comments
 (0)