Skip to content

Commit

Permalink
feat(ConfigReader): Look for stryker.conf.js in the CWD (#209)
Browse files Browse the repository at this point in the history
Look for stryker.conf.js in the CWD
  • Loading branch information
simondel authored and nicojs committed Dec 30, 2016
1 parent a114594 commit d196fd3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
20 changes: 12 additions & 8 deletions src/ConfigReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const log = log4js.getLogger('ConfigReader');

export default class ConfigReader {

constructor(private options: StrykerOptions) { }
constructor(private cliOptions: StrykerOptions) { }

readConfig() {
const configModule = this.loadConfigModule();
Expand All @@ -29,20 +29,20 @@ export default class ConfigReader {
}

// merge the config from config file and cliOptions (precedence)
config.set(this.options);
config.set(this.cliOptions);
this.validate(config);
return config;
}

private loadConfigModule(): Function {
let configModule: Function;
if (this.options.configFile) {
log.debug('Loading config %s', this.options.configFile);
if (this.cliOptions.configFile) {
log.debug('Loading config %s', this.cliOptions.configFile);
try {
configModule = require(`${process.cwd()}/${this.options.configFile}`);
configModule = require(`${process.cwd()}/${this.cliOptions.configFile}`);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND' && e.message.indexOf(this.options.configFile) !== -1) {
log.fatal('File %s does not exist!', this.options.configFile);
if (e.code === 'MODULE_NOT_FOUND' && e.message.indexOf(this.cliOptions.configFile) !== -1) {
log.fatal(`File ${process.cwd()}/${this.cliOptions.configFile} does not exist!`);
log.fatal(e);
} else {
log.fatal('Invalid config file!\n ' + e.stack);
Expand All @@ -53,8 +53,12 @@ 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.debug('No config file specified.');
log.info('No config file specified. Running with command line arguments');
// if no config file path is passed, we define a dummy config module.
configModule = function () { };
}
Expand Down
34 changes: 31 additions & 3 deletions test/integration/config-reader/ConfigReaderSpec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {expect} from 'chai';
import { expect } from 'chai';
import * as log4js from 'log4js';
import * as sinon from 'sinon';
import ConfigReader from '../../../src/ConfigReader';
import {Config} from 'stryker-api/config';
import { Config } from 'stryker-api/config';
import log from '../../helpers/log4jsMock';

describe('ConfigReader', () => {
Expand Down Expand Up @@ -33,6 +33,34 @@ describe('ConfigReader', () => {
});
});

describe('without config file or CLI options', () => {
describe('with a stryker.conf.js in the CWD', () => {
it('should parse the config', () => {
let mockCwd = process.cwd() + '/testResources/config-reader';
sandbox.stub(process, 'cwd').returns(mockCwd);
sut = new ConfigReader({});

result = sut.readConfig();

expect(result['valid']).to.be.eq('config');
expect(result['should']).to.be.eq('be');
expect(result['read']).to.be.eq(true);
});
});

describe('without a stryker.conf.js in the CWD', () => {
it('should report a fatal error', () => {
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!`);
});
});
});

describe('with invalid coverageAnalysis', () => {
beforeEach(() => {
sut = new ConfigReader({ coverageAnalysis: <any>'invalid' });
Expand Down Expand Up @@ -69,7 +97,7 @@ describe('ConfigReader', () => {
});

it('should report a fatal error', () => {
expect(log.fatal).to.have.been.calledWith('File %s does not exist!', '/did/you/know/that/this/file/does/not/exists/questionmark');
expect(log.fatal).to.have.been.calledWith(`File ${process.cwd()}//did/you/know/that/this/file/does/not/exists/questionmark does not exist!`);
});

it('should exit with 1', () => {
Expand Down
8 changes: 8 additions & 0 deletions testResources/config-reader/stryker.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

module.exports = function(config){
config.set({
'valid': 'config',
'should': 'be',
'read': true
});
};
9 changes: 1 addition & 8 deletions testResources/config-reader/valid.conf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@

module.exports = function(config){
config.set({
'valid': 'config',
'should': 'be',
'read': true
});
};
module.exports = require('./stryker.conf.js');

0 comments on commit d196fd3

Please sign in to comment.