nodejs-mock-fs/Mock-sync-methods-to-support-v20.8.patch
wang--ge 644962f5fd fix test failure
(cherry picked from commit 521b75251870190ae6e702319638c650ba97665b)
2024-05-07 12:09:13 +08:00

232 lines
7.2 KiB
Diff

From 639b6392b99dfb4cde8bdf3ee621f03ce851fd31 Mon Sep 17 00:00:00 2001
From: Andrew Nicols <andrew@nicols.co.uk>
Date: Sat, 14 Oct 2023 00:47:14 +0800
Subject: [PATCH] Mock sync methods to support v20.8
---
lib/binding.js | 141 ++++++++++++++++++++++++++++++++++++++++
test/lib/bypass.spec.js | 2 +-
2 files changed, 142 insertions(+), 1 deletion(-)
diff --git a/lib/binding.js b/lib/binding.js
index a4a3e6c..e9db26b 100644
--- a/lib/binding.js
+++ b/lib/binding.js
@@ -306,6 +306,17 @@ Binding.prototype.stat = function (filepath, bigint, callback, ctx) {
});
};
+/**
+ * Stat an item.
+ * @param {string} filepath Path.
+ * @param {boolean} bigint Use BigInt.
+ * @param {object} ctx Context object (optional), only for nodejs v10+.
+ * @return {Float64Array|BigUint64Array|undefined} Stats or undefined if sync.
+ */
+Binding.prototype.statSync = function (filepath, bigint, ctx) {
+ return this.stat(filepath, bigint, undefined, ctx);
+};
+
/**
* Stat an item.
* @param {number} fd File descriptor.
@@ -341,6 +352,16 @@ Binding.prototype.close = function (fd, callback, ctx) {
});
};
+/**
+ * Close a file descriptor.
+ * @param {number} fd File descriptor.
+ * @param {object} ctx Context object (optional), only for nodejs v10+.
+ * @return {*} The return.
+ */
+Binding.prototype.closeSync = function (fd, ctx) {
+ return this.close(fd, undefined, ctx);
+};
+
/**
* Open and possibly create a file.
* @param {string} pathname File path.
@@ -410,6 +431,18 @@ Binding.prototype.open = function (pathname, flags, mode, callback, ctx) {
});
};
+/**
+ * Open and possibly create a file.
+ * @param {string} pathname File path.
+ * @param {number} flags Flags.
+ * @param {number} mode Mode.
+ * @param {object} ctx Context object (optional), only for nodejs v10+.
+ * @return {string} File descriptor.
+ */
+Binding.prototype.openSync = function (pathname, flags, mode, ctx) {
+ return this.open(pathname, flags, mode, undefined, ctx);
+};
+
/**
* Open a file handler. A new api in nodejs v10+ for fs.promises
* @param {string} pathname File path.
@@ -531,6 +564,18 @@ Binding.prototype.copyFile = function (src, dest, flags, callback, ctx) {
});
};
+/**
+ * Write to a file descriptor given a buffer.
+ * @param {string} src Source file.
+ * @param {string} dest Destination file.
+ * @param {number} flags Modifiers for copy operation.
+ * @param {object} ctx Context object (optional), only for nodejs v10+.
+ * @return {*} The return if no callback is provided.
+ */
+Binding.prototype.copyFileSync = function (src, dest, flags, ctx) {
+ return this.copyFile(src, dest, flags, undefined, ctx);
+};
+
/**
* Write to a file descriptor given a buffer.
* @param {string} fd File descriptor.
@@ -722,6 +767,17 @@ Binding.prototype.rename = function (oldPath, newPath, callback, ctx) {
});
};
+/**
+ * Rename a file.
+ * @param {string} oldPath Old pathname.
+ * @param {string} newPath New pathname.
+ * @param {object} ctx Context object (optional), only for nodejs v10+.
+ * @return {undefined}
+ */
+Binding.prototype.renameSync = function (oldPath, newPath, ctx) {
+ return this.rename(oldPath, newPath, undefined, ctx);
+};
+
/**
* Read a directory.
* @param {string} dirpath Path to directory.
@@ -779,6 +835,24 @@ Binding.prototype.readdir = function (
});
};
+Binding.prototype.readFile = function (filepath, options, callback, ctx) {
+ markSyscall(ctx, 'readFile');
+
+ return maybeCallback(normalizeCallback(callback), ctx, this, function () {
+ filepath = deBuffer(filepath);
+ const item = this._system.getItem(filepath);
+
+ return item.getContent();
+ });
+};
+
+Binding.prototype.readFileSync = function (filepath, options, ctx) {
+ return this.readFile(filepath, options, undefined, ctx);
+};
+Binding.prototype.readFileUtf8 = function (filepath, options, ctx) {
+ return this.readFile(filepath, options, undefined, ctx).toString('utf-8');
+};
+
/**
* Create a directory.
* @param {string} pathname Path to new directory.
@@ -1059,6 +1133,16 @@ Binding.prototype.unlink = function (pathname, callback, ctx) {
});
};
+/**
+ * Delete a named item.
+ * @param {string} pathname Path to item.
+ * @param {object} ctx Context object (optional), only for nodejs v10+.
+ * @return {*} The return if no callback is provided.
+ */
+Binding.prototype.unlinkSync = function (pathname, ctx) {
+ return this.unlink(pathname, undefined, ctx);
+};
+
/**
* Update timestamps.
* @param {string} pathname Path to item.
@@ -1241,6 +1325,18 @@ Binding.prototype.symlink = function (srcPath, destPath, type, callback, ctx) {
});
};
+/**
+ * Create a symbolic link.
+ * @param {string} srcPath Path from link to the source file.
+ * @param {string} destPath Path for the generated link.
+ * @param {string} type Ignored (used for Windows only).
+ * @param {object} ctx Context object (optional), only for nodejs v10+.
+ * @return {*} The return if no callback is provided.
+ */
+Binding.prototype.symlinkSync = function (srcPath, destPath, type, ctx) {
+ return this.symlink(srcPath, destPath, type, undefined, ctx);
+};
+
/**
* Read the contents of a symbolic link.
* @param {string} pathname Path to symbolic link.
@@ -1338,6 +1434,51 @@ Binding.prototype.access = function (filepath, mode, callback, ctx) {
});
};
+/**
+ * Tests user permissions.
+ * @param {string} filepath Path.
+ * @param {number} mode Mode.
+ * @param {object} ctx Context object (optional), only for nodejs v10+.
+ * @return {*} The return if no callback is provided.
+ */
+Binding.prototype.accessSync = function (filepath, mode, ctx) {
+ return this.access(filepath, mode, undefined, ctx);
+};
+
+/**
+ * Tests whether or not the given path exists.
+ * @param {string} filepath Path.
+ * @param {function(Error)} callback Callback (optional).
+ * @param {object} ctx Context object (optional), only for nodejs v10+.
+ * @return {*} The return if no callback is provided.
+ */
+Binding.prototype.exists = function (filepath, callback, ctx) {
+ markSyscall(ctx, 'exists');
+
+ return maybeCallback(normalizeCallback(callback), ctx, this, function () {
+ filepath = deBuffer(filepath);
+ const item = this._system.getItem(filepath);
+
+ if (item) {
+ if (item instanceof SymbolicLink) {
+ return this.exists(item.getPath(), callback, ctx);
+ }
+ return true;
+ }
+ return false;
+ });
+};
+
+/**
+ * Tests whether or not the given path exists.
+ * @param {string} filepath Path.
+ * @param {object} ctx Context object (optional), only for nodejs v10+.
+ * @return {*} The return if no callback is provided.
+ */
+Binding.prototype.existsSync = function (filepath, ctx) {
+ return this.exists(filepath, undefined, ctx);
+};
+
/**
* Not yet implemented.
* @type {function()}
diff --git a/test/lib/bypass.spec.js b/test/lib/bypass.spec.js
index 23bbc49..078cf81 100644
--- a/test/lib/bypass.spec.js
+++ b/test/lib/bypass.spec.js
@@ -13,7 +13,7 @@ describe('mock.bypass()', () => {
it('runs a synchronous function using the real filesystem', () => {
mock({'/path/to/file': 'content'});
- assert.equal(fs.readFileSync('/path/to/file', 'utf8'), 'content');
+ assert.equal(fs.readFileSync('/path/to/file', 'utf-8'), 'content');
assert.isNotOk(fs.existsSync(__filename));
assert.isOk(mock.bypass(() => fs.existsSync(__filename)));