Skip to content

Commit

Permalink
Add fs option to allow passing virtual filesystem
Browse files Browse the repository at this point in the history
PR-URL: #430
Credit: @mmkal
Close: #430
Reviewed-by: @isaacs
  • Loading branch information
mmkal authored and isaacs committed Sep 22, 2021
1 parent ce43ea0 commit df4598a
Show file tree
Hide file tree
Showing 7 changed files with 1,119 additions and 1,139 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ the filesystem.
* `absolute` Set to true to always receive absolute paths for matched
files. Unlike `realpath`, this also affects the values returned in
the `match` event.
* `fs` File-system object with Node's `fs` API. By default, the built-in
`fs` module will be used. Set to a volume provided by a library like
`memfs` to avoid using the "real" file-system.

## Comparisons to other fnmatch/glob implementations

Expand Down
2 changes: 2 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function ownProp (obj, field) {
return Object.prototype.hasOwnProperty.call(obj, field)
}

var fs = require("fs")
var path = require("path")
var minimatch = require("minimatch")
var isAbsolute = require("path-is-absolute")
Expand Down Expand Up @@ -75,6 +76,7 @@ function setopts (self, pattern, options) {
self.stat = !!options.stat
self.noprocess = !!options.noprocess
self.absolute = !!options.absolute
self.fs = options.fs || fs

self.maxLength = options.maxLength || Infinity
self.cache = options.cache || Object.create(null)
Expand Down
9 changes: 4 additions & 5 deletions glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

module.exports = glob

var fs = require('fs')
var rp = require('fs.realpath')
var minimatch = require('minimatch')
var Minimatch = minimatch.Minimatch
Expand Down Expand Up @@ -501,7 +500,7 @@ Glob.prototype._readdirInGlobStar = function (abs, cb) {
var lstatcb = inflight(lstatkey, lstatcb_)

if (lstatcb)
fs.lstat(abs, lstatcb)
self.fs.lstat(abs, lstatcb)

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

var self = this
fs.readdir(abs, readdirCb(this, abs, cb))
self.fs.readdir(abs, readdirCb(this, abs, cb))
}

function readdirCb (self, abs, cb) {
Expand Down Expand Up @@ -746,13 +745,13 @@ Glob.prototype._stat = function (f, cb) {
var self = this
var statcb = inflight('stat\0' + abs, lstatcb_)
if (statcb)
fs.lstat(abs, statcb)
self.fs.lstat(abs, statcb)

function lstatcb_ (er, lstat) {
if (lstat && lstat.isSymbolicLink()) {
// If it's a symlink, then treat it as the target, unless
// the target does not exist, then treat it as a file.
return fs.stat(abs, function (er, stat) {
return self.fs.stat(abs, function (er, stat) {
if (er)
self._stat2(f, abs, null, lstat, cb)
else
Expand Down
Loading

0 comments on commit df4598a

Please sign in to comment.