Skip to content

Commit

Permalink
fix(ConfigReader): Use CLI options with default config file
Browse files Browse the repository at this point in the history
Change ConfigReader behavior to always use stryker.conf.js if present. CLI
options override options from the file.

Run does not fail anymore if a CLI option, but no config file is specified.
closes stryker-mutator#390
  • Loading branch information
Be-ngt-oH committed Oct 2, 2017
1 parent 6258ef1 commit 5ac8d0a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
26 changes: 17 additions & 9 deletions packages/stryker/src/ConfigReader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Config } from 'stryker-api/config';
import { StrykerOptions } from 'stryker-api/core';
import * as fs from 'mz/fs';
import * as log4js from 'log4js';
import * as _ from 'lodash';

Expand All @@ -11,6 +12,8 @@ export const CONFIG_SYNTAX_HELP = ' module.exports = function(config) {\n' +
' });\n' +
' };';

const DEFAULT_CONFIG_FILE = 'stryker.conf.js';

const log = log4js.getLogger('ConfigReader');

export default class ConfigReader {
Expand All @@ -34,10 +37,21 @@ export default class ConfigReader {
}

private loadConfigModule(): Function {
// we start with a dummy configModule
// Dummy module to be returned if no config file is loaded.
let configModule: Function = function () { };

if (!this.cliOptions.configFile) {
try {
fs.accessSync(`${process.cwd()}/${DEFAULT_CONFIG_FILE}`);
log.info(`Using ${DEFAULT_CONFIG_FILE} in the current working directory.`);
this.cliOptions.configFile = DEFAULT_CONFIG_FILE;
} catch (e) {
log.info('No config file specified. Running with command line arguments.');
}
}

if (this.cliOptions.configFile) {
log.debug('Loading config %s', this.cliOptions.configFile);
log.debug(`Loading config ${this.cliOptions.configFile}`);
try {
configModule = require(`${process.cwd()}/${this.cliOptions.configFile}`);
} catch (e) {
Expand All @@ -55,14 +69,8 @@ export default class ConfigReader {
log.fatal('Config file must export a function!\n' + CONFIG_SYNTAX_HELP);
process.exit(1);
}
} else if (Object.keys(this.cliOptions).length === 0) {
log.info('Using stryker.conf.js in the current working directory.');
this.cliOptions.configFile = 'stryker.conf.js';
return this.loadConfigModule();
} else {
log.info('No config file specified. Running with command line arguments');
// if no config file path is passed, we create and return a dummy config module.
}

return configModule;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ describe('ConfigReader', () => {
});

describe('without a stryker.conf.js in the CWD', () => {
it('should report a fatal error', () => {
it('should return default config', () => {
let mockCwd = process.cwd() + '/testResources/config-reader/no-config';
sandbox.stub(process, 'cwd').returns(mockCwd);

sut = new ConfigReader({});

result = sut.readConfig();

expect(log.fatal).to.have.been.calledWith(`File ${mockCwd}/stryker.conf.js does not exist!`);
expect(result).to.deep.equal(new Config());
});
});
});
Expand All @@ -77,17 +78,25 @@ describe('ConfigReader', () => {
});

describe('with config file', () => {

beforeEach(() => {
it('should read config file', () => {
sut = new ConfigReader({ configFile: 'testResources/config-reader/valid.conf.js' });

result = sut.readConfig();
});

it('should read config file', () => {
expect(result['valid']).to.be.eq('config');
expect(result['should']).to.be.eq('be');
expect(result['read']).to.be.eq(true);
});

describe('with CLI options', () => {
it('should give precedence to CLI options', () => {
sut = new ConfigReader({ configFile: 'testResources/config-reader/valid.conf.js', read: false });

result = sut.readConfig();

expect(result['read']).to.be.eq(false);
});
});
});

describe('with non-existing config file', () => {
Expand Down

0 comments on commit 5ac8d0a

Please sign in to comment.