Skip to content

Commit d4160d6

Browse files
committed
Use "which" to look for grep / find.
1 parent d1591e0 commit d4160d6

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"grunt-contrib-watch": "~0.5.3",
3434
"grunt": "~0.4.1",
3535
"async": "~0.2.9",
36-
"diff": "~1.0.7"
36+
"diff": "~1.0.7",
37+
"which": "~1.0.5"
3738
},
3839
"keywords": [
3940
"exit",

test/exit_test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,27 @@ var exec = require('child_process').exec;
2525
var async = require('async');
2626
var jsdiff = require('diff');
2727

28+
var _which = require('which').sync;
29+
function which(command) {
30+
try {
31+
return _which(command);
32+
} catch (err) {
33+
return false;
34+
}
35+
}
36+
37+
// Look for grep first (any OS). If not found (but on Windows) look for find,
38+
// which is Windows' horribly crippled grep alternative.
39+
var grep = which('grep') || process.platform === 'win32' && which('find');
40+
2841
function run(command, options, callback) {
2942
if (typeof options === 'function') {
3043
callback = options;
3144
options = {};
3245
}
3346
command += ' 2>&1';
3447
if (options.pipe) {
35-
command += ' | ' + (process.platform === 'win32' ? 'find' : 'grep') + ' "std"';
48+
command += ' | ' + grep + ' "std"';
3649
}
3750
exec(command, function(error, stdout) {
3851
callback(command, error ? error.code : 0, stdout);
@@ -69,6 +82,12 @@ exports['exit'] = {
6982
process.chdir(this.origCwd);
7083
done();
7184
},
85+
'grep': function(test) {
86+
test.expect(1);
87+
// Many unit tests depend on this.
88+
test.ok(grep, 'A suitable "grep" or "find" program was not found in the PATH.');
89+
test.done();
90+
},
7291
'stdout stderr': function(test) {
7392
var counts = [10, 100, 1000];
7493
test.expect(counts.length);

0 commit comments

Comments
 (0)