Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mock-fs

Package Overview
Dependencies
Maintainers
1
Versions
86
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 0.12.0 to 1.0.0

lib/error.js

93

lib/binding.js

@@ -7,2 +7,3 @@ var path = require('path');

var SymbolicLink = require('./symlink');
var FSError = require('./error');

@@ -41,13 +42,4 @@ var constants = process.binding('constants');

function noSuchFile(filepath, source) {
var code = 'ENOENT';
var errno = 34;
var error = new Error(code + ', ' + source + ' \'' + filepath + '\'');
error.code = code;
error.errno = errno;
return error;
}
/**

@@ -191,3 +183,3 @@ * Create a new stats object.

if (!this._openFiles.hasOwnProperty(fd)) {
throw new Error('EBADF, bad file descriptor');
throw new FSError('EBADF');
}

@@ -216,3 +208,3 @@ return this._openFiles[fd];

if (!this._openFiles.hasOwnProperty(fd)) {
throw new Error('EBADF, bad file descriptor');
throw new FSError('EBADF');
}

@@ -237,3 +229,3 @@ delete this._openFiles[fd];

if (!item) {
throw noSuchFile(filepath, 'stat');
throw new FSError('ENOENT', filepath);
}

@@ -289,3 +281,3 @@ return new Stats(item.getStats());

if (descriptor.isExclusive() && item) {
throw new Error('EEXIST, file already exists');
throw new FSError('EEXIST', pathname);
}

@@ -295,6 +287,6 @@ if (descriptor.isCreate() && !item) {

if (!parent) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', pathname);
}
if (!(parent instanceof Directory)) {
throw new Error('ENOTDIR, not a directory');
throw new FSError('ENOTDIR', pathname);
}

@@ -306,3 +298,3 @@ item = new File();

if (descriptor.isRead() && !item) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', pathname);
}

@@ -338,3 +330,3 @@ if (descriptor.isTruncate()) {

if (!descriptor.isRead()) {
throw new Error('EBADF, bad file descriptor');
throw new FSError('EBADF');
}

@@ -344,3 +336,3 @@ var file = descriptor.getItem();

// deleted or not a regular file
throw new Error('EBADF, bad file descriptor');
throw new FSError('EBADF');
}

@@ -377,3 +369,3 @@ if (typeof position !== 'number' || position < 0) {

if (!descriptor.isWrite()) {
throw new Error('EBADF, bad file descriptor');
throw new FSError('EBADF');
}

@@ -383,3 +375,3 @@ var file = descriptor.getItem();

// not a regular file
throw new Error('EBADF, bad file descriptor');
throw new FSError('EBADF');
}

@@ -455,3 +447,3 @@ if (typeof position !== 'number' || position < 0) {

if (!oldItem) {
throw noSuchFile(oldPath, 'rename');
throw new FSError('ENOENT', oldPath);
}

@@ -467,11 +459,10 @@ var oldParent = this._system.getItem(path.dirname(oldPath));

if (newItem instanceof Directory) {
// TODO: error factories
throw new Error('EISDIR, illegal operation on a directory');
throw new FSError('EISDIR', newPath);
}
} else if (oldItem instanceof Directory) {
if (!(newItem instanceof Directory)) {
throw new Error('ENOTDIR, not a directory');
throw new FSError('ENOTDIR', newPath);
}
if (newItem.list().length > 0) {
throw new Error('ENOTEMPTY, directory not empty');
throw new FSError('ENOTEMPTY', newPath);
}

@@ -482,6 +473,6 @@ }

if (!newParent) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', newPath);
}
if (!(newParent instanceof Directory)) {
throw new Error('ENOTDIR, not a directory');
throw new FSError('ENOTDIR', newPath);
}

@@ -506,6 +497,6 @@ }

if (!dir) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', dirpath);
}
if (!(dir instanceof Directory)) {
throw new Error('ENOTDIR, not a directory');
throw new FSError('ENOTDIR', dirpath);
}

@@ -527,7 +518,7 @@ return dir.list();

if (item) {
throw new Error('EEXIST, file already exists');
throw new FSError('EEXIST', pathname);
}
var parent = this._system.getItem(path.dirname(pathname));
if (!parent) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', pathname);
}

@@ -550,9 +541,9 @@ var dir = new Directory();

if (!item) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', pathname);
}
if (!(item instanceof Directory)) {
throw new Error('ENOTDIR, not a directory');
throw new FSError('ENOTDIR', pathname);
}
if (item.list().length > 0) {
throw new Error('ENOTEMPTY, directory not empty');
throw new FSError('ENOTEMPTY', pathname);
}

@@ -575,7 +566,7 @@ var parent = this._system.getItem(path.dirname(pathname));

if (!descriptor.isWrite()) {
throw new Error('EINVAL, invalid argument');
throw new FSError('EINVAL');
}
var file = descriptor.getItem();
if (!(file instanceof File)) {
throw new Error('EINVAL, invalid argument');
throw new FSError('EINVAL');
}

@@ -610,3 +601,3 @@ var content = file.getContent();

if (!item) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', pathname);
}

@@ -646,3 +637,3 @@ item.setUid(uid);

if (!item) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', pathname);
}

@@ -678,6 +669,6 @@ item.setMode(mode);

if (!item) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', pathname);
}
if (item instanceof Directory) {
throw new Error('EPERM, operation not permitted');
throw new FSError('EPERM', pathname);
}

@@ -701,3 +692,3 @@ var parent = this._system.getItem(path.dirname(pathname));

if (!item) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', pathname);
}

@@ -761,16 +752,16 @@ item.setATime(new Date(atime * 1000));

if (!item) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', srcPath);
}
if (item instanceof Directory) {
throw new Error('EPERM, operation not permitted');
throw new FSError('EPERM', srcPath);
}
if (this._system.getItem(destPath)) {
throw new Error('EEXIST, file already exists');
throw new FSError('EEXIST', destPath);
}
var parent = this._system.getItem(path.dirname(destPath));
if (!parent) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', destPath);
}
if (!(parent instanceof Directory)) {
throw new Error('ENOTDIR, not a directory');
throw new FSError('ENOTDIR', destPath);
}

@@ -792,10 +783,10 @@ parent.addItem(path.basename(destPath), item);

if (this._system.getItem(destPath)) {
throw new Error('EEXIST, file already exists');
throw new FSError('EEXIST', destPath);
}
var parent = this._system.getItem(path.dirname(destPath));
if (!parent) {
throw new Error('ENOENT, no such file or directory');
throw new FSError('ENOENT', destPath);
}
if (!(parent instanceof Directory)) {
throw new Error('ENOTDIR, not a directory');
throw new FSError('ENOTDIR', destPath);
}

@@ -819,3 +810,3 @@ var link = new SymbolicLink();

if (!(link instanceof SymbolicLink)) {
throw new Error('EINVAL, invalid argument');
throw new FSError('EINVAL', pathname);
}

@@ -837,3 +828,3 @@ return link.getPath();

if (!item) {
throw noSuchFile(filepath, 'stat');
throw new FSError('ENOENT', filepath);
}

@@ -840,0 +831,0 @@ return new Stats(item.getStats());

@@ -0,1 +1,2 @@

var realFs = require('fs');
var path = require('path');

@@ -11,4 +12,4 @@

'0.8': 'fs-0.8.26.js',
'0.10': 'fs-0.10.22.js',
'0.11': 'fs-0.11.9.js'
'0.10': 'fs-0.10.24.js',
'0.11': 'fs-0.11.10.js'
};

@@ -23,2 +24,32 @@

/**
* Copy properties from one object to another.
* @param {Object} obj The destination object.
* @param {Object} src The source object.
* @return {Object} The destination object.
*/
function mix(obj, src) {
for (var key in src) {
obj[key] = src[key];
}
return obj;
}
var originalFs = mix({}, realFs);
/**
* Override the real fs module with the given configuration. Returns a function
* that can be called to restore the original file system.
* @param {Object} config File system configuration.
* @return {function()} Function called to restore the original file system.
*/
var exports = module.exports = function(config) {
mix(realFs, exports.fs(config));
return function() {
mix(realFs, originalFs);
};
};
/**
* Create a new fs module based on the given file system configuration.

@@ -33,10 +64,10 @@ * @param {Object} config File system configuration.

// inject the mock binding
var fs = rewire(path.join(__dirname, '..', 'node', fsName));
fs.__set__('binding', binding);
var mockFs = rewire(path.join(__dirname, '..', 'node', fsName));
mockFs.__set__('binding', binding);
// overwrite fs.Stats from original binding
fs.Stats = binding.Stats;
mockFs.Stats = binding.Stats;
// provide a method to reconfigure the file system
fs._reconfigure = function(opt_config) {
mockFs._reconfigure = function(opt_config) {
var newConfig = opt_config || config;

@@ -47,3 +78,3 @@ var newSystem = FileSystem.create(newConfig);

return fs;
return mockFs;
};

@@ -50,0 +81,0 @@

{
"name": "mock-fs",
"description": "Mock fs implementation for testing",
"version": "0.12.0",
"version": "1.0.0",
"main": "lib/index.js",

@@ -6,0 +6,0 @@ "homepage": "https://github.com/tschaub/mock-fs",

@@ -7,3 +7,3 @@ # Mock FS

The code below creates a mock `fs` module that is configured to work with a few mock files and directories.
The code below makes it so the `fs` module is temporarily backed by a mock file system with a few files and directories.

@@ -13,3 +13,3 @@ ```js

var fs = mock.fs({
var restore = mock({
'path/to/fake/dir': {

@@ -24,13 +24,7 @@ 'some-file.txt': 'file content here',

If you are testing a module that `require`s the real `fs` module, you can use [`rewire`](https://npmjs.org/package/rewire) to inject a mock `fs` module for testing.
Note that the `mock` function returns a `restore` function. When you are ready to restore the `fs` module (so that it is backed by your real file system), call `restore()`.
```js
var rewire = require("rewire");
var moduleToTest = rewire('./path/to/module');
// inject the mock fs created above
moduleToTest.__set__('fs', fs);
// now functions in moduleToTest will use
// your mock fs instead of the real one
/// after a test runs
restore();
```

@@ -37,0 +31,0 @@

@@ -1,15 +0,11 @@

var rewire = require('rewire');
var mock = require('../../lib/index');
var assert = require('../helper').assert;
var count = rewire('./filecount');
var count = require('./filecount');
var fs = mock.fs();
count.__set__('fs', fs);
describe('count(dir, callback)', function() {
var restore;
beforeEach(function() {
mock.init(fs, {
restore = mock({
'path/to/dir': {

@@ -25,2 +21,5 @@ 'one.txt': 'first file',

});
afterEach(function() {
restore();
});

@@ -27,0 +26,0 @@ it('counts files in a directory', function(done) {

Sorry, the diff of this file is too big to display

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