Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock sync methods to support v20.8 #381

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
141 changes: 141 additions & 0 deletions lib/binding.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()}
Expand Down
2 changes: 1 addition & 1 deletion test/lib/bypass.spec.js
Expand Up @@ -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)));

Expand Down