Skip to content

Commit

Permalink
Expose the createCwd and createTmp options
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Dec 20, 2015
1 parent 150e5b6 commit fd9ab8a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
18 changes: 14 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ function setProcess(cwd, chdir) {
/**
* Swap out the fs bindings for a mock file system.
* @param {Object} config Mock file system configuration.
* @param {Object} options Any filesystem options.
* @param {boolean} options.createCwd Create a directory for `process.cwd()`
* (defaults to `true`).
* @param {boolean} options.createTmp Create a directory for `os.tmpdir()`
* (defaults to `true`).
*/
var exports = module.exports = function mock(config) {
var system = FileSystem.create(config);
var exports = module.exports = function mock(config, options) {
var system = FileSystem.create(config, options);
var binding = new Binding(system);
setBinding(binding, binding.Stats);

Expand Down Expand Up @@ -104,10 +109,15 @@ exports.restore = function() {
/**
* Create a mock fs module based on the given file system configuration.
* @param {Object} config File system configuration.
* @param {Object} options Any filesystem options.
* @param {boolean} options.createCwd Create a directory for `process.cwd()`
* (defaults to `true`).
* @param {boolean} options.createTmp Create a directory for `os.tmpdir()`
* (defaults to `true`).
* @return {Object} A fs module with a mock file system.
*/
exports.fs = function(config) {
var system = FileSystem.create(config);
exports.fs = function(config, options) {
var system = FileSystem.create(config, options);
var binding = new Binding(system);

// inject the mock binding
Expand Down
42 changes: 38 additions & 4 deletions test/lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ describe('The API', function() {
mock.restore();
});

});

describe('mock()', function() {

it('creates process.cwd() and os.tmpdir() by default', function() {
mock();

Expand All @@ -43,6 +39,30 @@ describe('The API', function() {
mock.restore();
});

it('passes the createCwd option to the FileSystem constructor', function() {
mock({}, {createCwd: false});

assert.isFalse(fs.existsSync(process.cwd()));

mock.restore();
});

it('passes the createTmp option to the FileSystem constructor', function() {
mock({}, {createTmp: false});

var tmp;
if (os.tmpdir) {
tmp = os.tmpdir();
} else if (os.tmpDir) {
tmp = os.tmpDir();
}
if (tmp) {
assert.isFalse(fs.existsSync(tmp));
}

mock.restore();
});

});

describe('mock.restore()', function() {
Expand Down Expand Up @@ -179,6 +199,20 @@ describe('The API', function() {

});

it('passes options to the FileSystem constructor', function() {

var mockFs = mock.fs({
'/path/to/file.txt': 'file content'
}, {
createCwd: false,
createTmp: false
});

assert.isTrue(mockFs.existsSync('/path/to/file.txt'));
assert.deepEqual(mockFs.readdirSync('/'), ['path']);

});

it('accepts an arbitrary nesting of files and directories', function() {

var mockFs = mock.fs({
Expand Down

0 comments on commit fd9ab8a

Please sign in to comment.