Skip to content

Commit

Permalink
Turns out fs.execSync could throw and write to parent's stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
markelog committed Mar 12, 2016
1 parent e91314f commit 84d4913
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
20 changes: 15 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { execSync as exec } from 'child_process';

function ip(type) {
type = type || 'default';
function ip(type = 'default') {
const isLinux = ip.platform === 'linux';

if (!ip.present()) {
Expand All @@ -12,13 +11,24 @@ function ip(type) {
return null;
}

return ip.exec(`docker-machine ip ${type}`, { encoding: 'utf8' }).trim();
return ip.exec(`docker-machine ip ${type}`, {
encoding: 'utf8',

// By default parent stderr is used, which is bad
stdio: []
}).trim();
}

ip.present = () => {
const result = ip.exec('docker-machine version && echo $?', { encoding: 'utf8' }).trim();

if (result === '127') {
try {
ip.exec('docker-machine version && echo $?', {
encoding: 'utf8',

// By default parent stderr is used, which is bad
stdio: []
}).trim();
} catch (e) {
return false;
}

Expand Down
22 changes: 17 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('docker-ip', () => {
});

it('should return `false` for not "present" docker-machine', () => {
sinon.stub(ip, 'exec', () => '127');
sinon.stub(ip, 'exec', () => { throw new Error('test'); });
expect(ip.present()).to.equal(false);
ip.exec.restore();
});
Expand Down Expand Up @@ -46,13 +46,19 @@ describe('docker-ip', () => {
it('should get `default` ip without arguments', () => {
ip();
expect(ip.exec.firstCall.args[0]).to.equal('docker-machine ip default');
expect(ip.exec.firstCall.args[1]).to.deep.equal({ encoding: 'utf8' });
expect(ip.exec.firstCall.args[1]).to.deep.equal({
encoding: 'utf8',
stdio: []
});
});

it('should get `dev` ip', () => {
ip('dev');
expect(ip.exec.firstCall.args[0]).to.equal('docker-machine ip dev');
expect(ip.exec.firstCall.args[1]).to.deep.equal({ encoding: 'utf8' });
expect(ip.exec.firstCall.args[1]).to.deep.equal({
encoding: 'utf8',
stdio: []
});
});
});

Expand Down Expand Up @@ -101,13 +107,19 @@ describe('docker-ip', () => {
it('should get `default` ip without arguments', () => {
ip();
expect(ip.exec.firstCall.args[0]).to.equal('docker-machine ip default');
expect(ip.exec.firstCall.args[1]).to.deep.equal({ encoding: 'utf8' });
expect(ip.exec.firstCall.args[1]).to.deep.equal({
encoding: 'utf8',
stdio: []
});
});

it('should get `dev` ip', () => {
ip('dev');
expect(ip.exec.firstCall.args[0]).to.equal('docker-machine ip dev');
expect(ip.exec.firstCall.args[1]).to.deep.equal({ encoding: 'utf8' });
expect(ip.exec.firstCall.args[1]).to.deep.equal({
encoding: 'utf8',
stdio: []
});
});
});

Expand Down

0 comments on commit 84d4913

Please sign in to comment.