Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding options.cwd option to allow searching from a different base di…

…rectory than process.cwd().
  • Loading branch information...
commit 3d011ef63ad58c8b8bb38fbf0b93a3b9e7026bbd 1 parent 301bb53
@cowboy authored
Showing with 18 additions and 2 deletions.
  1. +3 −2 lib/glob.js
  2. +15 −0 test/glob_test.js
View
5 lib/glob.js
@@ -27,8 +27,9 @@ function normalize(filepath) {
// A very simple, not-at-all-efficient, synchronous file globbing util.
exports.glob = function(pattern, options) {
if (!options) { options = {}; }
+ var cwd = options.cwd || process.cwd();
// The current absolute working directory.
- var base = normalize(path.join(process.cwd(), pathSeparator));
+ var base = normalize(path.join(cwd, pathSeparator));
// The passed pattern, resolved to an absolute path.
var absPattern = normalize(path.resolve(base, pattern));
// Since path.resolve strips off trailing '/', add it back if necessary.
@@ -90,7 +91,7 @@ exports.glob = function(pattern, options) {
// that is relative to the cwd.
if (!wasAbsolute) {
filepaths = filepaths.map(function(filepath) {
- var relPath = normalize(path.relative(process.cwd(), filepath));
+ var relPath = normalize(path.relative(cwd, filepath));
if (/\/$/.test(filepath)) { relPath += '/'; }
return relPath;
});
View
15 test/glob_test.js
@@ -29,6 +29,13 @@ function fileList(arr, prefix) {
});
}
+function relFileList(arr) {
+ return fileList(arr).map(function(filepath) {
+ // Strip leading test/fixture/ from each path
+ return filepath.replace(/^test\/fixture\//, '');
+ });
+}
+
exports['globsync'] = {
'relative': function(test) {
test.expect(11);
@@ -82,5 +89,13 @@ exports['globsync'] = {
test.deepEqual(globsync.glob('**/boot', {matchBase: true}), fileList([0, 4, 5, 6], ''), 'should match.');
test.deepEqual(globsync.glob('**/b*/boot', {matchBase: true}), fileList([4, 5], ''), 'should match.');
test.done();
+ },
+ 'options.cwd': function(test) {
+ test.expect(4);
+ test.deepEqual(globsync.glob('**', {cwd: 'test/fixture'}), relFileList([0,1,2,3,4,5,6]), '** should match');
+ test.deepEqual(globsync.glob('**', {cwd: 'test/fixture/'}), relFileList([0,1,2,3,4,5,6]), '** should match');
+ test.deepEqual(globsync.glob('*', {cwd: 'test/fixture'}), relFileList([0,1]), '* should match');
+ test.deepEqual(globsync.glob('*/', {cwd: 'test/fixture'}), relFileList([1]), '* should match');
+ test.done();
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.