Socket
Socket
Sign inDemoInstall

mock-fs

Package Overview
Dependencies
Maintainers
1
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-fs - npm Package Compare versions

Comparing version 5.2.0 to 5.3.0

175

lib/binding.js

@@ -47,3 +47,3 @@ 'use strict';

if (kUsePromises && callback === kUsePromises) {
if (usePromises(callback)) {
// support nodejs v10+ fs.promises

@@ -90,2 +90,6 @@ try {

function usePromises(callback) {
return kUsePromises && callback === kUsePromises;
}
/**

@@ -313,2 +317,13 @@ * set syscall property on context object, only for nodejs v10+.

* 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.

@@ -348,2 +363,12 @@ * @param {boolean} bigint Use BigInt.

/**
* 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.

@@ -362,3 +387,3 @@ * @param {string} pathname File path.

pathname = deBuffer(pathname);
const descriptor = new FileDescriptor(flags);
const descriptor = new FileDescriptor(flags, usePromises(callback));
let item = this._system.getItem(pathname);

@@ -419,2 +444,14 @@ while (item instanceof SymbolicLink) {

/**
* 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

@@ -542,2 +579,14 @@ * @param {string} pathname File path.

* 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.

@@ -642,3 +691,8 @@ * @param {Array<Buffer>} buffers Array of buffers with contents to write.

descriptor.setPosition(newLength);
return written;
// If we're in fs.promises / FileHandle we need to return a promise
// Both fs.promises.open().then(fd => fs.write())
// and fs.openSync().writeSync() use this function
// without a callback, so we have to check if the descriptor was opened
// with kUsePromises
return descriptor.isPromise() ? Promise.resolve(written) : written;
});

@@ -734,2 +788,13 @@ };

/**
* 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.

@@ -792,2 +857,39 @@ * @param {string} dirpath Path to directory.

/**
* Read file as utf8 string.
* @param {string} name file to write.
* @param {number} flags Flags.
* @return {string} the file content.
*/
Binding.prototype.readFileUtf8 = function (name, flags) {
const fd = this.open(name, flags);
const descriptor = this.getDescriptorById(fd);
if (!descriptor.isRead()) {
throw new FSError('EBADF');
}
const file = descriptor.getItem();
if (file instanceof Directory) {
throw new FSError('EISDIR');
}
if (!(file instanceof File)) {
// deleted or not a regular file
throw new FSError('EBADF');
}
const content = file.getContent();
return content.toString('utf8');
};
/**
* Write a utf8 string.
* @param {string} filepath file to write.
* @param {string} data data to write to filepath.
* @param {number} flags Flags.
* @param {number} mode Mode.
*/
Binding.prototype.writeFileUtf8 = function (filepath, data, flags, mode) {
const destFd = this.open(filepath, flags, mode);
this.writeBuffer(destFd, data, 0, data.length);
};
/**
* Create a directory.

@@ -1073,2 +1175,12 @@ * @param {string} pathname Path to new directory.

/**
* 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.

@@ -1256,2 +1368,14 @@ * @param {string} pathname Path to item.

/**
* 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.

@@ -1354,2 +1478,47 @@ * @param {string} pathname Path to symbolic link.

/**
* 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.

@@ -1356,0 +1525,0 @@ * @type {function()}

@@ -8,5 +8,6 @@ 'use strict';

* @param {number} flags Flags.
* @param {boolean} isPromise descriptor was opened via fs.promises
* @class
*/
function FileDescriptor(flags) {
function FileDescriptor(flags, isPromise = false) {
/**

@@ -29,2 +30,4 @@ * Flags.

this._position = 0;
this._isPromise = isPromise;
}

@@ -116,2 +119,10 @@

/**
* Check if the file descriptor was opened as a promise
* @return {boolean} Opened from fs.promise
*/
FileDescriptor.prototype.isPromise = function () {
return this._isPromise;
};
/**
* Export the constructor.

@@ -118,0 +129,0 @@ * @type {function()}

@@ -267,11 +267,19 @@ 'use strict';

file.setATime(config.atime);
} else if (config.hasOwnProperty('atimeMs')) {
file.setATime(new Date(config.atimeMs));
}
if (config.hasOwnProperty('ctime')) {
file.setCTime(config.ctime);
} else if (config.hasOwnProperty('ctimeMs')) {
file.setCTime(new Date(config.ctimeMs));
}
if (config.hasOwnProperty('mtime')) {
file.setMTime(config.mtime);
} else if (config.hasOwnProperty('mtimeMs')) {
file.setMTime(new Date(config.mtimeMs));
}
if (config.hasOwnProperty('birthtime')) {
file.setBirthtime(config.birthtime);
} else if (config.hasOwnProperty('birthtimeMs')) {
file.setBirthtime(new Date(config.birthtimeMs));
}

@@ -309,11 +317,19 @@ return file;

link.setATime(config.atime);
} else if (config.hasOwnProperty('atimeMs')) {
link.setATime(new Date(config.atimeMs));
}
if (config.hasOwnProperty('ctime')) {
link.setCTime(config.ctime);
} else if (config.hasOwnProperty('ctimeMs')) {
link.setCTime(new Date(config.ctimeMs));
}
if (config.hasOwnProperty('mtime')) {
link.setMTime(config.mtime);
} else if (config.hasOwnProperty('mtimeMs')) {
link.setMTime(new Date(config.mtimeMs));
}
if (config.hasOwnProperty('birthtime')) {
link.setBirthtime(config.birthtime);
} else if (config.hasOwnProperty('birthtimeMs')) {
link.setBirthtime(new Date(config.birthtimeMs));
}

@@ -349,11 +365,19 @@ return link;

dir.setATime(config.atime);
} else if (config.hasOwnProperty('atimeMs')) {
dir.setATime(new Date(config.atimeMs));
}
if (config.hasOwnProperty('ctime')) {
dir.setCTime(config.ctime);
} else if (config.hasOwnProperty('ctimeMs')) {
dir.setCTime(new Date(config.ctimeMs));
}
if (config.hasOwnProperty('mtime')) {
dir.setMTime(config.mtime);
} else if (config.hasOwnProperty('mtimeMs')) {
dir.setMTime(new Date(config.mtimeMs));
}
if (config.hasOwnProperty('birthtime')) {
dir.setBirthtime(config.birthtime);
} else if (config.hasOwnProperty('birthtimeMs')) {
dir.setBirthtime(new Date(config.birthtimeMs));
}

@@ -360,0 +384,0 @@ return dir;

6

package.json
{
"name": "mock-fs",
"description": "A configurable mock file system. You know, for testing.",
"version": "5.2.0",
"version": "5.3.0",
"main": "lib/index.js",

@@ -59,4 +59,4 @@ "homepage": "https://github.com/tschaub/mock-fs",

"eslint-config-tschaub": "^14.1.2",
"mocha": "^9.2.2",
"rimraf": "^3.0.2",
"mocha": "^10.7.3",
"rimraf": "^6.0.1",
"semver": "^7.3.5"

@@ -63,0 +63,0 @@ },

@@ -273,3 +273,3 @@ [![Build Status](https://github.com/tschaub/mock-fs/workflows/Test/badge.svg)](https://github.com/tschaub/mock-fs/actions?workflow=Test)

Tested on Linux, OSX, and Windows using Node 12 through 16. Check the tickets for a list of [known issues](https://github.com/tschaub/mock-fs/issues).
Tested on Linux, OSX, and Windows using Node 16 through 18. Check the tickets for a list of [known issues](https://github.com/tschaub/mock-fs/issues).

@@ -276,0 +276,0 @@ ### Using with Jest Snapshot Testing

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc