Skip to content

Commit

Permalink
[Fixes #2642] only detect watcher if a given command has watcher as a…
Browse files Browse the repository at this point in the history
…n available option
  • Loading branch information
stefanpenner committed Oct 2, 2016
1 parent d5c24bd commit 9b7493b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 38 deletions.
23 changes: 19 additions & 4 deletions lib/models/command.js
Expand Up @@ -232,14 +232,29 @@ var Command = CoreObject.extend({

var options = commandOptions.options;

// do stuff to try and provide a good experience when it comes to file watching
var watchPreference = detector.findBestWatcherOption(options);
this.project._watchmanInfo = watchPreference.watchmanInfo;
options.watcher = watchPreference.watcher;
if (this.hasOption('watcher')) {
// do stuff to try and provide a good experience when it comes to file watching
var watchPreference = detector.findBestWatcherOption(options);
this.project._watchmanInfo = watchPreference.watchmanInfo;
options.watcher = watchPreference.watcher;
}
resolve(this.run(options, commandOptions.args));
}.bind(this));
},

/**
Reports if the given command has a command line option by a given name
@method hasOption
@param {String} name
@return {Boolean}
*/
hasOption: function(name) {
return !!this.availableOptions.find(function(option) {
return option.name === name;
});
},

/**
Merges any options with duplicate keys in the availableOptions array.
Used primarily by registerOptions.
Expand Down
15 changes: 2 additions & 13 deletions tests/unit/cli/cli-test.js
@@ -1,6 +1,5 @@
'use strict';

var EOL = require('os').EOL;
var expect = require('chai').expect;
var MockUI = require('../../helpers/mock-ui');
var MockAnalytics = require('../../helpers/mock-analytics');
Expand Down Expand Up @@ -318,12 +317,7 @@ describe('Unit: CLI', function() {

var output = ui.output.trim();

var options = captor.value;
if (/win\d+/.test(process.platform) || options.watcher === 'watchman') {
expect(output).to.equal('', 'expected no extra output');
} else {
expect(output.split(EOL).length).to.equal(2, 'expected no extra output');
}
expect(output).to.equal('', 'expected no extra output');
});
});
});
Expand All @@ -348,12 +342,7 @@ describe('Unit: CLI', function() {

var output = ui.output.trim();

var options = captor.value;
if (/win\d+/.test(process.platform) || options.watcher === 'watchman') {
expect(output).to.equal('', 'expected no extra output');
} else {
expect(output.split(EOL).length).to.equal(2, 'expected no extra output');
}
expect(output).to.equal('', 'expected no extra output');
});
});
});
Expand Down
79 changes: 58 additions & 21 deletions tests/unit/models/command-test.js
Expand Up @@ -31,7 +31,7 @@ var ServeCommand = Command.extend({
{ name: 'live-reload-port', type: Number, description: '(Defaults to port number + 31529)' },
{ name: 'environment', type: String, default: 'development' }
],
run: function() {}
run: function(options) { return options; }
});

var DevelopEmberCLICommand = Command.extend({
Expand All @@ -40,19 +40,19 @@ var DevelopEmberCLICommand = Command.extend({
availableOptions: [
{ name: 'package-name', key: 'packageName', type: String, required: true }
],
run: function() {}
run: function(options) { return options; }
});

var InsideProjectCommand = Command.extend({
name: 'inside-project',
works: 'insideProject',
run: function() {}
run: function(options) { return options; }
});

var OutsideProjectCommand = Command.extend({
name: 'outside-project',
works: 'outsideProject',
run: function() {}
run: function(options) { return options; }
});

var OptionsAliasCommand = Command.extend({
Expand Down Expand Up @@ -80,7 +80,7 @@ var OptionsAliasCommand = Command.extend({
{ 'hw': 'Hello world' }
]
}],
run: function() {}
run: function(options) { return options; }
});

describe('models/command.js', function() {
Expand Down Expand Up @@ -155,26 +155,53 @@ describe('models/command.js', function() {
expect(new ServeCommand(options).parseArgs(['-lr', 'false'])).to.have.deep.property('options.liveReload', false);
});

it('validateAndRun() should print a message if a required option is missing.', function() {
return new DevelopEmberCLICommand(options).validateAndRun([]).then(function() {
expect(ui.output).to.match(/requires the option.*package-name/);
describe('#validateAndRun', function() {

it('should print a message if a required option is missing.', function() {
return new DevelopEmberCLICommand(options).validateAndRun([]).then(function() {
expect(ui.output).to.match(/requires the option.*package-name/);
});
});
});

it('validateAndRun() should print a message if outside a project and command is not valid there.', function() {
return new InsideProjectCommand(assign(options, {
project: {
hasDependencies: function() { return true; },
isEmberCLIProject: function() { return false; },
}
})).validateAndRun([]).catch(function(reason) {
expect(reason.message).to.match(/You have to be inside an ember-cli project/);
it('should print a message if outside a project and command is not valid there.', function() {
return new InsideProjectCommand(assign(options, {
project: {
hasDependencies: function() { return true; },
isEmberCLIProject: function() { return false; },
}
})).validateAndRun([]).catch(function(reason) {
expect(reason.message).to.match(/You have to be inside an ember-cli project/);
});
});

it('selects watcher if an option', function() {
return new InsideProjectCommand(assign(options, {
availableOptions: [{ type: 'string', name: 'watcher' }],
project: {
hasDependencies: function() { return true; },
isEmberCLIProject: function() { return true; },
}
})).validateAndRun([]).then(function(options) {
expect(options).to.have.property('watcher');
});
});
});

it('validateAndRun() should print a message if inside a project and command is not valid there.', function() {
return new OutsideProjectCommand(options).validateAndRun([]).catch(function(reason) {
expect(reason.message).to.match(/You cannot use.*inside an ember-cli project/);
it('selects NO watcher if NOT an option', function() {
return new InsideProjectCommand(assign(options, {
availableOptions: [{ type: 'string', name: 'foo' }],
project: {
hasDependencies: function() { return true; },
isEmberCLIProject: function() { return true; },
}
})).validateAndRun([]).then(function(options) {
expect(options).to.not.have.property('watcher');
});
});

it('should print a message if inside a project and command is not valid there.', function() {
return new OutsideProjectCommand(options).validateAndRun([]).catch(function(reason) {
expect(reason.message).to.match(/You cannot use.*inside an ember-cli project/);
});
});
});

Expand Down Expand Up @@ -569,6 +596,16 @@ describe('models/command.js', function() {
});
});

describe('hasOption', function() {
it('reports false if no option with that name is present', function() {
expect(command.hasOption('no-option-by-this-name')).to.be.false;
});

it('reports false if no option with that name is present', function() {
expect(command.hasOption('port')).to.be.true;
});
});

describe('getJson', function() {
beforeEach(function() {
forEachWithPropertyStub = function(forEach, context) {
Expand Down

0 comments on commit 9b7493b

Please sign in to comment.