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

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

Open
ryan-roemer opened this issue Oct 20, 2016 · 0 comments
Open

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

ryan-roemer opened this issue Oct 20, 2016 · 0 comments

Comments

@ryan-roemer
Copy link
Member

ryan-roemer commented Oct 20, 2016

Issue [email protected] 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:

Index: changelog.md
===================================================================
--- changelog.md    [email protected]
+++ changelog.md    [email protected]
@@ -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 [email protected]
+++ dir/foo [email protected]

Index: lib/binding.js
===================================================================
--- lib/binding.js  [email protected]
+++ lib/binding.js  [email protected]
@@ -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    [email protected]
+++ lib/index.js    [email protected]
@@ -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    [email protected]
+++ node/fs-6.3.0.js    [email protected]
@@ -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    [email protected]
+++ package.json    [email protected]
@@ -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    [email protected]
+++ smoke.js    [email protected]
@@ -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  [email protected]
+++ test/lib/index.spec.js  [email protected]
@@ -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() {
exogen added a commit that referenced this issue Oct 20, 2016
* End generated dev package.json with a newline
* Pin mock-fs to 3.10.0 to temporarily bandaid #10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant