-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6288806
commit a1d1764
Showing
3 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
// The Node team wants to deprecate `process.bind(...)`. | ||
// https://github.com/nodejs/node/pull/2768 | ||
// | ||
// However, we need the 'uv' binding for errname support. | ||
// This is a defensive wrapper around it so `execa` will not fail entirely if it stops working someday. | ||
// | ||
// If this ever stops working. See: https://github.com/sindresorhus/execa/issues/31#issuecomment-215939939 for another possible solution. | ||
var uv; | ||
|
||
try { | ||
uv = process.binding('uv'); | ||
|
||
if (typeof uv.errname !== 'function') { | ||
throw new Error('uv.errname is not a function'); | ||
} | ||
} catch (err) { | ||
console.error('execa/lib/errname: unable to establish process.binding(\'uv\')', err); | ||
uv = null; | ||
} | ||
|
||
function errname(uv, code) { | ||
if (uv) { | ||
return uv.errname(code); | ||
} | ||
|
||
if (!(code < 0)) { | ||
throw new Error('err >= 0'); | ||
} | ||
|
||
return 'Unknown system error ' + code; | ||
} | ||
|
||
module.exports = function getErrname(code) { | ||
return errname(uv, code); | ||
}; | ||
|
||
// used for testing the fallback behavior. | ||
module.exports.__test__ = errname; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import test from 'ava'; | ||
import errname from '../lib/errname'; | ||
|
||
const isWin = process.platform === 'win32'; | ||
const majorVersion = Number(process.version.substr(1).split('.')[0]); | ||
|
||
// simulates failure to capture process.binding('uv'); | ||
function fallback(code) { | ||
return errname.__test__(null, code); | ||
} | ||
|
||
function makeTests(name, m, expected) { | ||
test(`${name}: >=0 exit codes`, t => { | ||
// throws >= 0 | ||
t.throws(() => m(0), /err >= 0/); | ||
t.throws(() => m(1), /err >= 0/); | ||
t.throws(() => m('2'), /err >= 0/); | ||
t.throws(() => m('foo'), /err >= 0/); | ||
}); | ||
|
||
if (!(isWin && majorVersion < 1)) { | ||
// causes process.exit() on Node 0.12 for Windows. | ||
test(`${name}: negative exit codes`, t => { | ||
t.is(m(-2), expected); | ||
t.is(m('-2'), expected); | ||
}); | ||
} | ||
} | ||
|
||
const unknown = 'Unknown system error -2'; | ||
|
||
makeTests('native', errname, isWin ? unknown : 'ENOENT'); | ||
makeTests('fallback', fallback, unknown); |