Skip to content

Commit

Permalink
feat(ConfigReader): Use CLI options with default config file (#404)
Browse files Browse the repository at this point in the history
Use the stryker.conf.js file in the current working directory, if one is present. Display 'Use `stryker init` command to generate your config file.' when stryker is run without a config file.

Closes #390
  • Loading branch information
Be-ngt-oH authored and nicojs committed Oct 3, 2017
1 parent 6258ef1 commit 99cdc61
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
28 changes: 19 additions & 9 deletions packages/stryker/src/ConfigReader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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 path from 'path';
import * as _ from 'lodash';

const VALID_COVERAGE_ANALYSIS_VALUES = ['perTest', 'all', 'off'];
Expand All @@ -11,6 +13,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 +38,22 @@ 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(path.resolve(`./${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.');
log.info('Use `stryker init` command to generate your config file.');
}
}

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 +71,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 99cdc61

Please sign in to comment.