Skip to content

Commit

Permalink
feat: extractArgv refactor & extract debug port
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Mar 29, 2017
1 parent 6f5d525 commit 994616d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 37 deletions.
11 changes: 6 additions & 5 deletions lib/cmd/cov.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class CovCommand extends Command {
return 'Run test with coverage';
}

* run({ cwd, argv, execArgv }) {
* run(context) {
const { cwd, argv, execArgv } = context;
const tmpDir = path.join(cwd, '.tmp');
yield mkdirp(tmpDir);

Expand All @@ -61,7 +62,7 @@ class CovCommand extends Command {
};

// save coverage-xxxx.json to $PWD/coverage
const covArgs = this.getCovArgs(argv);
const covArgs = this.getCovArgs(context);
debug('covArgs: %j', covArgs);
yield this.helper.forkNode(covFile, covArgs, opt);
yield rimraf(tmpDir);
Expand All @@ -81,11 +82,11 @@ class CovCommand extends Command {

/**
* get coverage args
* @param {Object} argv - use to extract test args
* @param {Object} context - { cwd, argv, ...}
* @return {Array} args for istanbul
* @protected
*/
getCovArgs(argv) {
getCovArgs(context) {
const covArgs = [
'cover',
'--report', 'none',
Expand All @@ -98,7 +99,7 @@ class CovCommand extends Command {
covArgs.push(exclude);
}
const mochaFile = require.resolve('mocha/bin/_mocha');
const testArgs = this.formatTestArgs(argv);
const testArgs = this.formatTestArgs(context);
debug('testArgs: %j', testArgs);
return covArgs.concat(mochaFile, '--', ...testArgs);
}
Expand Down
46 changes: 26 additions & 20 deletions lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,39 @@ class TestCommand extends Command {
return 'Run test with mocha';
}

* run({ argv, execArgv }) {
* run(context) {
process.env.NODE_ENV = 'test';
const newArgs = this.formatTestArgs(argv);
const testArgs = this.formatTestArgs(context);
const opt = {
env: Object.assign({}, process.env),
execArgv,
execArgv: context.execArgv,
};
const mochaFile = require.resolve('mocha/bin/_mocha');
debug('run test: %s %s', mochaFile, newArgs.join(' '));
yield this.helper.forkNode(mochaFile, newArgs, opt);
debug('run test: %s %s', mochaFile, testArgs.join(' '));
yield this.helper.forkNode(mochaFile, testArgs, opt);
}

/**
* format test args then change it to array style
* @method helper#formatTestArgs
* @param {Object} argv - yargs style
* @param {Object} context - { cwd, argv, ...}
* @return {Array} [ '--require=xxx', 'xx.test.js' ]
* @protected
*/
formatTestArgs(argv) {
const newArgv = Object.assign({}, argv);
formatTestArgs({ argv, debug }) {
const testArgv = Object.assign({}, argv);

/* istanbul ignore next */
newArgv.timeout = newArgv.timeout || process.env.TEST_TIMEOUT || '30000';
newArgv.reporter = newArgv.reporter || process.env.TEST_REPORTER;
testArgv.timeout = testArgv.timeout || process.env.TEST_TIMEOUT || 30000;
testArgv.reporter = testArgv.reporter || process.env.TEST_REPORTER;

if (debug) {
// --no-timeouts
testArgv.timeouts = false;
testArgv.timeout = undefined;
}

// collect require
let requireArr = newArgv.require || newArgv.r || [];
let requireArr = testArgv.require || testArgv.r || [];
/* istanbul ignore next */
if (!Array.isArray(requireArr)) requireArr = [ requireArr ];

Expand All @@ -71,10 +77,10 @@ class TestCommand extends Command {
requireArr.push(require.resolve('intelli-espower-loader'));
}

newArgv.require = requireArr;
testArgv.require = requireArr;

// collect test files
let files = newArgv._.slice();
let files = testArgv._.slice();
if (!files.length) {
files = [ process.env.TESTS || 'test/**/*.test.js' ];
}
Expand All @@ -86,15 +92,15 @@ class TestCommand extends Command {
if (fs.existsSync(setupFile)) {
files.unshift(setupFile);
}
newArgv._ = files;
testArgv._ = files;

// remove alias
newArgv.$0 = undefined;
newArgv.r = undefined;
newArgv.t = undefined;
newArgv.g = undefined;
testArgv.$0 = undefined;
testArgv.r = undefined;
testArgv.t = undefined;
testArgv.g = undefined;

return this.helper.unparseArgv(newArgv);
return this.helper.unparseArgv(testArgv);
}
}

Expand Down
37 changes: 28 additions & 9 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,37 @@ class Command extends BaseCommand {
const argv = context.argv;

// extract execArgv to special item
context.execArgv = this.helper.unparseArgv(argv, {
includes: [ 'debug', 'debug-brk', 'inspect', 'inspect-brk', 'es_staging', 'expose_debug_as', /^harmony.*/ ],
});
const execArgvObj = {};
let debugPort;
const match = (key, arr) => arr.some(x => x instanceof RegExp ? x.test(key) : x === key); // eslint-disable-line no-confusing-arrow
for (const key of Object.keys(argv)) {
let isMatch = false;

// debug / debug-brk / debug-port / inspect / inspect-brk / inspect-port
if (match(key, [ /^debug.*/, /^inspect.*/ ])) {
isMatch = true;
// extract debug port
if (debugPort === undefined || typeof argv[key] === 'number') {
debugPort = argv[key];
}
} else if (match(key, [ 'es_staging', 'expose_debug_as', /^harmony.*/ ])) {
isMatch = true;
}

if (isMatch) {
execArgvObj[key] = argv[key];
argv[key] = undefined;
// also remove `debugBrk`
argv[changeCase.camelCase(key)] = undefined;
}
}

context.execArgv = this.helper.unparseArgv(execArgvObj);
context.execArgvObj = execArgvObj;
context.debug = debugPort;

// remove unuse args
argv.$0 = undefined;
for (const item of context.execArgv) {
// --debug=7000 => debug
const key = item.replace(/--([^=]*)=?.*/, '$1');
argv[key] = undefined;
argv[changeCase.camelCase(key)] = undefined;
}

return context;
}
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/my-egg-bin/lib/cmd/test-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const Command = require('../../../../../').TestCommand;

class TestDebugCommand extends Command {
get description() {
return 'test';
}

* run(context) {
const testArgs = this.formatTestArgs(context);
console.log('%j', testArgs);
}
}

module.exports = TestDebugCommand;
2 changes: 1 addition & 1 deletion test/lib/cmd/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('test/lib/cmd/debug.test.js', () => {

it('should startCluster success', done => {
coffee.fork(eggBin, [ 'debug' ], { cwd })
// .debug()
.expect('stderr', /Debugger listening/)
.expect('stderr', /chrome-devtools:/)
.expect('stdout', /"workers":1/)
.expect('code', 0)
.end(done);
Expand Down
21 changes: 19 additions & 2 deletions test/my-egg-bin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,33 @@ describe('test/my-egg-bin.test.js', () => {
const args = [
'echo',
'--baseDir=./dist',
'--debug=5555', '--debug-brk',
'--debug', '--debug-brk=5555',
'--expose_debug_as=v8debug',
'--inspect', '6666', '--inspect-brk',
'--es_staging', '--harmony', '--harmony_default_parameters',
];
coffee.fork(eggBin, args, { cwd })
// .debug()
.expect('stdout', /"baseDir":".\/dist"/)
.expect('stdout', /"debug":6666/)
.notExpect('stdout', /"debugBrk":true/)
.expect('stdout', /"execArgv":\["--debug=5555","--debug-brk","--expose_debug_as=v8debug","--inspect=6666","--inspect-brk","--es_staging","--harmony","--harmony_default_parameters"]/)
.expect('stdout', /"execArgv":\["--debug","--debug-brk=5555","--expose_debug_as=v8debug","--inspect=6666","--inspect-brk","--es_staging","--harmony","--harmony_default_parameters"]/)
.expect('code', 0)
.end(done);
});

it('should add no-timeouts at test when debug enabled', done => {
const args = [
'test-debug',
'--baseDir=./dist',
'--debug', '--debug-brk=5555',
'--expose_debug_as=v8debug',
'--inspect', '6666', '--inspect-brk',
];
coffee.fork(eggBin, args, { cwd })
// .debug()
.expect('stdout', /"--no-timeouts",/)
.notExpect('stdout', /"--timeout=/)
.expect('code', 0)
.end(done);
});
Expand Down

0 comments on commit 994616d

Please sign in to comment.