Merge pull request #349 from zhiyelee/fix-global
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix executable subcommand when install globally * refactor test cases
authored by Zhiye Li · committed by Zhiye Li · parents 5ae016f, eb850dd · Browse files · 8 files·+82 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+82 added−33 removed
- tests:+43 added−18 removed
- other:+39 added−15 removed
Provenance
- Commit
1ebf2a4- Tree
07f0bd1- Author
Zhiye Li <zhiyelee@gmail.com>- Committer
Zhiye Li <zhiyelee@gmail.com>- Parent 1
5ae016f- Parent 2
eb850dd
8 files changed+82 added−33 removed
added:test/fixtures/pm+10 added
1+#!/usr/bin/env node2+3+var program = require('../../');4+5+program6+ .version('0.0.1')7+ .command('install [name]', 'install one or more packages')8+ .command('search [query]', 'search with optional query')9+ .command('list', 'list packages installed')10+ .parse(process.argv);added:test/fixtures/pm-install+3 added
1+#!/usr/bin/env node2+console.log('install')3+added:test/fixtures/pm-search+3 added
1+#!/usr/bin/env node2+console.log('search')3+added:test/fixtures/pmlink+1 added
1+./pmadded:test/fixtures/pmlink-install+3 added
1+#!/usr/bin/env node2+console.log('install')3+modified:test/test.command.executableSubcommand.js+23 added-18 removed
1212345678951112131415166187891011121314202122151617181925202122232425262728293031var program = require('../') , util = require('util')var exec = require('child_process').exec , path = require('path') , should = require('should');
var oldError = console.error;var err = '';console.error = function (tpl, s) { err = util.format(tpl, s);};
var exitCode = 0;process.exit = function (code) { exitCode = code;}program .command('exec [options]', 'this is my command');
program.parse('node test exec a'.split(' '));var bin = path.join(__dirname, './fixtures/pm')// not existexec(bin + ' list', function (error, stdout, stderr) { //stderr.should.equal('\n pm-list(1) does not exist, try --help\n\n'); // TODO error info are not the same in between <=v0.8 and later version should.notEqual(0, stderr.length);});
program.runningCommand.on('error', function() { err.should.equal('\n test-exec(1) does not exist, try --help\n'); exitCode.should.equal(1);// success caseexec(bin + ' install', function (error, stdout, stderr) { stdout.should.equal('install\n');});
// @todo test `EACCES` error// spawn EACCESexec(bin + ' search', function (error, stdout, stderr) { // TODO error info are not the same in between <v0.10 and v0.12 should.notEqual(0, stderr.length);});
// when `bin` is a symbol link for mocking global installvar bin = path.join(__dirname, './fixtures/pmlink')// success caseexec(bin + ' install', function (error, stdout, stderr) { stdout.should.equal('install\n');});modified:index.js+9 added-3 removed
3 unmodified lines4 */5 6 var EventEmitter = require('events').EventEmitter;7 var spawn = require('child_process').spawn;8+var readlink = require('graceful-readlink').readlinkSync;9 var path = require('path');10 var dirname = path.dirname;11 var basename = path.basename;12 var fs = require('fs');456 unmodified lines469 args[1] = '--help';470 }471 472 // executable473+ var f = argv[1];474+ var link = readlink(f);475+ if (link !== f) {476+ link = path.join(dirname(f), link)477+ }472- var dir = dirname(argv[1]);473- var bin = basename(argv[1], '.js') + '-' + args[0];478+ var dir = dirname(link);479+ var bin = basename(f, '.js') + '-' + args[0];480 481 // prefer local `./<bin>` to bin in the $PATH482 var local = path.join(dir, bin);483 try {7 unmodified lines491 args = args.slice(1);492 493 var proc;494 if (process.platform !== 'win32') {489- proc = spawn(bin, args, { stdio: 'inherit'});495+ proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });496 } else {497 args.unshift(local);498 proc = spawn(process.execPath, args, { stdio: 'inherit'});499 }554 unmodified linesmodified:package.json+30 added-12 removed
12345678910111213234567891011121314151617181920212223242526272829303132{ "name": "commander" , "version": "2.6.0" , "description": "the complete solution for node.js command-line programs" , "keywords": ["command", "option", "parser"] , "author": "TJ Holowaychuk <tj@vision-media.ca>" , "license": "MIT" , "repository": { "type": "git", "url": "https://github.com/tj/commander.js.git" } , "devDependencies": { "should": ">= 0.0.1" } , "scripts": { "test": "make test" } , "main": "index" , "engines": { "node": ">= 0.6.x" } , "files": ["index.js"] "name": "commander", "version": "2.6.0", "description": "the complete solution for node.js command-line programs", "keywords": [ "command", "option", "parser" ], "author": "TJ Holowaychuk <tj@vision-media.ca>", "license": "MIT", "repository": { "type": "git", "url": "https://github.com/tj/commander.js.git" }, "devDependencies": { "should": ">= 0.0.1" }, "scripts": { "test": "make test" }, "main": "index", "engines": { "node": ">= 0.6.x" }, "files": [ "index.js" ], "dependencies": { "graceful-readlink": ">= 1.0.0" }}