Skip to content

Commit

Permalink
feat: internal tap parser
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Jun 16, 2019
1 parent 110b0e1 commit 9ed7206
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 40 deletions.
16 changes: 14 additions & 2 deletions bin/browser-do.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const run = require('../index');
const opts = require('commander');
const through = require('through');
const {Writable} = require('stream');
const tapFinished = require('../lib/tap-finished');
const tapParse = require('../lib/tap-parse');

opts
.version(require('../package.json').version)
Expand Down Expand Up @@ -50,7 +50,19 @@ readInput.on('finish', () => {

// note output stream is piped to two different destinations.
// 1. tap-parser to deal with tap results
if (tap) holdOutput.pipe(tapFinished(opts.keepOpen));
if (tap) {
tapParse(holdOutput, (err, passed) => {
if (err) {
console.error(err.message);
}

if (!opts.keepOpen) {
setTimeout(() => {
process.exit(passed ? 0 : 1);
}, 1000);
}
})
}
// 2. to stdout
holdOutput.pipe(process.stdout);

Expand Down
9 changes: 6 additions & 3 deletions lib/get-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ function load(name) {
return require('./browsers/' + name);
}


function fileExists(path) {
try {
const stats = fs.statSync(path);
Expand All @@ -14,7 +13,11 @@ function fileExists(path) {
}
};

module.exports = function(browsername, _load = load) {
module.exports = function(browsername, {
// for tests
_load = load,
_fileExists = fileExists
} = {}) {
let browser;
try {
browser = _load(browsername);
Expand All @@ -29,7 +32,7 @@ module.exports = function(browsername, _load = load) {
if (!browser) return;

const bpath = browser.path[process.platform];
if (bpath && (bpath === 'open' || fileExists(bpath))) {
if (bpath && (bpath === 'open' || _fileExists(bpath))) {
return {...browser, path: bpath};
}
};
34 changes: 0 additions & 34 deletions lib/tap-finished.js

This file was deleted.

86 changes: 86 additions & 0 deletions lib/tap-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Different from https://github.com/tapjs/tap-parser
// This implementation doesn't support subtests, because
// subtests is not in TAP spec v12 or v13, it's in v14 draft
// which only node-tap uses.
// But node-tap does not run in browser, so that's irrelevant.

const readline = require('readline');

module.exports = function(readableStream, cb) {
const rl = readline.createInterface({
input: readableStream,
crlfDelay: Infinity // '\r\n' as single line break.
});

function panic(err) {
rl.close();
cb(err);
}

function complete(passed) {
rl.close();
cb(null, passed);
}

let plan = null;
let tests = [];

function captureVersion(line) {
const m = line.match(/^TAP version (\d+)$/);
if (m) {
const version = parseInt(m[1], 10);
if (version < 12 || version > 13) {
throw new Error('TAP version ' + version + ' is not supported');
}
return true;
}
}

function capturePlan(line) {
const m = line.match(/^1\.\.(\d+)$/);
if (m) {
plan = parseInt(m[1], 10);
return true;
}
}

function captureTest(line) {
const m = line.match(/^((?:not )?ok)\b/);
if (m) {
const pass = m[1] === 'ok';
const todo = (/# TODO /i).test(line);
const skip = !todo && (/# SKIP\S*\s+/i).test(line);
tests.push({pass, todo, skip});
return true;
}
}

function captureBailout(line) {
const m = line.match(/^Bail out!/);
if (m) {
throw new Error(line);
}
}

rl.on('line', line => {
if (line.match(/^\s*$/)) return;

// console.log(' LINE: ' + line);
try {
captureVersion(line) ||
capturePlan(line) ||
captureTest(line) ||
captureBailout(line);

// console.log(' PLAN: ' + plan);
// console.log(' TESTS: ', tests);
if (plan && tests.length === plan) {
complete(tests.every(t => t.pass || t.todo || t.skip));
}
} catch (err) {
panic(err);
}
});

return rl;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"mocha": "^6.1.4",
"source-map-support": "^0.5.12",
"standard-changelog": "^2.0.11",
"tap-dot": "^2.0.0",
"tape": "^4.10.2",
"utf8-stream": "0.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion test/get-browser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function load(name) {
}

function _getBrowser(name) {
return getBrowser(name, load);
return getBrowser(name, {_load: load, _fileExists: () => true});
}

test('getBrowser returns nothing for missing browser', t => {
Expand Down

0 comments on commit 9ed7206

Please sign in to comment.