tschaub / mock-fs

Configurable mock for the fs module
https://npmjs.org/package/mock-fs
Other
912 stars 86 forks source link

Add node v5.x compatibility #69

Closed tmcw closed 9 years ago

tmcw commented 9 years ago

This adds node v5 compatibility to mock-fs. Fixes #67

The node/fs-5.0.0.js is the same as https://github.com/nodejs/node/blob/v5.x/lib/fs.js except:

--- v5.js   2015-11-28 20:45:32.000000000 -0500
+++ node/fs-5.0.0.js    2015-11-28 20:43:23.000000000 -0500
@@ -7,8 +7,8 @@
 const util = require('util');
 const pathModule = require('path');

-const binding = process.binding('fs');
-const constants = require('constants');
+var binding = process.binding('fs');
+const constants = process.binding('constants');
 const fs = exports;
 const Buffer = require('buffer').Buffer;
 const Stream = require('stream').Stream;
@@ -250,7 +250,7 @@
   context.isUserFd = isFd(path); // file descriptor ownership
   var req = new FSReqWrap();
   req.context = context;
-  req.oncomplete = readFileAfterOpen;
+  req.oncomplete = readFileAfterOpen.bind(req);

   if (context.isUserFd) {
     process.nextTick(function() {
@@ -295,7 +295,7 @@
   }

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

   binding.read(this.fd, buffer, offset, length, -1, req);
@@ -303,7 +303,7 @@

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

@@ -328,7 +328,7 @@
   context.fd = fd;

   var req = new FSReqWrap();
-  req.oncomplete = readFileAfterStat;
+  req.oncomplete = readFileAfterStat.bind(req);
   req.context = context;
   binding.fstat(fd, req);
 }
@@ -2072,4 +2072,4 @@
   return true;
 };
tschaub commented 9 years ago

Awesome. Thanks @tmcw. Seeing the patch here is very useful, and I regret not including these in the repo from the start. I keep thinking I'll rework this to avoid the patching, but haven't taken the time yet.