From 99cdc61c30b1785b18677d33e339abc50017c336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bengt-Ove=20Holl=C3=A4nder?= Date: Tue, 3 Oct 2017 07:00:33 +0200 Subject: [PATCH] feat(ConfigReader): Use CLI options with default config file (#404) 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 --- packages/stryker/src/ConfigReader.ts | 28 +++++++++++++------ .../config-reader/ConfigReaderSpec.ts | 21 ++++++++++---- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/packages/stryker/src/ConfigReader.ts b/packages/stryker/src/ConfigReader.ts index 55c9ffd94c..904259714e 100644 --- a/packages/stryker/src/ConfigReader.ts +++ b/packages/stryker/src/ConfigReader.ts @@ -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']; @@ -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 { @@ -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) { @@ -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; } diff --git a/packages/stryker/test/integration/config-reader/ConfigReaderSpec.ts b/packages/stryker/test/integration/config-reader/ConfigReaderSpec.ts index e200ffe15c..6a56dd4640 100644 --- a/packages/stryker/test/integration/config-reader/ConfigReaderSpec.ts +++ b/packages/stryker/test/integration/config-reader/ConfigReaderSpec.ts @@ -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()); }); }); }); @@ -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', () => {