Skip to content

Commit

Permalink
Fix a couple of issues with cli
Browse files Browse the repository at this point in the history
- Increasing coverage
- donejs-cli package.json was required from wrong path
- cli was throwing silently if folder had no package.json
  while looking up for local npm scripts
  • Loading branch information
Manuel Mujica committed Apr 6, 2016
1 parent f6189bd commit 5648441
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 28 deletions.
9 changes: 8 additions & 1 deletion bin/donejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ var utils = require('../lib/utils');

utils.projectRoot()
.then(function(root) {
cli(root, process.argv);
var program = cli(root);

program.parse(process.argv);

// show help if no args provided
if (!program.args.length) {
program.help();
}
})
.catch(function(err) {
throw err;
Expand Down
6 changes: 5 additions & 1 deletion lib/cli/get-npm-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ var path = require('path');
var debug = require('debug')('donejs-cli:binary');

module.exports = function(root) {
var scripts = {};

try {
var curpkg = require(path.join(root, 'package.json'));
return curpkg.scripts || {};
scripts = curpkg.scripts || {};
}
catch (e) {
// otherwise no local package.json
debug('Could not load local package.json');
}

return scripts;
};
11 changes: 3 additions & 8 deletions lib/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var init = require('./cmd-init');
var generate = require('./cmd-generate');
var commandList = ['init', 'generate', 'add'];

module.exports = function(root, argv) {
var mypkg = require(path.join(root, 'package.json'));
module.exports = function(root) {
var mypkg = require(path.join(__dirname, '..', '..', 'package.json'));

debug('mypkg', mypkg);

Expand Down Expand Up @@ -87,10 +87,5 @@ module.exports = function(root, argv) {
program.help();
});

program.parse(argv);

// show help if no args provided
if (!program.args.length) {
program.help();
}
return program;
};
1 change: 1 addition & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports.projectRoot = function() {
var root = process.cwd();
var current = root;

// istanbul ignore next
while(current && !fs.existsSync(path.join(current, 'node_modules')) ) {
if(current === path.dirname(current)) {
debug('Returning cwd project root', root);
Expand Down
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('./tests/cli-test');
require('./tests/utils-test');
require('./tests/cmd-init-test');
require('./tests/cmd-add-test');
require('./tests/cmd-init-test');
require('./tests/cmd-generate-test');
require('./tests/get-local-bins-test');
require('./tests/get-npm-scripts-test');
43 changes: 32 additions & 11 deletions test/tests/cli-test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
var path = require('path');
var assert = require('assert');
var concat = require('concat-stream');
var spawn = require('cross-spawn-async');
var pkg = require('../../package.json');
var cli = require('../../lib/cli/index');

describe('./bin/donejs', function() {
var command;
var program;

beforeEach(function() {
command = path.join(__dirname, '..', '..', 'bin', 'donejs');
program = cli(__dirname);
});

it.skip('shows cli help when no options passed', function(done) {
var proc = spawn(command, []);
it('reports the correct package version', function() {
assert.equal(program.version(), pkg.version);
});

it('includes the init, add and generate commands', function() {
var cliCommands = program.commands.map(function(cmd) {
return cmd._name;
});

assert(has(cliCommands, 'init'), 'should include init');
assert(has(cliCommands, 'add'), 'should include add');
assert(has(cliCommands, 'generate'), 'should include generate');
});

it('includes local scripts and binaries as commands', function() {
program = cli(path.join(__dirname, '..', '..'));

proc.stdout.pipe(concat(function(output) {
var res = output.toString('utf8');
var cliCommands = program.commands.map(function(cmd) {
return cmd._name;
});

assert(/Usage: donejs \[options\]/.test(res), 'should show cli help');
done();
}));
assert(has(cliCommands, 'test'), 'should include test');
assert(has(cliCommands, 'jshint'), 'should include jshint');
assert(has(cliCommands, 'coverage'), 'should include coverage');
assert(has(cliCommands, 'mocha'), 'should include mocha');
assert(has(cliCommands, 'documentjs'), 'should include documentjs');
});

function has(coll, cmd) {
return coll.indexOf(cmd) !== -1;
}
});
43 changes: 43 additions & 0 deletions test/tests/cmd-generate-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var Q = require('q');
var path = require('path');
var assert = require('assert');
var mockery = require('mockery');

describe('cli generate cmd', function() {
it('calls generate with the right arguments', function() {
var params = {};
var name = 'component';
var generateCalled = false;
var root = path.join(__dirname, '..', '..');
var cmdGeneratePath = '../../lib/cli/cmd-generate';

mockery.registerAllowable(cmdGeneratePath);

mockery.registerMock('../utils', {
generate: function() {
var args = Array.prototype.slice.call(arguments);

assert.equal(args[0], path.join(root, 'node_modules'));
assert.equal(args[1], 'generator-donejs');
assert.deepEqual(args[2], [ ['component', {}] ]);
generateCalled = true;

return Q(true);
}
});

mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});

var generate = require(cmdGeneratePath);

return generate(root, name, params)
.then(function() {
assert(generateCalled, 'generate should be called');
mockery.disable();
mockery.deregisterAll();
});
});
});
4 changes: 4 additions & 0 deletions test/tests/get-npm-scripts-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ describe('getNpmScripts', function() {
assert(keys.indexOf('jshint') !== -1, 'should include "jshint" script');
assert(keys.indexOf('coverage') !== -1, 'should include "coverage" script');
});

it('returns empty object if package.json does not exist', function() {
assert.deepEqual(getNpmScripts(__dirname), {}, 'should be an empty object');
});
});
63 changes: 57 additions & 6 deletions test/tests/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function fail(error) {

var isCI = require('is-ci');
var isWindows = require('os').platform() === 'win32';

// change the current working directory to "test" where the .yo-rc.json
// and the package.json is located
var testDir = path.join(process.cwd(), 'test');
Expand Down Expand Up @@ -73,12 +74,28 @@ describe('DoneJS CLI tests', function () {
});
});

it('runScript and runCommand', function (done) {
utils.runScript('verify', ['testing', 'args']).then(function (child) {
assert.equal(child.exitCode, 0, 'Exited successfully');
done();
})
.fail(fail);
it('runScript resolves if command exits successfully', function (done) {
utils.runScript('verify', ['testing', 'args'])
.then(function (child) {
assert.equal(child.exitCode, 0, 'Exited successfully');
done();
})
.fail(fail);
});

it('runScript rejects if command exits with error', function(done) {
utils.runScript('foobar')
.then(function() {
assert(false, 'should fail');
done();
})
.catch(function(err) {
assert.equal(
err.message,
'Command `npm` did not complete successfully'
);
done();
});
});

it('generate .component', function (done) {
Expand Down Expand Up @@ -142,4 +159,38 @@ describe('DoneJS CLI tests', function () {
.fail(done);
});
});

describe('utils.log', function() {
var _exit;
var exitCode;

beforeEach(function() {
_exit = process.exit;

Object.defineProperty(process, 'exit', {
value: function(code) {
exitCode = code;
}
});
});

afterEach(function() {
Object.defineProperty(process, 'exit', { value: _exit });
});

it('ends process successfully if promise resolves', function() {
return utils.log(Q(true))
.then(function() {
assert.equal(exitCode, 0);
});
});

it('ends process with error if promise rejects', function(done) {
utils.log(Q.reject(new Error('foobar')))
.finally(function() {
assert.equal(exitCode, 1);
done();
});
});
});
});

0 comments on commit 5648441

Please sign in to comment.