Comparing version 3.5.0 to 3.6.0
# Change Log | ||
## 3.6.0 | ||
* Add `createCwd` and `createTmp` options to control the creation of `process.cwd()` and `os.tmpdir()` directories in the mocked filesystem (see [#72][#72]). | ||
* Update Travis and AppVeyor configurations (see [#73][#73]) | ||
* Remove unused dev dependency (see [#75][#75]) | ||
## 3.5.0 | ||
* Support for Node 5.x (thanks @tmcw, see[#69][#69]). | ||
* Support for Node 5.x (thanks @tmcw, see [#69][#69]). | ||
## 3.4.0 | ||
* Support for Node 4.x (thanks @AlexMeah, see[#65][#65]). | ||
* Support for Node 4.x (thanks @AlexMeah, see [#65][#65]). | ||
## 3.3.0 | ||
* Traverse symlinks recursively (thanks @caitp, see[#57][#57]). | ||
* Traverse symlinks recursively (thanks @caitp, see [#57][#57]). | ||
* Upgrade to rewire@2.3.4 (thanks @mbarlock, see [#60][#60]). | ||
@@ -102,1 +108,4 @@ | ||
[#69]: https://github.com/tschaub/mock-fs/pull/69 | ||
[#72]: https://github.com/tschaub/mock-fs/pull/72 | ||
[#73]: https://github.com/tschaub/mock-fs/pull/73 | ||
[#75]: https://github.com/tschaub/mock-fs/pull/75 |
@@ -33,10 +33,27 @@ 'use strict'; | ||
* Create a new file system. | ||
* @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`). | ||
* @constructor | ||
*/ | ||
function FileSystem() { | ||
function FileSystem(options) { | ||
options = options || {}; | ||
var createCwd = 'createCwd' in options ? options.createCwd : true; | ||
var createTmp = 'createTmp' in options ? options.createTmp : true; | ||
var root = new Directory(); | ||
// populate with default directories | ||
var defaults = [os.tmpdir && os.tmpdir() || os.tmpDir(), process.cwd()]; | ||
var defaults = []; | ||
if (createCwd) { | ||
defaults.push(process.cwd()); | ||
} | ||
if (createTmp) { | ||
defaults.push(os.tmpdir && os.tmpdir() || os.tmpDir()); | ||
} | ||
defaults.forEach(function(dir) { | ||
@@ -145,6 +162,11 @@ var parts = getPathParts(dir); | ||
* @param {Object} paths Config object. | ||
* @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 {FileSystem} Mock file system. | ||
*/ | ||
FileSystem.create = function(paths) { | ||
var system = new FileSystem(); | ||
FileSystem.create = function(paths, options) { | ||
var system = new FileSystem(options); | ||
@@ -151,0 +173,0 @@ for (var filepath in paths) { |
@@ -74,5 +74,10 @@ 'use strict'; | ||
* @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); | ||
@@ -108,6 +113,11 @@ setBinding(binding, binding.Stats); | ||
* @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); | ||
@@ -114,0 +124,0 @@ |
{ | ||
"name": "mock-fs", | ||
"description": "A configurable mock file system. You know, for testing.", | ||
"version": "3.5.0", | ||
"version": "3.6.0", | ||
"main": "lib/index.js", | ||
@@ -37,3 +37,2 @@ "homepage": "https://github.com/tschaub/mock-fs", | ||
"eslint-config-tschaub": "^2.0.0", | ||
"glob": "^5.0.15", | ||
"mocha": "^2.3.3", | ||
@@ -40,0 +39,0 @@ "rimraf": "^2.4.3" |
@@ -31,7 +31,7 @@ # `mock-fs` | ||
### <a id='mockconfig'>`mock(config)`</a> | ||
### <a id='mockconfigoptions'>`mock(config, options)`</a> | ||
Configure the `fs` module so it is backed by an in-memory file system. | ||
Calling `mock` sets up a mock file system with at least two directories: `process.cwd()` and `os.tmpdir()` (or `os.tmpDir()` for older Node). When called with no arguments, just these two directories are created. When called with a `config` object, additional files, directories, and symlinks are created. | ||
Calling `mock` sets up a mock file system with two directories by default: `process.cwd()` and `os.tmpdir()` (or `os.tmpDir()` for older Node). When called with no arguments, just these two directories are created. When called with a `config` object, additional files, directories, and symlinks are created. To avoid creating a directory for `process.cwd()` and `os.tmpdir()`, see the [`options`](#options) below. | ||
@@ -42,2 +42,9 @@ Property names of the `config` object are interpreted as relative paths to resources (relative from `process.cwd()`). Property values of the `config` object are interpreted as content or configuration for the generated resources. | ||
### <a id='options'>`options`</a> | ||
The second (optional) argument may include the properties below. | ||
* `createCwd` - `boolean` Create a directory for `process.cwd()`. This is `true` by default. | ||
* `createTmp` - `boolean` Create a directory for `os.tmpdir()`. This is `true` by default. | ||
### Creating files | ||
@@ -174,5 +181,5 @@ | ||
### <a id='mockfsconfig'>`mock.fs(config)`</a> | ||
### <a id='mockfsconfigoptions'>`mock.fs(config, options)`</a> | ||
Calling `mock()` modifies Node's built-in `fs` module. This is useful when you want to test with a mock file system. If for some reason you want to work with the real file system and an in-memory version at the same time, you can call the `mock.fs()` function. This takes the same `config` object [described above](#mockconfig) and sets up a in-memory file system. Instead of modifying the binding for the built-in `fs` module (as is done when calling `mock(config)`), the `mock.fs(config)` function returns an object with the same interface as the `fs` module, but backed by your mock file system. | ||
Calling `mock()` modifies Node's built-in `fs` module. This is useful when you want to test with a mock file system. If for some reason you want to work with the real file system and an in-memory version at the same time, you can call the `mock.fs()` function. This takes the same `config` and `options` objects [described above](#mockconfigoptions) and sets up a in-memory file system. Instead of modifying the binding for the built-in `fs` module (as is done when calling `mock(config)`), the `mock.fs(config)` function returns an object with the same interface as the `fs` module, but backed by your mock file system. | ||
@@ -179,0 +186,0 @@ ## Install |
/* eslint-env mocha */ | ||
'use strict'; | ||
var os = require('os'); | ||
var path = require('path'); | ||
@@ -21,2 +22,20 @@ | ||
it('accepts a createCwd option', function() { | ||
var cwd = process.cwd(); | ||
var withCwd = new FileSystem({createCwd: true}); | ||
var withoutCwd = new FileSystem({createCwd: false}); | ||
assert.instanceOf(withCwd.getItem(cwd), Directory); | ||
assert.isNull(withoutCwd.getItem(cwd)); | ||
}); | ||
it('accepts a createTmp option', function() { | ||
var tmp = os.tmpdir ? os.tmpdir() : os.tmpDir(); | ||
var withTmp = new FileSystem({createTmp: true}); | ||
var withoutTmp = new FileSystem({createTmp: false}); | ||
assert.instanceOf(withTmp.getItem(tmp), Directory); | ||
assert.isNull(withoutTmp.getItem(tmp)); | ||
}); | ||
}); | ||
@@ -164,2 +183,18 @@ | ||
it('passes options to the FileSystem constructor', function() { | ||
var cwd = process.cwd(); | ||
var tmp = os.tmpdir ? os.tmpdir() : os.tmpDir(); | ||
var withoutCwd = FileSystem.create({}, {createCwd: false}); | ||
var withoutTmp = FileSystem.create({}, {createTmp: false}); | ||
assert.isNull(withoutCwd.getItem(cwd)); | ||
assert.instanceOf(withoutCwd.getItem(tmp), Directory); | ||
assert.isNull(withoutTmp.getItem(tmp)); | ||
assert.instanceOf(withoutTmp.getItem(cwd), Directory); | ||
}); | ||
it('accepts file factory', function() { | ||
@@ -166,0 +201,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
695101
6
21814
212