Skip to content

Commit

Permalink
adding help test for command lookup on disk
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden committed Oct 1, 2015
1 parent 9dd49a9 commit 9547d0c
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 42 deletions.
71 changes: 32 additions & 39 deletions lib/commands/help.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

var path = require('path');
var Command = require('../models/command');
var lookupCommand = require('../cli/lookup-command');
var string = require('../utilities/string');
var assign = require('lodash/object/assign');
var path = require('path');
var Command = require('../models/command');
var lookupCommand = require('../cli/lookup-command');
var string = require('../utilities/string');
var assign = require('lodash/object/assign');
var GenerateCommand = require('./generate');

var RootCommand = Command.extend({
isRoot: true,
Expand All @@ -31,7 +32,7 @@ module.exports = Command.extend({
],

run: function(commandOptions, rawArgs) {
var multipleCommands = ['g', 'generate'];
var multipleCommands = [GenerateCommand.prototype.name].concat(GenerateCommand.prototype.aliases);
var command;
var json;
var rootCommand = new RootCommand({
Expand All @@ -54,11 +55,7 @@ module.exports = Command.extend({
}

Object.keys(this.commands).forEach(function(commandName) {
if (commandOptions.json) {
this._addCommandHelpToJson(commandName, false, commandOptions, json);
} else {
this._printBasicHelpForCommand(commandName, commandOptions);
}
this._printBasicHelpForCommand(commandName, commandOptions, json);
}, this);

if (this.project.eachAddonCommand) {
Expand All @@ -74,11 +71,7 @@ module.exports = Command.extend({
this.ui.writeLine('Available commands from ' + addonName + ':');
}
Object.keys(this.commands).forEach(function(commandName) {
if (commandOptions.json) {
this._addCommandHelpToJson(commandName, false, commandOptions, addonJson);
} else {
this._printBasicHelpForCommand(commandName, commandOptions);
}
this._printBasicHelpForCommand(commandName, commandOptions, addonJson);
}, this);
}.bind(this));
}
Expand All @@ -101,42 +94,42 @@ module.exports = Command.extend({
if (rawArgs.length > 0) {
commandOptions.rawArgs = rawArgs;
}
if (commandOptions.json) {
this._addCommandHelpToJson(command, true, commandOptions, json);
} else {
this._printDetailedHelpForCommand(command, commandOptions);
}
} else {
// Iterate through each arg beyond the initial 'help' command,
// and try to display usage instructions.
rawArgs.forEach(function(commandName) {
if (commandOptions.json) {
this._addCommandHelpToJson(commandName, true, commandOptions, json);
} else {
this._printDetailedHelpForCommand(commandName, commandOptions);
}
}, this);
rawArgs = [command];
}

// Iterate through each arg beyond the initial 'help' command,
// and try to display usage instructions.
rawArgs.forEach(function(commandName) {
this._printDetailedHelpForCommand(commandName, commandOptions, json);
}, this);
}

if (commandOptions.json) {
this._printJsonHelp(json);
}
},

_addCommandHelpToJson: function(commandName, single, options, json) {
var command = this._lookupCommand(commandName);
if (!command.skipHelp || single) {
json.commands.push(command.getJson(options));
_printBasicHelpForCommand: function(commandName, options, json) {
if (options.json) {
this._addCommandHelpToJson(commandName, false, options, json);
} else {
this._printHelpForCommand(commandName, false, options);
}
},

_printBasicHelpForCommand: function(commandName, options) {
this._printHelpForCommand(commandName, false, options);
_printDetailedHelpForCommand: function(commandName, options, json) {
if (options.json) {
this._addCommandHelpToJson(commandName, true, options, json);
} else {
this._printHelpForCommand(commandName, true, options);
}
},

_printDetailedHelpForCommand: function(commandName, options) {
this._printHelpForCommand(commandName, true, options);
_addCommandHelpToJson: function(commandName, single, options, json) {
var command = this._lookupCommand(commandName);
if (!command.skipHelp || single) {
json.commands.push(command.getJson(options));
}
},

_printHelpForCommand: function(commandName, detailed, options) {
Expand Down
233 changes: 230 additions & 3 deletions tests/unit/commands/help-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,86 @@

var expect = require('chai').expect;
var EOL = require('os').EOL;
var proxyquire = require('proxyquire');
var path = require('path');
var stub = require('../../helpers/stub').stub;
var processHelpString = require('../../helpers/process-help-string');
var convertToJson = require('../../helpers/convert-help-output-to-json');
var commandOptions = require('../../factories/command-options');
var HelpCommand = require('../../../lib/commands/help');

var lookupCommandStub;
var HelpCommand = proxyquire('../../../lib/commands/help', {
'../cli/lookup-command': function() {
return lookupCommandStub.apply(this, arguments);
}
});

describe('help command', function() {
var options;

beforeEach(function() {
options = commandOptions();

lookupCommandStub = require('../../../lib/cli/lookup-command');
});

describe('print text', function() {
describe('common to both', function() {
it('finds command on disk', function() {
var Command1 = function() {};
stub(Command1.prototype, 'printBasicHelp');
stub(Command1.prototype, 'printDetailedHelp');

options.commands = {
Command1: Command1
};

var wasCalled;
lookupCommandStub = function() {
expect(arguments[0]).to.equal(options.commands);
expect(arguments[1]).to.equal('command-2');
wasCalled = true;
return Command1;
};

var command = new HelpCommand(options);

command.run(options, ['command-2']);

expect(Command1.prototype.printBasicHelp.called).to.equal(1);
expect(wasCalled).to.be.true;
});

it('looks up multiple commands', function() {
var Command1 = function() {};
var Command2 = function() {};
var Command3 = function() {};
stub(Command1.prototype, 'printBasicHelp');
stub(Command2.prototype, 'printBasicHelp');
stub(Command3.prototype, 'printBasicHelp');
stub(Command1.prototype, 'printDetailedHelp');
stub(Command2.prototype, 'printDetailedHelp');
stub(Command3.prototype, 'printDetailedHelp');

options.commands = {
Command1: Command1,
Command2: Command2,
Command3: Command3
};

var command = new HelpCommand(options);

command.run(options, ['command-1', 'command-2']);

expect(Command1.prototype.printBasicHelp.called).to.equal(1);
expect(Command2.prototype.printBasicHelp.called).to.equal(1);
expect(Command3.prototype.printBasicHelp.called).to.equal(0);
expect(Command1.prototype.printDetailedHelp.called).to.equal(1);
expect(Command2.prototype.printDetailedHelp.called).to.equal(1);
expect(Command3.prototype.printDetailedHelp.called).to.equal(0);
});
});

describe('unique to text printing', function() {
it('lists commands', function() {
var Command1 = function() {};
var Command2 = function() {};
Expand Down Expand Up @@ -81,6 +147,58 @@ describe('help command', function() {
expect(Command1.prototype.printBasicHelp.called).to.equal(1);
});

it('passes extra commands to `generate`', function() {
var Generate = function() {};
stub(Generate.prototype, 'printBasicHelp');
stub(Generate.prototype, 'printDetailedHelp');

options.commands = {
Generate: Generate
};

var command = new HelpCommand(options);

command.run(options, ['generate', 'something', 'else']);

expect(Generate.prototype.printBasicHelp.calledWith[0][0].rawArgs).to.deep.equal(['something', 'else']);
expect(Generate.prototype.printDetailedHelp.calledWith[0][0].rawArgs).to.deep.equal(['something', 'else']);
});

it('handles no extra commands to `generate`', function() {
var Generate = function() {};
stub(Generate.prototype, 'printBasicHelp');
stub(Generate.prototype, 'printDetailedHelp');

options.commands = {
Generate: Generate
};

var command = new HelpCommand(options);

command.run(options, ['generate']);

expect(Generate.prototype.printBasicHelp.calledWith[0][0].rawArgs).to.equal(undefined);
expect(Generate.prototype.printDetailedHelp.calledWith[0][0].rawArgs).to.equal(undefined);
});

it('passes extra commands to `generate` alias', function() {
var Generate = function() {};
Generate.prototype.aliases = ['g'];
stub(Generate.prototype, 'printBasicHelp');
stub(Generate.prototype, 'printDetailedHelp');

options.commands = {
Generate: Generate
};

var command = new HelpCommand(options);

command.run(options, ['g', 'something', 'else']);

expect(Generate.prototype.printBasicHelp.calledWith[0][0].rawArgs).to.deep.equal(['something', 'else']);
expect(Generate.prototype.printDetailedHelp.calledWith[0][0].rawArgs).to.deep.equal(['something', 'else']);
});

it('handles missing command', function() {
options.commands = {
Command1: function() {}
Expand Down Expand Up @@ -184,7 +302,7 @@ Available commands from my-addon:' + EOL);
});
});

describe('print json', function() {
describe('unique to json printing', function() {
beforeEach(function() {
options.json = true;
});
Expand Down Expand Up @@ -256,6 +374,115 @@ Available commands from my-addon:' + EOL);
]);
});

it('passes extra commands to `generate`', function() {
options.commands = {
Generate: function() {
return {
getJson: function(options) {
expect(options.rawArgs).to.deep.equal(['something', 'else']);
return {
test1: 'bar'
};
}
};
}
};

var command = new HelpCommand(options);

command.run(options, ['generate', 'something', 'else']);

var json = convertToJson(options.ui.output);

expect(json.commands).to.deep.equal([
{
test1: 'bar'
}
]);
});

it('handles no extra commands to `generate`', function() {
options.commands = {
Generate: function() {
return {
getJson: function(options) {
expect(options.rawArgs).to.equal(undefined);
return {
test1: 'bar'
};
}
};
}
};

var command = new HelpCommand(options);

command.run(options, ['generate']);

var json = convertToJson(options.ui.output);

expect(json.commands).to.deep.equal([
{
test1: 'bar'
}
]);
});

it('passes extra commands to `generate` alias', function() {
var Generate = function() {
return {
getJson: function() {
return {
test1: 'bar'
};
}
};
};
Generate.prototype.aliases = ['g'];

options.commands = {
Generate: Generate
};

var command = new HelpCommand(options);

command.run(options, ['g', 'something', 'else']);

var json = convertToJson(options.ui.output);

expect(json.commands).to.deep.equal([
{
test1: 'bar'
}
]);
});

it('handles special option `path`', function() {
options.commands = {
Command1: function() {
return {
getJson: function() {
return {
test1: path
};
}
};
}
};

var command = new HelpCommand(options);

command.run(options, ['command-1']);

var json = convertToJson(options.ui.output);

expect(json.commands).to.deep.equal([
{
test1: 'path'
}
]);
});

it('handles missing command', function() {
options.commands = {
Command1: function() {}
Expand Down

0 comments on commit 9547d0c

Please sign in to comment.