Skip to content

Commit df4598a

Browse files
mmkalisaacs
authored andcommitted
Add fs option to allow passing virtual filesystem
PR-URL: #430 Credit: @mmkal Close: #430 Reviewed-by: @isaacs
1 parent ce43ea0 commit df4598a

7 files changed

+1119
-1139
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ the filesystem.
276276
* `absolute` Set to true to always receive absolute paths for matched
277277
files. Unlike `realpath`, this also affects the values returned in
278278
the `match` event.
279+
* `fs` File-system object with Node's `fs` API. By default, the built-in
280+
`fs` module will be used. Set to a volume provided by a library like
281+
`memfs` to avoid using the "real" file-system.
279282

280283
## Comparisons to other fnmatch/glob implementations
281284

common.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function ownProp (obj, field) {
1010
return Object.prototype.hasOwnProperty.call(obj, field)
1111
}
1212

13+
var fs = require("fs")
1314
var path = require("path")
1415
var minimatch = require("minimatch")
1516
var isAbsolute = require("path-is-absolute")
@@ -75,6 +76,7 @@ function setopts (self, pattern, options) {
7576
self.stat = !!options.stat
7677
self.noprocess = !!options.noprocess
7778
self.absolute = !!options.absolute
79+
self.fs = options.fs || fs
7880

7981
self.maxLength = options.maxLength || Infinity
8082
self.cache = options.cache || Object.create(null)

glob.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
module.exports = glob
4242

43-
var fs = require('fs')
4443
var rp = require('fs.realpath')
4544
var minimatch = require('minimatch')
4645
var Minimatch = minimatch.Minimatch
@@ -501,7 +500,7 @@ Glob.prototype._readdirInGlobStar = function (abs, cb) {
501500
var lstatcb = inflight(lstatkey, lstatcb_)
502501

503502
if (lstatcb)
504-
fs.lstat(abs, lstatcb)
503+
self.fs.lstat(abs, lstatcb)
505504

506505
function lstatcb_ (er, lstat) {
507506
if (er && er.code === 'ENOENT')
@@ -542,7 +541,7 @@ Glob.prototype._readdir = function (abs, inGlobStar, cb) {
542541
}
543542

544543
var self = this
545-
fs.readdir(abs, readdirCb(this, abs, cb))
544+
self.fs.readdir(abs, readdirCb(this, abs, cb))
546545
}
547546

548547
function readdirCb (self, abs, cb) {
@@ -746,13 +745,13 @@ Glob.prototype._stat = function (f, cb) {
746745
var self = this
747746
var statcb = inflight('stat\0' + abs, lstatcb_)
748747
if (statcb)
749-
fs.lstat(abs, statcb)
748+
self.fs.lstat(abs, statcb)
750749

751750
function lstatcb_ (er, lstat) {
752751
if (lstat && lstat.isSymbolicLink()) {
753752
// If it's a symlink, then treat it as the target, unless
754753
// the target does not exist, then treat it as a file.
755-
return fs.stat(abs, function (er, stat) {
754+
return self.fs.stat(abs, function (er, stat) {
756755
if (er)
757756
self._stat2(f, abs, null, lstat, cb)
758757
else

0 commit comments

Comments
 (0)