Skip to content

Commit d4a06f2

Browse files
committed
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.
1 parent f499f7b commit d4a06f2

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

lib/cli.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ var path = require('path');
22
var optimist = require('optimist');
33
var helper = require('./helper');
44
var constant = require('./constants');
5+
var fs = require('fs');
56

6-
7-
var processArgs = function(argv, options) {
7+
var processArgs = function(argv, options, fs, path) {
88

99
if (argv.help) {
1010
console.log(optimist.help());
@@ -67,7 +67,18 @@ var processArgs = function(argv, options) {
6767
options.refresh = options.refresh === 'true';
6868
}
6969

70-
options.configFile = path.resolve(argv._.shift() || 'karma.conf.js');
70+
var configFile = argv._.shift();
71+
72+
if (!configFile) {
73+
// default config file (if exists)
74+
if (fs.existsSync('./karma.conf.js')) {
75+
configFile = './karma.conf.js';
76+
} else if (fs.existsSync('./karma.conf.coffee')) {
77+
configFile = './karma.conf.coffee';
78+
}
79+
}
80+
81+
options.configFile = configFile ? path.resolve(configFile) : null;
7182

7283
return options;
7384
};
@@ -194,7 +205,7 @@ exports.process = function() {
194205
process.exit(1);
195206
}
196207

197-
return processArgs(argv, options);
208+
return processArgs(argv, options, fs, path);
198209
};
199210

200211
// just for testing

test/unit/cli.spec.coffee

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@
44
describe 'cli', ->
55
cli = require '../../lib/cli'
66
optimist = require 'optimist'
7+
path = require 'path'
78
constant = require '../../lib/constants'
8-
CWD = process.cwd()
99
path = require 'path'
10+
mocks = require 'mocks'
11+
12+
fsMock = mocks.fs.create
13+
cwd:
14+
'karma.conf.js': true
15+
cwd2:
16+
'karma.conf.coffee': true
17+
18+
currentCwd = null
19+
20+
pathMock =
21+
resolve: (p) -> path.resolve currentCwd, p
22+
23+
setCWD = (cwd) ->
24+
currentCwd = cwd
25+
fsMock._setCWD cwd
1026

1127
processArgs = (args, opts) ->
1228
argv = optimist.parse(args)
13-
cli.processArgs argv, opts || {}
29+
cli.processArgs argv, opts || {}, fsMock, pathMock
30+
31+
beforeEach -> setCWD '/'
1432

1533
describe 'processArgs', ->
1634

@@ -23,13 +41,28 @@ describe 'cli', ->
2341

2442

2543
it 'should parse options without configFile and set default', ->
44+
setCWD '/cwd'
2645
options = processArgs ['--auto-watch', '--auto-watch-interval', '10']
2746

28-
expect(options.configFile).to.equal path.join(CWD, 'karma.conf.js')
47+
expect(options.configFile).to.equal '/cwd/karma.conf.js'
2948
expect(options.autoWatch).to.equal true
3049
expect(options.autoWatchInterval).to.equal 10
3150

3251

52+
it 'should set default karma.conf.coffee config file if exists', ->
53+
setCWD '/cwd2'
54+
options = processArgs ['--port', '10']
55+
56+
expect(options.configFile).to.equal '/cwd2/karma.conf.coffee'
57+
58+
59+
it 'should not set default config if neither exists', ->
60+
setCWD '/'
61+
options = processArgs []
62+
63+
expect(options.configFile).to.equal null
64+
65+
3366
it 'should parse auto-watch, colors, singleRun to boolean', ->
3467
options = processArgs ['--auto-watch', 'false', '--colors', 'false', '--single-run', 'false']
3568

@@ -61,8 +94,9 @@ describe 'cli', ->
6194

6295

6396
it 'should resolve configFile to absolute path', ->
97+
setCWD '/cwd'
6498
options = processArgs ['some/config.js']
65-
expect(options.configFile).to.equal path.join(CWD, '/some/config.js')
99+
expect(options.configFile).to.equal '/cwd/some/config.js'
66100

67101

68102
it 'should parse report-slower-than to a number', ->

0 commit comments

Comments
 (0)