Skip to content

Commit

Permalink
This implements -r and --require as command line options
Browse files Browse the repository at this point in the history
@Raynos accurately pointed out that the source map support was a
bit too specific, and felt outside of the purview of tape.
As a more general solution, this implements the `-r` and `--require`
CLI flags, which will allow a user to load node modules before any
tests are run. For example, if someon wants to enable source map
support in node, they might do:

    tape -r source-map-support/register test/*.js
  • Loading branch information
mstade committed Dec 21, 2015
1 parent 3f02033 commit 7ae60f5
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 3 deletions.
26 changes: 23 additions & 3 deletions bin/tape
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
#!/usr/bin/env node

var path = require('path');
var resolveModule = require('resolve').sync;
var resolvePath = require('path').resolve;
var parseOpts = require('minimist');
var glob = require('glob');

process.argv.slice(2).forEach(function (arg) {
var opts = parseOpts(process.argv.slice(2), {
alias: { 'r': 'require' },
string: 'require',
default: {'r': [] }
});

var cwd = process.cwd();

/* If only one require is specified, the value of `opts.require`
* will be a string. This is why we concatenate.
*/
;[].concat(opts.require).forEach(function(module) {
/* The `module &&` ensures we ignore `-r ""`, trailing `-r` or other
* silly things the user might (inadvertedly) be doing.
*/
module && require(resolveModule(module, { basedir: cwd }));
});

opts._.forEach(function (arg) {
glob(arg, function (err, files) {
files.forEach(function (file) {
require(path.resolve(process.cwd(), file));
require(resolvePath(cwd, file));
});
});
});
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"glob": "~5.0.3",
"has": "~1.0.1",
"inherits": "~2.0.1",
"minimist": "1.2.0",
"object-inspect": "~1.0.0",
"resolve": "1.1.6",
"resumer": "~0.0.0",
"string.prototype.trim": "^1.1.1",
"through": "~2.3.4"
Expand Down
83 changes: 83 additions & 0 deletions test/require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
var tap = require('tap');
var spawn = require('child_process').spawn;
var trim = require('string.prototype.trim');

tap.test('requiring a single module', function (t) {
t.plan(2);

var tc = tap.createConsumer();

var rows = [];
tc.on('data', function (r) { rows.push(r) });
tc.on('end', function () {
var rs = rows.map(function (r) {
if (r && typeof r === 'object') {
return { id : r.id, ok : r.ok, name : trim(r.name) };
}
else return r;
});
t.same(rs, [
'TAP version 13',
'module-a',
{ id: 1, ok: true, name: 'loaded module a' },
'test-a',
{ id: 2, ok: true, name: 'module-a loaded in same context'},
{ id: 3, ok: true, name: 'test ran after module-a was loaded'},
'tests 3',
'pass 3',
'ok'
]);
});

var ps = tape('-r ./require/a require/test-a.js');
ps.stdout.pipe(tc);
ps.on('exit', function (code) {
t.equal(code, 0);
});
});

tap.test('requiring multiple modules', function (t) {
t.plan(2);

var tc = tap.createConsumer();

var rows = [];
tc.on('data', function (r) { rows.push(r) });
tc.on('end', function () {
var rs = rows.map(function (r) {
if (r && typeof r === 'object') {
return { id : r.id, ok : r.ok, name : trim(r.name) };
}
else return r;
});
t.same(rs, [
'TAP version 13',
'module-a',
{ id: 1, ok: true, name: 'loaded module a' },
'module-b',
{ id: 2, ok: true, name: 'loaded module b' },
'test-a',
{ id: 3, ok: true, name: 'module-a loaded in same context'},
{ id: 4, ok: true, name: 'test ran after module-a was loaded'},
'test-b',
{ id: 5, ok: true, name: 'module-b loaded in same context'},
{ id: 6, ok: true, name: 'test ran after module-b was loaded'},
'tests 6',
'pass 6',
'ok'
]);
});

var ps = tape('-r ./require/a -r ./require/b require/test-a.js require/test-b.js');
ps.stdout.pipe(tc);
ps.on('exit', function (code) {
t.equal(code, 0);
});
});

function tape(args) {
var proc = require('child_process')
var bin = __dirname + '/../bin/tape'

return proc.spawn(bin, args.split(' '), { cwd: __dirname })
}
8 changes: 8 additions & 0 deletions test/require/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var tape = require('../..');

tape.test('module-a', function(t) {
t.pass('loaded module a')
t.end()
})

global.module_a = true
8 changes: 8 additions & 0 deletions test/require/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var tape = require('../..');

tape.test('module-b', function(t) {
t.pass('loaded module b')
t.end()
})

global.module_b = true
7 changes: 7 additions & 0 deletions test/require/test-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var tape = require('../..');

tape.test('test-a', function(t) {
t.ok(global.module_a, 'module-a loaded in same context')
t.pass('test ran after module-a was loaded')
t.end()
})
7 changes: 7 additions & 0 deletions test/require/test-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var tape = require('../..');

tape.test('test-b', function(t) {
t.ok(global.module_b, 'module-b loaded in same context')
t.pass('test ran after module-b was loaded')
t.end()
})

0 comments on commit 7ae60f5

Please sign in to comment.