From 56bd0732515f1b7a8117b6eb90c2fa4c1d861350 Mon Sep 17 00:00:00 2001 From: johnnyman727 Date: Tue, 22 Jul 2014 16:15:37 -0700 Subject: [PATCH 1/3] Moving testrunner and tester scripts into appropriate repos --- lib/tester/tester.js | 102 +++++++++++++++++++++++++ index.js => lib/testrunner/parser.js | 2 - tinytap.js => lib/testrunner/runner.js | 4 +- package.json | 6 +- test.sh => test/test.sh | 2 +- 5 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 lib/tester/tester.js rename index.js => lib/testrunner/parser.js (98%) rename tinytap.js => lib/testrunner/runner.js (96%) rename test.sh => test/test.sh (64%) mode change 100755 => 100644 diff --git a/lib/tester/tester.js b/lib/tester/tester.js new file mode 100644 index 0000000..8d34a3e --- /dev/null +++ b/lib/tester/tester.js @@ -0,0 +1,102 @@ +// tiny node-tap lookalike. +module.exports = test + +var tests = module.exports.tests = []; +var ran = false +var id = 0 +var fail = 0 +var pass = 0 + +function test(name, fn) { + tests.push([name, fn]); + if (ran) return; + ran = true + process.nextTick(run) +} + +var assert = require('assert'); +Error.captureStackTrace = function(){}; + +var t = Object.keys(assert).map(function (k) { + if (typeof assert[k] !== 'function') return; + return [k, function () { + var s = null + id++ + try { + assert[k].apply(assert, arguments) + pass ++ + console.log('ok', id, k) + } catch (e) { + + fail ++ + // ignore everything up to the run() function + // Comment out until captureStackTrace is implemented + // Error.captureStackTrace(e, t[k]) + s = e.stack + if (s) { + s = s.trim().split(/\n/) + // bottom two frames are nextTick and this file + s.pop() + s.pop() + } + + if (s && !e.message) + e.message = s[0] + console.log('') + console.log('not ok', id, s ? s.shift() : e.message) + if (s && s.length) { + s = s.map(function(s) { + return s.trim() + '\n' + }) + console.log('#' + s.join('# ')) + } + console.log('') + } + }] +}).reduce(function (set, kv) { + set[kv[0]] = kv[1] + return set +}, {}) + +t.pass = function (m) { + assert(true, m) +} + +t.fail = function (m) { + assert(false, m) +} + +t.comment = function (m) { + console.log('#', m.replace(/^#\s*/, ''), "\n") +} + +t.end = run + +var children = [] +t.test = function(name, fn) { + children.push([name, fn]) +} + +function run() { + if (children.length) { + tests.unshift.apply(tests, children) + children.length = 0 + } + + var next = tests.shift(); + if (!next) { + console.log('0..', id) + console.log('') + console.log('# pass', pass, pass + fail) + console.log('# fail', fail, pass + fail) + process.exit(fail) + return + } + + var name = next[0]; + var fn = next[1]; + console.log('#', name); + process.nextTick(function() { + fn(t); + }) +} diff --git a/index.js b/lib/testrunner/parser.js similarity index 98% rename from index.js rename to lib/testrunner/parser.js index 2471aab..9b25027 100644 --- a/index.js +++ b/lib/testrunner/parser.js @@ -2,8 +2,6 @@ var fs = require('fs'); var Duplex = require('stream').Duplex; var util = require('util'); -// console.log('tap tiny'); - function state (machine) { var key = 'start'; return function caller () { diff --git a/tinytap.js b/lib/testrunner/runner.js similarity index 96% rename from tinytap.js rename to lib/testrunner/runner.js index 79aec18..5e0f138 100755 --- a/tinytap.js +++ b/lib/testrunner/runner.js @@ -4,7 +4,7 @@ var spawn = require('child_process').spawn; var parse = require('shell-quote').parse; var quote = require('shell-quote').quote; var stream = require('stream'); -var tinytap = require('./'); +var tinytap = require('./parser.js'); var glob = require('glob'); var args = process.argv.slice(2); @@ -68,7 +68,7 @@ console.log('1..' + total); return transform; } - if ('TAP_VERBOSE' in process.env || 'TAPV' in process.env) { + if (true || 'TAP_VERBOSE' in process.env || 'TAPV' in process.env) { proc.stdout.pipe(prefixStream()).pipe(process.stderr); proc.stderr.pipe(prefixStream()).pipe(process.stderr); } diff --git a/package.json b/package.json index afb32be..f2fb04f 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "tinytap", - "version": "0.1.0", + "version": "0.1.1", "description": "Tiny tap parser and tools.", - "main": "index.js", + "main": "./lib/tester/tester.js", "bin": { - "tinytap": "./tinytap.js" + "tinytap": "./lib/testrunner/runner.js" }, "dependencies": { "glob": "^4.0.0", diff --git a/test.sh b/test/test.sh old mode 100755 new mode 100644 similarity index 64% rename from test.sh rename to test/test.sh index dceeebb..8fbd36d --- a/test.sh +++ b/test/test.sh @@ -1,4 +1,4 @@ -cat test/colony.tap | node index.js - > out.tap +cat colony.tap | node index.js - > out.tap cat out.tap | node index.js - > out2.tap diff out.tap out2.tap rm out.tap From 26e0301183c914bf6f8ad6113d1396c8e641a304 Mon Sep 17 00:00:00 2001 From: johnnyman727 Date: Tue, 22 Jul 2014 16:16:39 -0700 Subject: [PATCH 2/3] Remove debug flag --- lib/testrunner/runner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/testrunner/runner.js b/lib/testrunner/runner.js index 5e0f138..b2b3a1c 100755 --- a/lib/testrunner/runner.js +++ b/lib/testrunner/runner.js @@ -68,7 +68,7 @@ console.log('1..' + total); return transform; } - if (true || 'TAP_VERBOSE' in process.env || 'TAPV' in process.env) { + if ('TAP_VERBOSE' in process.env || 'TAPV' in process.env) { proc.stdout.pipe(prefixStream()).pipe(process.stderr); proc.stderr.pipe(prefixStream()).pipe(process.stderr); } From 674994ef3a9de4847b7372e6f9c324f3acc0323b Mon Sep 17 00:00:00 2001 From: Tim Cameron Ryan Date: Tue, 22 Jul 2014 16:39:07 -0700 Subject: [PATCH 3/3] Reorganizes structure, adds test.count. --- package.json | 10 +++++----- {lib/testrunner => src}/parser.js | 0 {lib/testrunner => src}/runner.js | 3 ++- {lib/tester => src}/tester.js | 9 +++++---- test/test.sh | 4 ++-- 5 files changed, 14 insertions(+), 12 deletions(-) rename {lib/testrunner => src}/parser.js (100%) rename {lib/testrunner => src}/runner.js (98%) rename {lib/tester => src}/tester.js (95%) mode change 100644 => 100755 test/test.sh diff --git a/package.json b/package.json index f2fb04f..7089839 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,17 @@ { "name": "tinytap", - "version": "0.1.1", - "description": "Tiny tap parser and tools.", - "main": "./lib/tester/tester.js", + "version": "0.2.0", + "description": "Tiny TAP tester and test runner.", + "main": "./src/tester.js", "bin": { - "tinytap": "./lib/testrunner/runner.js" + "tinytap": "./src/runner.js" }, "dependencies": { "glob": "^4.0.0", "shell-quote": "^1.4.1" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "cd test; ./test.sh" }, "author": "", "license": "MIT", diff --git a/lib/testrunner/parser.js b/src/parser.js similarity index 100% rename from lib/testrunner/parser.js rename to src/parser.js diff --git a/lib/testrunner/runner.js b/src/runner.js similarity index 98% rename from lib/testrunner/runner.js rename to src/runner.js index b2b3a1c..45a1196 100755 --- a/lib/testrunner/runner.js +++ b/src/runner.js @@ -4,9 +4,10 @@ var spawn = require('child_process').spawn; var parse = require('shell-quote').parse; var quote = require('shell-quote').quote; var stream = require('stream'); -var tinytap = require('./parser.js'); var glob = require('glob'); +var tinytap = require('../src/parser'); + var args = process.argv.slice(2); var list = [[null]]; diff --git a/lib/tester/tester.js b/src/tester.js similarity index 95% rename from lib/tester/tester.js rename to src/tester.js index 8d34a3e..a91546d 100644 --- a/lib/tester/tester.js +++ b/src/tester.js @@ -1,4 +1,3 @@ -// tiny node-tap lookalike. module.exports = test var tests = module.exports.tests = []; @@ -17,6 +16,10 @@ function test(name, fn) { var assert = require('assert'); Error.captureStackTrace = function(){}; +test.count = function (n) { + console.log('1..' + Number(n)); +} + var t = Object.keys(assert).map(function (k) { if (typeof assert[k] !== 'function') return; return [k, function () { @@ -77,7 +80,7 @@ t.test = function(name, fn) { children.push([name, fn]) } -function run() { +function run () { if (children.length) { tests.unshift.apply(tests, children) children.length = 0 @@ -85,8 +88,6 @@ function run() { var next = tests.shift(); if (!next) { - console.log('0..', id) - console.log('') console.log('# pass', pass, pass + fail) console.log('# fail', fail, pass + fail) process.exit(fail) diff --git a/test/test.sh b/test/test.sh old mode 100644 new mode 100755 index 8fbd36d..68fd0b5 --- a/test/test.sh +++ b/test/test.sh @@ -1,5 +1,5 @@ -cat colony.tap | node index.js - > out.tap -cat out.tap | node index.js - > out2.tap +cat colony.tap | node ../src/runner.js - > out.tap +cat out.tap | node ../src/runner.js - > out2.tap diff out.tap out2.tap rm out.tap rm out2.tap