Skip to content
This repository was archived by the owner on Feb 19, 2022. It is now read-only.
This repository was archived by the owner on Feb 19, 2022. It is now read-only.

BUG: mock-fs and fs-extra collide in tests. #10

@ryan-roemer

Description

@ryan-roemer

Issue mock-fs@3.11.0 breaks fs-extra in our tests...

    var readStream = fs.createReadStream(file.name)
    var writeStream = fs.createWriteStream(target, { mode: file.mode })

    // readStream and writeStream don't have an `on` method now...

    readStream.on('error', onError)
    writeStream.on('error', onError)

Temporarily hacking around with hard pin of dependency at:

    "mock-fs": "3.10.0",

Here's a diff of what changed:

$ publish-diff -o mock-fs@3.10.0 -n mock-fs@3.11.0
Index: changelog.md
===================================================================
--- changelog.md    mock-fs@3.10.0
+++ changelog.md    mock-fs@3.11.0
@@ -1,6 +1,11 @@
 # Change Log

+## 3.11.0
+
+ * Make `require()` calls use the real filesystem ([#139][#139]).
+ * Reduce the manual `fs` module patching ([#140][#140]).
+
 ## 3.10.0

  * Fixes for Node 6.3 ([#138][#138]).
  * Fix permissions issues on directories (thanks @as3richa, see [#105][#105]).
@@ -130,4 +135,6 @@
 [#94]: https://github.com/tschaub/mock-fs/pull/94
 [#107]: https://github.com/tschaub/mock-fs/pull/107
 [#105]: https://github.com/tschaub/mock-fs/pull/105
 [#138]: https://github.com/tschaub/mock-fs/pull/138
+[#139]: https://github.com/tschaub/mock-fs/pull/139
+[#140]: https://github.com/tschaub/mock-fs/pull/140

Index: dir/foo
===================================================================
--- dir/foo mock-fs@3.10.0
+++ dir/foo mock-fs@3.11.0

Index: lib/binding.js
===================================================================
--- lib/binding.js  mock-fs@3.10.0
+++ lib/binding.js  mock-fs@3.11.0
@@ -28,9 +28,11 @@
     } catch (e) {
       err = e;
     }
     // Unpack callback from FSReqWrap
-    callback = callback.oncomplete || callback;
+    if (callback.oncomplete) {
+      callback = callback.oncomplete.bind(callback);
+    }
     process.nextTick(function() {
       if (val === undefined) {
         callback(err);
       } else {
@@ -519,9 +521,11 @@
     callback) {
   var buffer = new Buffer(string, encoding);
   var wrapper;
   if (callback) {
-    callback = callback.oncomplete || callback;
+    if (callback.oncomplete) {
+      callback = callback.oncomplete.bind(callback);
+    }
     wrapper = function(err, written, returned) {
       callback(err, written, returned && string);
     };
   }

Index: lib/index.js
===================================================================
--- lib/index.js    mock-fs@3.10.0
+++ lib/index.js    mock-fs@3.11.0
@@ -49,9 +49,18 @@
 for (var name in mockFs) {
   var descriptor = Object.getOwnPropertyDescriptor(realFs, name);

   if (!descriptor || descriptor && descriptor.writable) {
-    realFs[name] = mockFs[name];
+    realFs[name] = (function(mockFunction, realFunction) {
+      return function() {
+        var stack = new Error().stack;
+        if (stack.indexOf('at Module.require (module') >= 0) {
+          return realFunction.apply(realFs, arguments);
+        } else {
+          return mockFunction.apply(realFs, arguments);
+        }
+      };
+    }(mockFs[name], realFs[name]));
   }
 }
 var originalProcess = {
   cwd: process.cwd,

Index: node/fs-6.3.0.js
===================================================================
--- node/fs-6.3.0.js    mock-fs@3.10.0
+++ node/fs-6.3.0.js    mock-fs@3.11.0
@@ -248,9 +248,9 @@
   var context = new ReadFileContext(callback, encoding);
   context.isUserFd = isFd(path); // file descriptor ownership
   var req = new FSReqWrap();
   req.context = context;
-  req.oncomplete = readFileAfterOpen.bind(req);
+  req.oncomplete = readFileAfterOpen;

   if (context.isUserFd) {
     process.nextTick(function() {
       req.oncomplete(null, path);
@@ -293,17 +293,17 @@
     length = this.size - this.pos;
   }

   var req = new FSReqWrap();
-  req.oncomplete = readFileAfterRead.bind(req);
+  req.oncomplete = readFileAfterRead;
   req.context = this;

   binding.read(this.fd, buffer, offset, length, -1, req);
 };

 ReadFileContext.prototype.close = function(err) {
   var req = new FSReqWrap();
-  req.oncomplete = readFileAfterClose.bind(req);
+  req.oncomplete = readFileAfterClose;
   req.context = this;
   this.err = err;

   if (this.isUserFd) {
@@ -326,9 +326,9 @@

   context.fd = fd;

   var req = new FSReqWrap();
-  req.oncomplete = readFileAfterStat.bind(req);
+  req.oncomplete = readFileAfterStat;
   req.context = context;
   binding.fstat(fd, req);
 }


Index: package.json
===================================================================
--- package.json    mock-fs@3.10.0
+++ package.json    mock-fs@3.11.0
@@ -1,8 +1,8 @@
 {
   "name": "mock-fs",
   "description": "A configurable mock file system.  You know, for testing.",
-  "version": "3.10.0",
+  "version": "3.11.0",
   "main": "lib/index.js",
   "homepage": "https://github.com/tschaub/mock-fs",
   "author": {
     "name": "Tim Schaub",

Index: smoke.js
===================================================================
--- smoke.js    mock-fs@3.10.0
+++ smoke.js    mock-fs@3.11.0
@@ -1,24 +1,6 @@
-
 var mock = require('./lib');
-var fs = require('fs');

-mock({
-  dir: {
-    link: mock.symlink({
-      path: 'foo'
-    }),
-    foo: {
-      bar: 'content'
-    }
-  }
-});
+mock({'test.json': JSON.stringify({name: 'bam'})}, {mockRequire: true});
+var pkg = require('./test.json');

-fs.readdir('dir', function(err, items) {
-  if (err) {
-    process.stderr.write(`${err.stack}\n`);
-    process.exit(1);
-  } else {
-    process.stdout.write(`items: ${items}\n`);
-    process.exit(0);
-  }
-});
+console.log(pkg.name);

Index: test/lib/index.spec.js
===================================================================
--- test/lib/index.spec.js  mock-fs@3.10.0
+++ test/lib/index.spec.js  mock-fs@3.11.0
@@ -63,8 +63,17 @@

       mock.restore();
     });

+    it('uses the real fs module in require() calls', function() {
+      mock({foo: 'bar'});
+
+      var pkg = require('../../package.json');
+      assert.equal(pkg.name, 'mock-fs');
+
+      mock.restore();
+    });
+
   });

   describe('mock.restore()', function() {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions