From d4a06f296c4d805f2dccd85b4898766593af4d66 Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Thu, 1 Aug 2013 19:41:07 -0700 Subject: [PATCH] feat(config): default config can be karma.conf.js or karma.conf.coffee Before Karma would always set the config to `./karma.conf.js` (if not specified otherwise). Now, it checks for `karma.conf.js` and `karma.conf.coffee` and set the config file path ONLY if the file actually exists. --- lib/cli.js | 19 ++++++++++++++---- test/unit/cli.spec.coffee | 42 +++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 659680f7b..29211116e 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -2,9 +2,9 @@ var path = require('path'); var optimist = require('optimist'); var helper = require('./helper'); var constant = require('./constants'); +var fs = require('fs'); - -var processArgs = function(argv, options) { +var processArgs = function(argv, options, fs, path) { if (argv.help) { console.log(optimist.help()); @@ -67,7 +67,18 @@ var processArgs = function(argv, options) { options.refresh = options.refresh === 'true'; } - options.configFile = path.resolve(argv._.shift() || 'karma.conf.js'); + var configFile = argv._.shift(); + + if (!configFile) { + // default config file (if exists) + if (fs.existsSync('./karma.conf.js')) { + configFile = './karma.conf.js'; + } else if (fs.existsSync('./karma.conf.coffee')) { + configFile = './karma.conf.coffee'; + } + } + + options.configFile = configFile ? path.resolve(configFile) : null; return options; }; @@ -194,7 +205,7 @@ exports.process = function() { process.exit(1); } - return processArgs(argv, options); + return processArgs(argv, options, fs, path); }; // just for testing diff --git a/test/unit/cli.spec.coffee b/test/unit/cli.spec.coffee index 5dae993b0..ef227fa8c 100644 --- a/test/unit/cli.spec.coffee +++ b/test/unit/cli.spec.coffee @@ -4,13 +4,31 @@ describe 'cli', -> cli = require '../../lib/cli' optimist = require 'optimist' + path = require 'path' constant = require '../../lib/constants' - CWD = process.cwd() path = require 'path' + mocks = require 'mocks' + + fsMock = mocks.fs.create + cwd: + 'karma.conf.js': true + cwd2: + 'karma.conf.coffee': true + + currentCwd = null + + pathMock = + resolve: (p) -> path.resolve currentCwd, p + + setCWD = (cwd) -> + currentCwd = cwd + fsMock._setCWD cwd processArgs = (args, opts) -> argv = optimist.parse(args) - cli.processArgs argv, opts || {} + cli.processArgs argv, opts || {}, fsMock, pathMock + + beforeEach -> setCWD '/' describe 'processArgs', -> @@ -23,13 +41,28 @@ describe 'cli', -> it 'should parse options without configFile and set default', -> + setCWD '/cwd' options = processArgs ['--auto-watch', '--auto-watch-interval', '10'] - expect(options.configFile).to.equal path.join(CWD, 'karma.conf.js') + expect(options.configFile).to.equal '/cwd/karma.conf.js' expect(options.autoWatch).to.equal true expect(options.autoWatchInterval).to.equal 10 + it 'should set default karma.conf.coffee config file if exists', -> + setCWD '/cwd2' + options = processArgs ['--port', '10'] + + expect(options.configFile).to.equal '/cwd2/karma.conf.coffee' + + + it 'should not set default config if neither exists', -> + setCWD '/' + options = processArgs [] + + expect(options.configFile).to.equal null + + it 'should parse auto-watch, colors, singleRun to boolean', -> options = processArgs ['--auto-watch', 'false', '--colors', 'false', '--single-run', 'false'] @@ -61,8 +94,9 @@ describe 'cli', -> it 'should resolve configFile to absolute path', -> + setCWD '/cwd' options = processArgs ['some/config.js'] - expect(options.configFile).to.equal path.join(CWD, '/some/config.js') + expect(options.configFile).to.equal '/cwd/some/config.js' it 'should parse report-slower-than to a number', ->