Skip to content

Commit

Permalink
Ensure beforeRun is included in command instrumentation.
Browse files Browse the repository at this point in the history
Prior to this change `command.beforeRun` was being included in `init`
instrumentation. This change fixes that by moving `.beforeRun` into
`command` instrumentation.

This change also ensures that `beforeRun` can return a promise (which
we had previously documented in the API docs but was not actually
supported until this commit) and adds a test.
  • Loading branch information
rwjblue committed Jan 1, 2017
1 parent cbcb528 commit 13faa4a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
11 changes: 7 additions & 4 deletions lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,21 @@ CLI.prototype.run = function(environment) {
}
}

command.beforeRun(commandArgs);

var instrumentation = this.instrumentation;
shutdownOnExit = function() {
instrumentation.stopAndReport('shutdown');
};

return Promise.resolve().then(function() {
loggerTesting.info('cli: command.validateAndRun');

instrumentation.stopAndReport('init');
instrumentation.start('command');

loggerTesting.info('cli: command.beforeRun');

return command.beforeRun(commandArgs);
}).then(function() {
loggerTesting.info('cli: command.validateAndRun');

return command.validateAndRun(commandArgs);
}).then(function (result) {
instrumentation.stopAndReport('command', commandName, commandArgs);
Expand Down
65 changes: 54 additions & 11 deletions tests/unit/cli/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ var MockAnalytics = require('../../helpers/mock-analytics');
var CLI = require('../../../lib/cli/cli');
var td = require('testdouble');
var heimdall = require('heimdalljs');
var Command = require('../../../lib/models/command');
var Promise = require('../../../lib/ext/promise');

var ui;
var analytics;
var commands = {};
var argv;
var isWithinProject;
var project;

// helper to similate running the CLI
function ember(args) {
Expand All @@ -29,17 +32,7 @@ function ember(args) {
commands: commands,
cliArgs: args || [],
settings: {},
project: {
isEmberCLIProject: function() { // similate being inside or outside of a project
return isWithinProject;
},
hasDependencies: function() {
return true;
},
blueprintLookupPaths: function() {
return [];
}
}
project: project
}).then(function (value) {
td.verify(stopInstr('init'), { times: 1 });
td.verify(startInstr('command'), { times: 1 });
Expand Down Expand Up @@ -77,6 +70,17 @@ describe('Unit: CLI', function() {
argv = [];
commands = { };
isWithinProject = true;
project = {
isEmberCLIProject: function() { // similate being inside or outside of a project
return isWithinProject;
},
hasDependencies: function() {
return true;
},
blueprintLookupPaths: function() {
return [];
}
};
});

afterEach(function() {
Expand Down Expand Up @@ -147,6 +151,45 @@ describe('Unit: CLI', function() {
td.verify(init(), {ignoreExtraArgs: true, times: 0});
});

describe('custom addon command', function() {
it('beforeRun can return a promise', function() {
var CustomCommand = Command.extend({
name: 'custom',

init: function() {
this._super && this._super.init.apply(this, arguments);

this._beforeRunFinished = false;
},

beforeRun: function() {
var command = this;

return new Promise(function(resolve) {
setTimeout(function() {
command._beforeRunFinished = true;
resolve();
}, 5);
});
},

run: function() {
if (!this._beforeRunFinished) {
throw new Error('beforeRun not completed before run called!');
}
}
});

project.eachAddonCommand = function(callback) {
callback('custom-addon', {
custom: CustomCommand
});
};

return ember(['custom']);
});
});

describe('help', function() {
['--help', '-h'].forEach(function(command) {
it('ember ' + command, function() {
Expand Down

0 comments on commit 13faa4a

Please sign in to comment.