这是indexloc提供的服务,不要输入任何密码
Skip to content

Usage Documentation #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: pass fail test
.PHONY: pass fail mix test

TAP = ./node_modules/tape/bin/tape
MIN = ./bin/tap-difflet
Expand All @@ -9,4 +9,10 @@ pass:
fail:
@$(TAP) test/fail.js | $(MIN)

mix:
@$(TAP) test/mix.js | $(MIN)

mixp:
@$(TAP) test/mix.js | $(MIN) -p

test: pass fail
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ npm install tap-difflet --save-dev
## Usage

~~~ text
tape test/*.js | tap-difflet
tape test/*.js | tap-difflet [options]

Options:
-p --pessimistic Only output failed tests.
-v --version Print the version of tap-difflet.
-h --help Show this.
~~~

## Output
Expand Down
122 changes: 97 additions & 25 deletions bin/tap-difflet
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,82 @@ var objdiff = require('difflet')({

var ansidiff = require('ansidiff');

var docopt = require('docopt').docopt;

var usage = [
'Process TAP output on STDIN and print diffs of failed comparisons.',
'',
'Usage: tap-difflet [options]',
'',
'Options:',
' -p --pessimistic Only output failed tests.',
' -v --version Print the version of tap-difflet.',
' -h --help Show this.',
].join('\n');

var pkg = require('../package.json');
var options = docopt(usage, {
version: pkg.version,
});

var tap = parser();
var out = through();
var dup = duplexer(tap, out);

if (options['--pessimistic']) {
var anAssertFailed = true;
var originalOut = out;
var buffer = null;
tap.on('comment', function(comment) {
if (buffer && anAssertFailed) {
for (var bi=0; bi<buffer.length; ++bi) {
originalOut.push(buffer[bi]);
}
}

buffer = null;
anAssertFailed = false;
out = originalOut;

if (getCommentType(comment) === CommentType.OTHER) {
buffer = [];
out = buffer;
}
});

tap.on('assert', function(res) {
if (!res.ok) {
anAssertFailed = true;
}
});
}

/* Helpers */
var CommentType = {
OTHER: 1,
TESTS: 2,
PASS: 3,
FAIL: 4,
OK: 5
};

function getCommentType(comment) {
if (/^tests\s+[0-9]+$/gi.test(comment)) {
return CommentType.TESTS;
}
else if (/^pass\s+[0-9]+$/gi.test(comment)) {
return CommentType.PASS;
}
else if (/^fail\s+[0-9]+$/gi.test(comment)) {
return CommentType.FAIL;
}
else if (/^ok$/gi.test(comment)) {
return CommentType.OK;
}
else {
return CommentType.OTHER;
}
};

function output(str) {
out.push(str);
Expand All @@ -31,25 +102,26 @@ var timer = hirestime();
var errors = [];

tap.on('comment', function(comment) {
if (/^tests\s+[0-9]+$/gi.test(comment)) {
output('\n');
comment = chalk.white.bold(comment);
}
else if (/^pass\s+[0-9]+$/gi.test(comment)) {
comment = chalk.green.bold(comment);
}
else if (/^fail\s+[0-9]+$/gi.test(comment)) {
comment = chalk.red.bold(comment);
}
else if (/^ok$/gi.test(comment)) {
return;
}
else {
output('\n');
comment = chalk.white.bold(comment);
}
var commentType = getCommentType(comment);
if (commentType === CommentType.TESTS) {
output('\n');
comment = chalk.white.bold(comment);
}
else if (commentType === CommentType.PASS) {
comment = chalk.green.bold(comment);
}
else if (commentType === CommentType.FAIL) {
comment = chalk.red.bold(comment);
}
else if (commentType === CommentType.OK) {
return;
}
else {
output('\n');
comment = chalk.white.bold(comment);
}

output(' ' + comment + '\n');
output(' ' + comment + '\n');
});

tap.on('assert', function(res) {
Expand Down Expand Up @@ -90,10 +162,10 @@ tap.on('results', function(res) {

tap.on('diag', function (diag) {
var expected, actual, at,
gotExpected = true,
gotActual = true,
gotAt = true,
str = '';
gotExpected = true,
gotActual = true,
gotAt = true,
str = '';

if (diag.hasOwnProperty('expected')) {
expected = diag.expected;
Expand All @@ -120,15 +192,15 @@ tap.on('diag', function (diag) {

if (gotActual && gotExpected) {
if (typeof expected !== typeof actual ||
typeof expected === "object" && (!actual || !expected)) {
typeof expected === 'object' && (!actual || !expected)) {
str = 'Expected ' + typeof expected + ' but got ' + typeof actual;
} else if (typeof expected === "string") {
} else if (typeof expected === 'string') {
if (str.indexOf('\n') >= 0) {
str = ansidiff.lines(expected, actual);
} else {
str = ansidiff.chars(expected, actual);
}
} else if (typeof expected === "object") {
} else if (typeof expected === 'object') {
str = objdiff(expected, actual);
} else {
str = chalk.white('Expected ') + chalk.bold(''+expected) + chalk.white(' but got ') + chalk.bold(''+actual);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"ansidiff": "^1.0.0",
"chalk": "^0.5.1",
"difflet": "^0.2.6",
"docopt": "^0.6.2",
"duplexer": "^0.1.1",
"hirestime": "^0.2.4",
"pretty-ms": "^1.0.0",
Expand Down
2 changes: 2 additions & 0 deletions test/mix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require ('./pass');
require ('./fail');