Skip to content

Commit

Permalink
feat(config): default config can be karma.conf.js or karma.conf.coffee
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vojtajina committed Aug 2, 2013
1 parent f499f7b commit d4a06f2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
19 changes: 15 additions & 4 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -194,7 +205,7 @@ exports.process = function() {
process.exit(1);
}

return processArgs(argv, options);
return processArgs(argv, options, fs, path);
};

// just for testing
Expand Down
42 changes: 38 additions & 4 deletions test/unit/cli.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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', ->

Expand All @@ -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']

Expand Down Expand Up @@ -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', ->
Expand Down

0 comments on commit d4a06f2

Please sign in to comment.