Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check bash path before invocation #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
sudo: false
os:
- linux
- osx
- windows
language: node_js

# https://github.com/nodejs/Release#end-of-life-releases
node_js:
- node
- '16'
- '14'
- '12'
- '10'
- '8'
- '7'
- '6'
- '5'
- '4'
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ glob.sync = function(pattern, options) {
return (opts.nullglob || fs.existsSync(fp)) ? [pattern] : [];
}

var cp = spawn.sync(bashPath, cmd(pattern, opts), opts);
var cp = spawn.sync(getBash(), cmd(pattern, opts), opts);
var error = cp.stderr ? String(cp.stderr).trim() : null;
if (error) {
err = handleError(error, pattern, opts);
Expand Down Expand Up @@ -264,7 +264,7 @@ function bash(pattern, options, cb) {
return;
}

var cp = spawn(bashPath, cmd(pattern, options), options);
var cp = spawn(getBash(), cmd(pattern, options), options);
var buf = new Buffer(0);

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

/**
* Returns bash path if exists.
* @ignore
* @return {String} Bash bin path.
*
*/
function getBash() {
const bashBinPath = bashPath()

if (!bashBinPath) {
throw new TypeError('`bash` not found')
}

return bashBinPath
}

/**
* Expose `glob`
*/
Expand Down
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,32 @@
],
"main": "index.js",
"engines": {
"node": ">=4.0"
"node": ">=8.0"
},
"scripts": {
"test": "mocha"
},
"dependencies": {
"bash-path": "^1.0.1",
"component-emitter": "^1.2.1",
"cross-spawn": "^5.1.0",
"bash-path": "^2.0.1",
"component-emitter": "^1.3.0",
"cross-spawn": "^7.0.3",
"each-parallel-async": "^1.0.0",
"extend-shallow": "^2.0.1",
"extend-shallow": "^3.0.2",
"is-extglob": "^2.1.1",
"is-glob": "^4.0.0"
"is-glob": "^4.0.1"
},
"devDependencies": {
"arr-union": "^3.1.0",
"array-unique": "^0.3.2",
"async-array-reduce": "^1.0.0",
"delete": "^1.1.0",
"glob": "^7.1.2",
"gulp-format-md": "^1.0.0",
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"mocha": "^3.2.0"
"glob": "^7.1.7",
"gulp-format-md": "^2.0.0",
"minimist": "^1.2.5",
"mkdirp": "^0.5.5",
"mocha": "^7.2.0",
"mock-require": "^3.0.3",
"sinon": "^9.2.4"
},
"keywords": [
"bash",
Expand Down
19 changes: 19 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require('mocha');
var assert = require('assert');
var sinon = require('sinon');
var mock = require('mock-require');
var glob = require('..');

describe('bash-glob', function() {
Expand Down Expand Up @@ -48,4 +50,21 @@ describe('bash-glob', function() {
}
});
});

it('throws exception if `bash` not found', function() {
var bashPathSpy = sinon.spy(function () { return null; });
mock('bash-path', bashPathSpy);

var glob = mock.reRequire('..')

try {
glob.sync(['*']);

} catch (err) {
assert(err);
assert.equal(err.message, '`bash` not found');
}

mock.stop('bash-path');
});
});