Skip to content

Commit 86c603c

Browse files
committed
fix: check bash path before invocation
1 parent eddb7de commit 86c603c

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
sudo: false
22
os:
33
- linux
4-
- osx
4+
- windows
55
language: node_js
6+
7+
# https://github.com/nodejs/Release#end-of-life-releases
68
node_js:
7-
- node
9+
- '14'
10+
- '12'
11+
- '10'
812
- '8'
9-
- '7'
10-
- '6'
11-
- '5'
12-
- '4'

index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ glob.sync = function(pattern, options) {
186186
return (opts.nullglob || fs.existsSync(fp)) ? [pattern] : [];
187187
}
188188

189-
var cp = spawn.sync(bashPath, cmd(pattern, opts), opts);
189+
var cp = spawn.sync(getBash(), cmd(pattern, opts), opts);
190190
var error = cp.stderr ? String(cp.stderr).trim() : null;
191191
if (error) {
192192
err = handleError(error, pattern, opts);
@@ -264,7 +264,7 @@ function bash(pattern, options, cb) {
264264
return;
265265
}
266266

267-
var cp = spawn(bashPath, cmd(pattern, options), options);
267+
var cp = spawn(getBash(), cmd(pattern, options), options);
268268
var buf = new Buffer(0);
269269

270270
cp.stdout.on('data', function(data) {
@@ -475,6 +475,22 @@ function emitMatches(str, pattern, options) {
475475
glob.emit('match', getFiles(str, pattern, options), options.cwd);
476476
}
477477

478+
/**
479+
* Returns bash path if exists.
480+
* @ignore
481+
* @return {String} Bash bin path.
482+
*
483+
*/
484+
function getBash() {
485+
const bashBinPath = bashPath()
486+
487+
if (!bashBinPath) {
488+
throw new TypeError('`bash` not found')
489+
}
490+
491+
return bashBinPath
492+
}
493+
478494
/**
479495
* Expose `glob`
480496
*/

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,26 @@
2020
"test": "mocha"
2121
},
2222
"dependencies": {
23-
"bash-path": "^1.0.1",
24-
"component-emitter": "^1.2.1",
23+
"bash-path": "^2.0.1",
24+
"component-emitter": "^1.3.0",
2525
"cross-spawn": "^5.1.0",
2626
"each-parallel-async": "^1.0.0",
2727
"extend-shallow": "^2.0.1",
2828
"is-extglob": "^2.1.1",
29-
"is-glob": "^4.0.0"
29+
"is-glob": "^4.0.1"
3030
},
3131
"devDependencies": {
3232
"arr-union": "^3.1.0",
3333
"array-unique": "^0.3.2",
3434
"async-array-reduce": "^1.0.0",
3535
"delete": "^1.1.0",
36-
"glob": "^7.1.2",
36+
"glob": "^7.1.6",
3737
"gulp-format-md": "^1.0.0",
38-
"minimist": "^1.2.0",
39-
"mkdirp": "^0.5.1",
40-
"mocha": "^3.2.0"
38+
"minimist": "^1.2.5",
39+
"mkdirp": "^1.0.4",
40+
"mocha": "^6.2.3",
41+
"mock-require": "^3.0.3",
42+
"sinon": "^9.1.0"
4143
},
4244
"keywords": [
4345
"bash",

test/api.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require('mocha');
44
var assert = require('assert');
5+
var sinon = require('sinon');
6+
var mock = require('mock-require');
57
var glob = require('..');
68

79
describe('bash-glob', function() {
@@ -48,4 +50,21 @@ describe('bash-glob', function() {
4850
}
4951
});
5052
});
53+
54+
it('throws exception if `bash` not found', function() {
55+
var bashPathSpy = sinon.spy(function () { return null; });
56+
mock('bash-path', bashPathSpy);
57+
58+
var glob = mock.reRequire('..')
59+
60+
try {
61+
glob.sync(['*']);
62+
63+
} catch (err) {
64+
assert(err);
65+
assert.equal(err.message, '`bash` not found');
66+
}
67+
68+
mock.stop('bash-path');
69+
});
5170
});

0 commit comments

Comments
 (0)