New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mock-fs

Package Overview
Dependencies
Maintainers
1
Versions
87
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 2.7.1 to 3.0.0

.editorconfig

1

benchmarks/write-mock.js
var fs = require('fs');
var path = require('path');

@@ -4,0 +3,0 @@ var mock = require('..');

# Change Log
## 3.0.0
* Override `process.cwd()` and `process.chdir()` to work with mocked filesystem (thanks @timkendrick, see [#41][#41]).
* Add note about known incompatibilities (thanks @psalaets, see [#45][#45]).
## 2.7.0

@@ -68,1 +73,3 @@

[#38]: https://github.com/tschaub/mock-fs/pull/38
[#41]: https://github.com/tschaub/mock-fs/pull/41
[#45]: https://github.com/tschaub/mock-fs/pull/45

@@ -50,3 +50,2 @@ 'use strict';

/**

@@ -130,3 +129,2 @@ * Create a new stats object.

/**

@@ -133,0 +131,0 @@ * Create a new binding with the given file system.

@@ -6,3 +6,2 @@ 'use strict';

/**

@@ -9,0 +8,0 @@ * Create a new file descriptor.

'use strict';
var path = require('path');
var util = require('util');

@@ -11,3 +10,2 @@

/**

@@ -14,0 +12,0 @@ * A directory.

@@ -248,3 +248,2 @@ 'use strict';

/**

@@ -251,0 +250,0 @@ * Create an error.

'use strict';
var path = require('path');
var util = require('util');

@@ -12,3 +11,2 @@

/**

@@ -15,0 +13,0 @@ * A directory.

@@ -31,3 +31,2 @@ 'use strict';

/**

@@ -34,0 +33,0 @@ * Create a new file system.

@@ -11,2 +11,3 @@ 'use strict';

var FileSystem = require('./filesystem');
var FSError = require('./error');

@@ -52,2 +53,6 @@ var versions = {

}
var originalProcess = {
cwd: process.cwd,
chdir: process.chdir
};

@@ -59,3 +64,8 @@ function setBinding(binding, Stats) {

function setProcess(cwd, chdir) {
process.cwd = cwd;
process.chdir = chdir;
}
/**

@@ -69,2 +79,15 @@ * Swap out the fs bindings for a mock file system.

setBinding(binding, binding.Stats);
var currentPath = process.cwd();
setProcess(
function cwd() {
return currentPath;
},
function chdir(directory) {
if (!mockFs.statSync(directory).isDirectory()) {
throw new FSError('ENOTDIR');
}
currentPath = path.resolve(currentPath, directory);
}
);
};

@@ -78,2 +101,3 @@

setBinding(originalBinding, originalStats);
setProcess(originalProcess.cwd, originalProcess.chdir);
};

@@ -92,9 +116,9 @@

// inject the mock binding
var mockFs = rewire(path.join(__dirname, '..', 'node', fsName));
mockFs.__set__('binding', binding);
var newMockFs = rewire(path.join(__dirname, '..', 'node', fsName));
newMockFs.__set__('binding', binding);
// overwrite fs.Stats from original binding
mockFs.Stats = binding.Stats;
newMockFs.Stats = binding.Stats;
return mockFs;
return newMockFs;
};

@@ -101,0 +125,0 @@

'use strict';
var path = require('path');
var counter = 0;

@@ -33,3 +31,2 @@

/**

@@ -36,0 +33,0 @@ * A filesystem item.

'use strict';
var path = require('path');
var util = require('util');

@@ -11,3 +10,2 @@

/**

@@ -14,0 +12,0 @@ * A directory.

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

@@ -28,13 +28,14 @@ "homepage": "https://github.com/tschaub/mock-fs",

"scripts": {
"debug": "node --debug-brk ./tasks.js test",
"test": "node ./tasks.js lint test",
"pretest": "eslint benchmarks lib test",
"test": "mocha --recursive test",
"bench": "bench benchmarks"
},
"devDependencies": {
"chai": "~1.8.1",
"jshint": "~2.4.1",
"mocha": "~1.17.0",
"bench-it": "^0.3.0",
"chai": "^3.0.0",
"eslint": "^0.23.0",
"eslint-config-tschaub": "^1.0.0",
"glob": "~3.2.8",
"rimraf": "~2.2.6",
"bench-it": "~0.1.0"
"mocha": "^2.2.5",
"rimraf": "~2.2.6"
},

@@ -41,0 +42,0 @@ "dependencies": {

@@ -111,3 +111,3 @@ # `mock-fs`

To create a mock filesystem with a directory with the relative path `some/dir` that has a mode of `0755` and a couple child files, you could do something like this:
To create a mock filesystem with a directory with the relative path `some/dir` that has a mode of `0755` and two child files, you could do something like this:
```js

@@ -187,4 +187,10 @@ mock({

### Using with other modules that modify `fs`
When you require `mock-fs`, Node's own `fs` module is patched to allow the binding to the underlying file system to be swapped out. If you require `mock-fs` *before* any other modules that modify `fs` (e.g. `graceful-fs`), the mock should behave as expected.
**Note** `mock-fs` is not compatible with `graceful-fs >= 3.0.0` and therefore not compatible with `gulp >= 3.7.0`.
### `fs` overrides
The following [`fs` functions](http://nodejs.org/api/fs.html) are overridden: `fs.ReadStream`, `fs.Stats`, `fs.WriteStream`, `fs.appendFile`, `fs.appendFileSync`, `fs.chmod`, `fs.chmodSync`, `fs.chown`, `fs.chownSync`, `fs.close`, `fs.closeSync`, `fs.createReadStream`, `fs.createWriteStream`, `fs.exists`, `fs.existsSync`, `fs.fchmod`, `fs.fchmodSync`, `fs.fchown`, `fs.fchownSync`, `fs.fdatasync`, `fs.fdatasyncSync`, `fs.fstat`, `fs.fstatSync`, `fs.fsync`, `fs.fsyncSync`, `fs.ftruncate`, `fs.ftruncateSync`, `fs.futimes`, `fs.futimesSync`, `fs.lchmod`, `fs.lchmodSync`, `fs.lchown`, `fs.lchownSync`, `fs.link`, `fs.linkSync`, `fs.lstatSync`, `fs.lstat`, `fs.mkdir`, `fs.mkdirSync`, `fs.open`, `fs.openSync`, `fs.read`, `fs.readSync`, `fs.readFile`, `fs.readFileSync`, `fs.readdir`, `fs.readdirSync`, `fs.readlink`, `fs.readlinkSync`, `fs.realpath`, `fs.realpathSync`, `fs.rename`, `fs.renameSync`, `fs.rmdir`, `fs.rmdirSync`, `fs.stat`, `fs.statSync`, `fs.symlink`, `fs.symlinkSync`, `fs.truncate`, `fs.truncateSync`, `fs.unlink`, `fs.unlinkSync`, `fs.utimes`, `fs.utimesSync`, `fs.write`, `fs.writeSync`, `fs.writeFile`, and `fs.writeFileSync`.

@@ -191,0 +197,0 @@

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

/** @type {boolean} */
chai.Assertion.includeStack = true;
chai.config.includeStack = true;

@@ -64,3 +64,5 @@

constants.O_EXCL;
default:
throw new Error('Unsupported flag: ' + str);
}
};

@@ -16,2 +16,5 @@ 'use strict';

fs.stat(path.join(dir, item), function(err, stats) {
if (err) {
return callback(err);
}
if (stats && stats.isFile()) {

@@ -18,0 +21,0 @@ ++files;

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

/* eslint-env mocha */
'use strict';

@@ -2,0 +3,0 @@

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

/* eslint-env mocha */
'use strict';

@@ -356,3 +357,3 @@

var binding = new Binding(system);
var fd = binding.open(path.join('mock-dir', 'two.txt'), flags('r+'));
binding.open(path.join('mock-dir', 'two.txt'), flags('r+'));
var file = system.getItem(path.join('mock-dir', 'two.txt'));

@@ -398,3 +399,3 @@ assert.instanceOf(file, File);

var binding = new Binding(system);
var fd = binding.open('new.txt', flags('w'), parseInt('0644', 8));
binding.open('new.txt', flags('w'), parseInt('0644', 8));
var file = system.getItem('new.txt');

@@ -407,3 +408,3 @@ assert.instanceOf(file, File);

var binding = new Binding(system);
var fd = binding.open(path.join('mock-dir', 'two.txt'), flags('w'),
binding.open(path.join('mock-dir', 'two.txt'), flags('w'),
parseInt('0666', 8));

@@ -424,3 +425,3 @@ var file = system.getItem(path.join('mock-dir', 'two.txt'));

var binding = new Binding(system);
var fd = binding.open('new.txt', flags('w+'), parseInt('0644', 8));
binding.open('new.txt', flags('w+'), parseInt('0644', 8));
var file = system.getItem('new.txt');

@@ -434,3 +435,3 @@ assert.instanceOf(file, File);

var binding = new Binding(system);
var fd = binding.open(
binding.open(
path.join('mock-dir', 'one.txt'), flags('w+'), parseInt('0666', 8));

@@ -444,3 +445,3 @@ var file = system.getItem(path.join('mock-dir', 'one.txt'));

var binding = new Binding(system);
var fd = binding.open('new.txt', flags('wx+'), parseInt('0644', 8));
binding.open('new.txt', flags('wx+'), parseInt('0644', 8));
var file = system.getItem('new.txt');

@@ -462,3 +463,3 @@ assert.instanceOf(file, File);

var binding = new Binding(system);
var fd = binding.open('new.txt', flags('a'), parseInt('0666', 8));
binding.open('new.txt', flags('a'), parseInt('0666', 8));
var file = system.getItem('new.txt');

@@ -472,3 +473,3 @@ assert.instanceOf(file, File);

var binding = new Binding(system);
var fd = binding.open(
binding.open(
path.join('mock-dir', 'one.txt'), flags('a'), parseInt('0666', 8));

@@ -482,3 +483,3 @@ var file = system.getItem(path.join('mock-dir', 'one.txt'));

var binding = new Binding(system);
var fd = binding.open('new.txt', flags('ax'), parseInt('0664', 8));
binding.open('new.txt', flags('ax'), parseInt('0664', 8));
var file = system.getItem('new.txt');

@@ -500,3 +501,3 @@ assert.instanceOf(file, File);

var binding = new Binding(system);
var fd = binding.open('new.txt', flags('a+'), parseInt('0666', 8));
binding.open('new.txt', flags('a+'), parseInt('0666', 8));
var file = system.getItem('new.txt');

@@ -510,3 +511,3 @@ assert.instanceOf(file, File);

var binding = new Binding(system);
var fd = binding.open(
binding.open(
path.join('mock-dir', 'one.txt'), flags('a+'), parseInt('0666', 8));

@@ -520,3 +521,3 @@ var file = system.getItem(path.join('mock-dir', 'two.txt'));

var binding = new Binding(system);
var fd = binding.open('new.txt', flags('ax+'), parseInt('0666', 8));
binding.open('new.txt', flags('ax+'), parseInt('0666', 8));
var file = system.getItem('new.txt');

@@ -678,3 +679,3 @@ assert.instanceOf(file, File);

var newPath = path.join('mock-dir', 'empty', 'new.txt');
binding.rename(oldPath, newPath, function(err) {
binding.rename(oldPath, newPath, function(_) {
var stats = binding.stat(newPath);

@@ -711,3 +712,3 @@ assert.equal(stats.mode & constants.S_IFMT, constants.S_IFREG);

var newPath = path.join('mock-dir', 'new');
binding.rename(oldPath, newPath, function(err) {
binding.rename(oldPath, newPath, function(_) {
var stats = binding.stat(newPath);

@@ -714,0 +715,0 @@ assert.equal(stats.mode & constants.S_IFMT, constants.S_IFDIR);

@@ -0,5 +1,4 @@

/* eslint-env mocha */
'use strict';
var path = require('path');
var FileDescriptor = require('../../lib/descriptor');

@@ -6,0 +5,0 @@ var helper = require('../helper');

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

/* eslint-env mocha */
'use strict';

@@ -2,0 +3,0 @@

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

/* eslint-env mocha */
'use strict';

@@ -2,0 +3,0 @@

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

/* eslint-env mocha */
'use strict';

@@ -2,0 +3,0 @@

@@ -0,5 +1,4 @@

/* eslint-env mocha */
'use strict';
var path = require('path');
var Item = require('../../lib/item');

@@ -6,0 +5,0 @@ var assert = require('../helper').assert;

Sorry, the diff of this file is not supported yet

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