From 6987222ad19d88edf4f01b99a247e70f8702f40c Mon Sep 17 00:00:00 2001 From: qatechniker <34143347+qatechniker@users.noreply.github.com> Date: Mon, 30 Jul 2018 03:58:26 +0200 Subject: [PATCH] Origin/issue #183 add support grep option (#187) * - add support option grep - add test * - add support grep option - add test for new option * add test in list * Add grep flag to runner and mocha ts * back tests * final fixes to make working grep according to pull request comments --- src/bin/cli.ts | 7 +++++++ src/bin/options/grep.ts | 1 + src/main/mocha.ts | 4 ++++ src/subprocess/runner.ts | 5 +++++ src/util.ts | 6 ++++++ test/grep/README | 1 + test/grep/index.js | 27 +++++++++++++++++++++++++++ test/grep/tests/test.js | 19 +++++++++++++++++++ test/grep/tests/test1.js | 14 ++++++++++++++ test/index.sh | 1 + 10 files changed, 85 insertions(+) create mode 100644 src/bin/options/grep.ts create mode 100644 test/grep/README create mode 100755 test/grep/index.js create mode 100644 test/grep/tests/test.js create mode 100644 test/grep/tests/test1.js diff --git a/src/bin/cli.ts b/src/bin/cli.ts index 7bba1de..84f88f2 100755 --- a/src/bin/cli.ts +++ b/src/bin/cli.ts @@ -14,6 +14,7 @@ import applyExit from './options/exit'; import applyForbidOnly from './options/forbid-only'; import applyForbidPending from './options/forbid-pending'; import applyFullTrace from './options/full-trace'; +import applyGrepPattern from './options/grep'; import applyMaxParallel from './options/max-parallel'; import applyNoTimeouts from './options/no-timeouts'; import applyReporter from './options/reporter'; @@ -65,6 +66,9 @@ const argv = yargs .option('max-parallel', { number: true, }) + .option('grep', { + string: true, + }) .option('recursive', { boolean: true, }) @@ -122,6 +126,9 @@ applyForbidPending(mocha, argv.forbidPending); // --full-trace applyFullTrace(mocha, argv.fullTrace); +// --grep option +applyGrepPattern(mocha, argv.grep); + // --max-parallel applyMaxParallel(mocha, argv.maxParallel); diff --git a/src/bin/options/grep.ts b/src/bin/options/grep.ts new file mode 100644 index 0000000..ee6ab7d --- /dev/null +++ b/src/bin/options/grep.ts @@ -0,0 +1 @@ +export { applyGrepPattern as default } from '../../util'; diff --git a/src/main/mocha.ts b/src/main/mocha.ts index 1bd4564..e2af14c 100644 --- a/src/main/mocha.ts +++ b/src/main/mocha.ts @@ -187,6 +187,10 @@ export default class MochaWrapper extends Mocha { forkArgs.push('--delay'); } + if (this.options.grep) { + forkArgs.push('--grep', this.options.grep.toString()); + } + if (this.exitImmediately) { forkArgs.push('--exit'); } diff --git a/src/subprocess/runner.ts b/src/subprocess/runner.ts index e35b52e..0ffdbb8 100755 --- a/src/subprocess/runner.ts +++ b/src/subprocess/runner.ts @@ -18,6 +18,7 @@ import { SUITE_OWN_OPTIONS } from '../config'; import { applyCompilers, applyDelay, + applyGrepPattern, applyNoTimeouts, applyRequires, applyTimeouts, @@ -33,6 +34,7 @@ const argv = yargs default: [], }) .boolean('delay') + .string('grep') .boolean('enableTimeouts') .option('exit', { boolean: true, @@ -204,6 +206,9 @@ applyCompilers(argv.compilers); // --delay applyDelay(mocha, argv.delay); +// --grep +applyGrepPattern(mocha, argv.grep); + // --enableTimeouts applyNoTimeouts(mocha, argv.enableTimeouts); diff --git a/src/util.ts b/src/util.ts index 835a4a6..7319d0c 100644 --- a/src/util.ts +++ b/src/util.ts @@ -73,6 +73,12 @@ export function applyDelay(mocha: Mocha, delay: boolean) { } } +export function applyGrepPattern(mocha: Mocha, stringPattern?: string) { + if (stringPattern) { + mocha.grep(stringPattern); + } +} + export function applyNoTimeouts(mocha: Mocha, allowTimeouts: boolean) { if (allowTimeouts === false) { mocha.enableTimeouts(false); diff --git a/test/grep/README b/test/grep/README new file mode 100644 index 0000000..821e739 --- /dev/null +++ b/test/grep/README @@ -0,0 +1 @@ +Check that --grep flag run tests by string pattern 'grep' diff --git a/test/grep/index.js b/test/grep/index.js new file mode 100755 index 0000000..828ee3d --- /dev/null +++ b/test/grep/index.js @@ -0,0 +1,27 @@ +#!/usr/bin/env node + +var assert = require('assert'); +var path = require('path'); +var exec = require('child_process').exec; +var libExecutable = path.resolve(__dirname, '../../dist/bin/cli.js'); + +exec(libExecutable + ' -R json --grep grep test/grep/tests', { + cwd: path.resolve(__dirname, '../../') +}, function (err, stderr) { + if (err) { + console.error(err); + process.exit(1); + } + + var jsonReporterOutput = stderr.toString(); + + try { + jsonReporterOutput = JSON.parse(jsonReporterOutput); + } catch (ex) { + console.error('Native JSON reporter output is not valid JSON: ' + jsonReporterOutput); + process.exit(1); + } + + assert.strictEqual(jsonReporterOutput.stats.passes, 3, 'Tests number is wrong. Expected 3. Actual: ' + + jsonReporterOutput.stats.passes); +}); diff --git a/test/grep/tests/test.js b/test/grep/tests/test.js new file mode 100644 index 0000000..408acae --- /dev/null +++ b/test/grep/tests/test.js @@ -0,0 +1,19 @@ +'use strict'; + +const assert = require('assert'); + +describe('tests', () => { + it('should be run 1. grep', () => { + assert.ok(true); + }); + + it('should be skipped', () => { + assert.ok(true); + }); +}); + +describe('tests should be run. grep', () => { + it('should be run 2', () => { + assert.ok(true); + }); +}); diff --git a/test/grep/tests/test1.js b/test/grep/tests/test1.js new file mode 100644 index 0000000..2d63054 --- /dev/null +++ b/test/grep/tests/test1.js @@ -0,0 +1,14 @@ +'use strict'; + +const assert = require('assert'); + +describe('tests file 2', () => { + it('should be run 3. grep', () => { + assert.ok(true); + }); + + it('should be skipped', () => { + assert.ok(true); + }); +}); + diff --git a/test/index.sh b/test/index.sh index a932f7a..9c85ef5 100755 --- a/test/index.sh +++ b/test/index.sh @@ -76,6 +76,7 @@ test 'TESTCASE: --retries and --bail should work well together' test/bail-and-re test 'TESTCASE: subprocess exits before sending an end message' test/no-subprocess-end/index.js test 'TESTCASE: unhandled rejections should not force subprocess to exit' test/q-promises/index.js test 'TESTCASE: uncaught exceptions should not force subprocess to exit' test/uncaught-exception/index.js +test 'TESTCASE: grep option' test/grep/index.js echo "Passes: $PASSES Failes: $FAILES" echo ""