Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ConfigReader): Use CLI options with default config file #404

Merged
merged 3 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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