diff --git a/lib/cli.js b/lib/cli.js index 4bd4aad83..1d71df41a 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,12 +1,14 @@ -var path = require('path') -var optimist = require('optimist') -var fs = require('graceful-fs') +'use strict' -var Server = require('./server') -var helper = require('./helper') -var constant = require('./constants') +const path = require('path') +const optimist = require('optimist') +const fs = require('graceful-fs') -var processArgs = function (argv, options, fs, path) { +const Server = require('./server') +const helper = require('./helper') +const constant = require('./constants') + +function processArgs (argv, options, fs, path) { if (argv.help) { console.log(optimist.help()) process.exit(0) @@ -19,7 +21,7 @@ var processArgs = function (argv, options, fs, path) { // TODO(vojta): warn/throw when unknown argument (probably mispelled) Object.getOwnPropertyNames(argv).forEach(function (name) { - var argumentValue = argv[name] + let argumentValue = argv[name] if (name !== '_' && name !== '$0') { if (name.indexOf('_') !== -1) { throw new Error('Bad argument: ' + name + ' did you mean ' + name.replace('_', '-')) @@ -45,8 +47,9 @@ var processArgs = function (argv, options, fs, path) { } if (helper.isString(options.formatError)) { + let required try { - var required = require(options.formatError) + required = require(options.formatError) } catch (err) { console.error('Could not require formatError: ' + options.formatError, err) } @@ -59,7 +62,7 @@ var processArgs = function (argv, options, fs, path) { } if (helper.isString(options.logLevel)) { - var logConstant = constant['LOG_' + options.logLevel.toUpperCase()] + const logConstant = constant['LOG_' + options.logLevel.toUpperCase()] if (helper.isDefined(logConstant)) { options.logLevel = logConstant } else { @@ -103,7 +106,7 @@ var processArgs = function (argv, options, fs, path) { options.refresh = options.refresh === 'true' } - var configFile = argv._.shift() + let configFile = argv._.shift() if (!configFile) { // default config file (if exists) @@ -127,11 +130,11 @@ var processArgs = function (argv, options, fs, path) { return options } -var parseClientArgs = function (argv) { +function parseClientArgs (argv) { // extract any args after '--' as clientArgs - var clientArgs = [] + let clientArgs = [] argv = argv.slice(2) - var idx = argv.indexOf('--') + const idx = argv.indexOf('--') if (idx !== -1) { clientArgs = argv.slice(idx + 1) } @@ -139,13 +142,13 @@ var parseClientArgs = function (argv) { } // return only args that occur before `--` -var argsBeforeDoubleDash = function (argv) { - var idx = argv.indexOf('--') +function argsBeforeDoubleDash (argv) { + const idx = argv.indexOf('--') return idx === -1 ? argv : argv.slice(0, idx) } -var describeShared = function () { +function describeShared () { optimist .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' + 'Usage:\n' + @@ -160,7 +163,7 @@ var describeShared = function () { .describe('version', 'Print current version.') } -var describeInit = function () { +function describeInit () { optimist .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' + 'INIT - Initialize a config file.\n\n' + @@ -172,7 +175,7 @@ var describeInit = function () { .describe('help', 'Print usage and options.') } -var describeStart = function () { +function describeStart () { optimist .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' + 'START - Start the server / do a single run.\n\n' + @@ -196,7 +199,7 @@ var describeStart = function () { .describe('help', 'Print usage and options.') } -var describeRun = function () { +function describeRun () { optimist .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' + 'RUN - Run the tests (requires running server).\n\n' + @@ -212,7 +215,7 @@ var describeRun = function () { .describe('no-colors', 'Do not use colors when reporting or printing logs.') } -var describeStop = function () { +function describeStop () { optimist .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' + 'STOP - Stop the server (requires running server).\n\n' + @@ -223,7 +226,7 @@ var describeStop = function () { .describe('help', 'Print usage.') } -var describeCompletion = function () { +function describeCompletion () { optimist .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' + 'COMPLETION - Bash/ZSH completion for karma.\n\n' + @@ -233,8 +236,8 @@ var describeCompletion = function () { } exports.process = function () { - var argv = optimist.parse(argsBeforeDoubleDash(process.argv.slice(2))) - var options = { + const argv = optimist.parse(argsBeforeDoubleDash(process.argv.slice(2))) + const options = { cmd: argv._.shift() } @@ -276,7 +279,7 @@ exports.process = function () { } exports.run = function () { - var config = exports.process() + const config = exports.process() switch (config.cmd) { case 'start': diff --git a/test/unit/cli.spec.js b/test/unit/cli.spec.js index 04744d96d..66cd55736 100644 --- a/test/unit/cli.spec.js +++ b/test/unit/cli.spec.js @@ -1,38 +1,40 @@ -var optimist = require('optimist') -var path = require('path') -var mocks = require('mocks') +'use strict' -var cli = require('../../lib/cli') -var constant = require('../../lib/constants') +const optimist = require('optimist') +const path = require('path') +const mocks = require('mocks') -var loadFile = mocks.loadFile +const cli = require('../../lib/cli') +const constant = require('../../lib/constants') + +const loadFile = mocks.loadFile describe('cli', () => { - var m - var e - var mockery + let m + let e + let mockery - var fsMock = mocks.fs.create({ + const fsMock = mocks.fs.create({ cwd: {'karma.conf.js': true}, cwd2: {'karma.conf.coffee': true}, cwd3: {'karma.conf.ts': true} }) - var currentCwd = null + let currentCwd = null - var pathMock = { + const pathMock = { resolve (p) { return path.resolve(currentCwd, p) } } - var setCWD = (cwd) => { + const setCWD = (cwd) => { currentCwd = cwd fsMock._setCWD(cwd) } - var processArgs = (args, opts) => { - var argv = optimist.parse(args) + const processArgs = (args, opts) => { + const argv = optimist.parse(args) return e.processArgs(argv, opts || {}, fsMock, pathMock) } @@ -61,14 +63,14 @@ describe('cli', () => { describe('processArgs', () => { it('should override if multiple options given', () => { // optimist parses --port 123 --port 456 as port = [123, 456] which makes no sense - var options = processArgs(['some.conf', '--port', '12', '--log-level', 'info', '--port', '34', '--log-level', 'debug']) + const options = processArgs(['some.conf', '--port', '12', '--log-level', 'info', '--port', '34', '--log-level', 'debug']) expect(options.port).to.equal(34) expect(options.logLevel).to.equal('DEBUG') }) it('should return camelCased options', () => { - var options = processArgs(['some.conf', '--port', '12', '--single-run']) + const options = processArgs(['some.conf', '--port', '12', '--single-run']) expect(options.configFile).to.exist expect(options.port).to.equal(12) @@ -77,7 +79,7 @@ describe('cli', () => { it('should parse options without configFile and set default', () => { setCWD('/cwd') - var options = processArgs(['--auto-watch', '--auto-watch-interval', '10']) + const options = processArgs(['--auto-watch', '--auto-watch-interval', '10']) expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd/karma.conf.js')) expect(options.autoWatch).to.equal(true) expect(options.autoWatchInterval).to.equal(10) @@ -85,27 +87,27 @@ describe('cli', () => { it('should set default karma.conf.coffee config file if exists', () => { setCWD('/cwd2') - var options = processArgs(['--port', '10']) + const options = processArgs(['--port', '10']) expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd2/karma.conf.coffee')) }) it('should set default karma.conf.ts config file if exists', () => { setCWD('/cwd3') - var options = processArgs(['--port', '10']) + const options = processArgs(['--port', '10']) expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd3/karma.conf.ts')) }) it('should not set default config if neither exists', () => { setCWD('/') - var options = processArgs([]) + const options = processArgs([]) expect(options.configFile).to.equal(null) }) it('should parse auto-watch, colors, singleRun to boolean', () => { - var options = processArgs(['--auto-watch', 'false', '--colors', 'false', '--single-run', 'false']) + let options = processArgs(['--auto-watch', 'false', '--colors', 'false', '--single-run', 'false']) expect(options.autoWatch).to.equal(false) expect(options.colors).to.equal(false) @@ -119,7 +121,7 @@ describe('cli', () => { }) it('should replace log-level constants', () => { - var options = processArgs(['--log-level', 'debug']) + let options = processArgs(['--log-level', 'debug']) expect(options.logLevel).to.equal(constant.LOG_DEBUG) options = processArgs(['--log-level', 'error']) @@ -137,29 +139,29 @@ describe('cli', () => { it('should parse format-error into a function', () => { // root export - var options = processArgs(['--format-error', '../../test/unit/fixtures/format-error-root']) - var formatErrorRoot = require('../../test/unit/fixtures/format-error-root') + let options = processArgs(['--format-error', '../../test/unit/fixtures/format-error-root']) + const formatErrorRoot = require('../../test/unit/fixtures/format-error-root') expect(options.formatError).to.equal(formatErrorRoot) // property export options = processArgs(['--format-error', '../../test/unit/fixtures/format-error-property']) - var formatErrorProperty = require('../../test/unit/fixtures/format-error-property').formatError + const formatErrorProperty = require('../../test/unit/fixtures/format-error-property').formatError expect(options.formatError).to.equal(formatErrorProperty) }) it('should parse browsers into an array', () => { - var options = processArgs(['--browsers', 'Chrome,ChromeCanary,Firefox']) + const options = processArgs(['--browsers', 'Chrome,ChromeCanary,Firefox']) expect(options.browsers).to.deep.equal(['Chrome', 'ChromeCanary', 'Firefox']) }) it('should resolve configFile to absolute path', () => { setCWD('/cwd') - var options = processArgs(['some/config.js']) + const options = processArgs(['some/config.js']) expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd/some/config.js')) }) it('should parse report-slower-than to a number', () => { - var options = processArgs(['--report-slower-than', '2000']) + let options = processArgs(['--report-slower-than', '2000']) expect(options.reportSlowerThan).to.equal(2000) options = processArgs(['--no-report-slower-than']) @@ -167,7 +169,7 @@ describe('cli', () => { }) it('should cast reporters to array', () => { - var options = processArgs(['--reporters', 'dots,junit']) + let options = processArgs(['--reporters', 'dots,junit']) expect(options.reporters).to.deep.equal(['dots', 'junit']) options = processArgs(['--reporters', 'dots']) @@ -175,7 +177,7 @@ describe('cli', () => { }) it('should parse removed/added/changed files to array', () => { - var options = processArgs([ + const options = processArgs([ '--removed-files', 'r1.js,r2.js', '--changed-files', 'ch1.js,ch2.js', '--added-files', 'a1.js,a2.js' @@ -187,9 +189,9 @@ describe('cli', () => { }) it('should error on args with underscores', () => { - var expectedException + let expectedException try { - var options = processArgs(['--no_browsers']) + const options = processArgs(['--no_browsers']) expectedException = 'Should have thrown but got ' + options } catch (e) { expectedException = e @@ -201,19 +203,19 @@ describe('cli', () => { describe('parseClientArgs', () => { it('should return arguments after --', () => { - var args = cli.parseClientArgs(['node', 'karma.js', 'runArg', '--flag', '--', '--foo', '--bar', 'baz']) + const args = cli.parseClientArgs(['node', 'karma.js', 'runArg', '--flag', '--', '--foo', '--bar', 'baz']) expect(args).to.deep.equal(['--foo', '--bar', 'baz']) }) it('should return empty args if -- is not present', () => { - var args = cli.parseClientArgs(['node', 'karma.js', 'runArg', '--flag', '--foo', '--bar', 'baz']) + const args = cli.parseClientArgs(['node', 'karma.js', 'runArg', '--flag', '--foo', '--bar', 'baz']) expect(args).to.deep.equal([]) }) }) describe('argsBeforeDoubleDash', () => { it('should return array of args that occur before --', () => { - var args = cli.argsBeforeDoubleDash(['aa', '--bb', 'value', '--', 'some', '--no-more']) + const args = cli.argsBeforeDoubleDash(['aa', '--bb', 'value', '--', 'some', '--no-more']) expect(args).to.deep.equal(['aa', '--bb', 'value']) }) })