From e0e2542240875e5dad2ba12b1edc4be3251ae571 Mon Sep 17 00:00:00 2001 From: Ram Damera Date: Tue, 7 Jan 2020 13:40:59 +0530 Subject: [PATCH] [New] `tape` binary: Add -i flag to ignore files from gitignore (#492) --- bin/tape | 22 ++++- package.json | 1 + test/ignore/.ignore | 1 + test/ignore/fake_node_modules/stub1.js | 8 ++ test/ignore/fake_node_modules/stub2.js | 8 ++ test/ignore/test.js | 8 ++ test/ignore/test/stub1.js | 8 ++ test/ignore/test/stub2.js | 8 ++ test/ignore/test/sub/sub.stub1.js | 8 ++ test/ignore/test/sub/sub.stub2.js | 8 ++ test/ignore/test2.js | 8 ++ test/ignore_from_gitignore.js | 122 +++++++++++++++++++++++++ 12 files changed, 205 insertions(+), 5 deletions(-) create mode 100644 test/ignore/.ignore create mode 100644 test/ignore/fake_node_modules/stub1.js create mode 100644 test/ignore/fake_node_modules/stub2.js create mode 100644 test/ignore/test.js create mode 100644 test/ignore/test/stub1.js create mode 100644 test/ignore/test/stub2.js create mode 100644 test/ignore/test/sub/sub.stub1.js create mode 100644 test/ignore/test/sub/sub.stub2.js create mode 100644 test/ignore/test2.js create mode 100644 test/ignore_from_gitignore.js diff --git a/bin/tape b/bin/tape index 74f4aba5..3ea1cec5 100755 --- a/bin/tape +++ b/bin/tape @@ -2,14 +2,16 @@ var resolveModule = require('resolve').sync; var resolvePath = require('path').resolve; +var readFileSync = require('fs').readFileSync; var parseOpts = require('minimist'); var glob = require('glob'); +var ignore = require('dotignore'); var opts = parseOpts(process.argv.slice(2), { - alias: { r: 'require' }, - string: 'require', - default: { r: [] } - }); + alias: { r: 'require', i: 'ignore' }, + string: ['require', 'ignore'], + default: { r: [], i: null } +}); var cwd = process.cwd(); @@ -26,6 +28,16 @@ opts.require.forEach(function (module) { } }); +if (typeof opts.ignore === 'string') { + try { + var ignoreStr = readFileSync(resolvePath(cwd, opts.ignore || '.gitignore'), 'utf-8'); + } catch (e) { + console.error(e.message); + process.exit(2); + } + var matcher = ignore.createMatcher(ignoreStr); +} + opts._.forEach(function (arg) { // If glob does not match, `files` will be an empty array. // Note: `glob.sync` may throw an error and crash the node process. @@ -35,7 +47,7 @@ opts._.forEach(function (arg) { throw new TypeError('unknown error: glob.sync did not return an array or throw. Please report this.'); } - files.forEach(function (file) { + files.filter(function (file) { return !matcher || !matcher.shouldIgnore(file); }).forEach(function (file) { require(resolvePath(cwd, file)); }); }); diff --git a/package.json b/package.json index 2fabfaee..664e7674 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "deep-equal": "~1.1.1", "defined": "~1.0.0", + "dotignore": "~0.1.2", "for-each": "~0.3.3", "function-bind": "~1.1.1", "glob": "~7.1.6", diff --git a/test/ignore/.ignore b/test/ignore/.ignore new file mode 100644 index 00000000..ec1cc296 --- /dev/null +++ b/test/ignore/.ignore @@ -0,0 +1 @@ +fake_node_modules diff --git a/test/ignore/fake_node_modules/stub1.js b/test/ignore/fake_node_modules/stub1.js new file mode 100644 index 00000000..6ef0b71c --- /dev/null +++ b/test/ignore/fake_node_modules/stub1.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../'); + +tape.test(function (t) { + t.plan(1); + t.fail('Should not print'); +}); diff --git a/test/ignore/fake_node_modules/stub2.js b/test/ignore/fake_node_modules/stub2.js new file mode 100644 index 00000000..27bebd87 --- /dev/null +++ b/test/ignore/fake_node_modules/stub2.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../'); + +tape.test(function (t) { + t.fail('Should not print'); + t.end(); +}); diff --git a/test/ignore/test.js b/test/ignore/test.js new file mode 100644 index 00000000..f2d6f572 --- /dev/null +++ b/test/ignore/test.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../'); + +tape.test(function (t) { + t.plan(1); + t.ok('Okay'); +}); diff --git a/test/ignore/test/stub1.js b/test/ignore/test/stub1.js new file mode 100644 index 00000000..e91244ee --- /dev/null +++ b/test/ignore/test/stub1.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../'); + +tape.test(function (t) { + t.plan(1); + t.pass('test/stub1'); +}); diff --git a/test/ignore/test/stub2.js b/test/ignore/test/stub2.js new file mode 100644 index 00000000..f4661b88 --- /dev/null +++ b/test/ignore/test/stub2.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../'); + +tape.test(function (t) { + t.pass('test/stub2'); + t.end(); +}); diff --git a/test/ignore/test/sub/sub.stub1.js b/test/ignore/test/sub/sub.stub1.js new file mode 100644 index 00000000..0f39cfee --- /dev/null +++ b/test/ignore/test/sub/sub.stub1.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../../'); + +tape.test(function (t) { + t.plan(1); + t.pass('test/sub/stub1'); +}); diff --git a/test/ignore/test/sub/sub.stub2.js b/test/ignore/test/sub/sub.stub2.js new file mode 100644 index 00000000..bec910d7 --- /dev/null +++ b/test/ignore/test/sub/sub.stub2.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../../'); + +tape.test(function (t) { + t.pass('test/sub/stub2'); + t.end(); +}); diff --git a/test/ignore/test2.js b/test/ignore/test2.js new file mode 100644 index 00000000..a2ceecc0 --- /dev/null +++ b/test/ignore/test2.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../'); + +tape.test(function (t) { + t.pass('Should print'); + t.end(); +}); diff --git a/test/ignore_from_gitignore.js b/test/ignore_from_gitignore.js new file mode 100644 index 00000000..16b076a4 --- /dev/null +++ b/test/ignore_from_gitignore.js @@ -0,0 +1,122 @@ +'use strict'; + +var tap = require('tap'); +var path = require('path'); +var spawn = require('child_process').spawn; +var concat = require('concat-stream'); + +var stripFullStack = require('./common').stripFullStack; + +var tapeBin = path.join(process.cwd(), 'bin/tape'); + +tap.test('Should pass with ignoring', { skip: process.platform === 'win32' }, function (tt) { + tt.plan(2); + + var tc = function (rows) { + tt.same(stripFullStack(rows.toString('utf8')), [ + 'TAP version 13', + '# (anonymous)', + 'ok 1 should be truthy', + '# (anonymous)', + 'ok 2 test/stub1', + '# (anonymous)', + 'ok 3 test/stub2', + '# (anonymous)', + 'ok 4 test/sub/stub1', + '# (anonymous)', + 'ok 5 test/sub/stub2', + '# (anonymous)', + 'ok 6 Should print', + '', + '1..6', + '# tests 6', + '# pass 6', + '', + '# ok', + '', + '' + ].join('\n')); + }; + + var ps = spawn(tapeBin, ['**/*.js', '-i', '.ignore'], {cwd: path.join(__dirname, 'ignore')}); + ps.stdout.pipe(concat(tc)); + ps.on('exit', function (code) { + tt.equal(code, 0); // code 0 + }); +}); + +tap.test('Should pass', { skip: process.platform === 'win32' }, function (tt) { + tt.plan(2); + + var tc = function (rows) { + tt.same(stripFullStack(rows.toString('utf8')), [ + 'TAP version 13', + '# (anonymous)', + 'not ok 1 Should not print', + ' ---', + ' operator: fail', + ' at: Test. ($TEST/ignore/fake_node_modules/stub1.js:$LINE:$COL)', + ' stack: |-', + ' Error: Should not print', + ' [... stack stripped ...]', + ' at Test. ($TEST/ignore/fake_node_modules/stub1.js:$LINE:$COL)', + ' [... stack stripped ...]', + ' ...', + '# (anonymous)', + 'not ok 2 Should not print', + ' ---', + ' operator: fail', + ' at: Test. ($TEST/ignore/fake_node_modules/stub2.js:$LINE:$COL)', + ' stack: |-', + ' Error: Should not print', + ' [... stack stripped ...]', + ' at Test. ($TEST/ignore/fake_node_modules/stub2.js:$LINE:$COL)', + ' [... stack stripped ...]', + ' ...', + '# (anonymous)', + 'ok 3 should be truthy', + '# (anonymous)', + 'ok 4 test/stub1', + '# (anonymous)', + 'ok 5 test/stub2', + '# (anonymous)', + 'ok 6 test/sub/stub1', + '# (anonymous)', + 'ok 7 test/sub/stub2', + '# (anonymous)', + 'ok 8 Should print', + '', + '1..8', + '# tests 8', + '# pass 6', + '# fail 2', + '', + '' + ].join('\n')); + }; + + var ps = spawn(tapeBin, ['**/*.js'], {cwd: path.join(__dirname, 'ignore')}); + ps.stdout.pipe(concat(tc)); + ps.on('exit', function (code) { + tt.equal(code, 1); + }); +}); + +tap.test('Should fail when ignore file does not exist', { skip: process.platform === 'win32' }, function (tt) { + tt.plan(3); + + var testStdout = function (rows) { + tt.same(rows.toString('utf8'), ''); + }; + + var testStderr = function (rows) { + tt.ok(/^ENOENT[:,] no such file or directory,? (?:open )?'\$TEST\/ignore\/.gitignore'\n$/m.test(stripFullStack(rows.toString('utf8')))); + }; + + var ps = spawn(tapeBin, ['**/*.js', '-i'], {cwd: path.join(__dirname, 'ignore')}); + ps.stdout.pipe(concat(testStdout)); + ps.stderr.pipe(concat(testStderr)); + ps.on('exit', function (code) { + tt.equal(code, 2); + }); +});