Comparing version 4.2.0 to 4.3.0
# Change Log | ||
## 4.3.0 | ||
* Add support for fs.mkdtemp() and fs.mkdtempSync (see [#207][#207]) | ||
## 4.2.0 | ||
@@ -186,1 +190,2 @@ | ||
[#198]: https://github.com/tschaub/mock-fs/pull/198 | ||
[#207]: https://github.com/tschaub/mock-fs/pull/207 |
@@ -714,3 +714,62 @@ 'use strict'; | ||
var PATH_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | ||
var MAX_ATTEMPTS = 62 * 62 * 62; | ||
/** | ||
* Create a directory based on a template. | ||
* See http://web.mit.edu/freebsd/head/lib/libc/stdio/mktemp.c | ||
* @param {string} template Path template (trailing Xs will be replaced). | ||
* @param {string} encoding The encoding ('utf-8' or 'buffer'). | ||
* @param {function(Error, string)} callback Optional callback. | ||
*/ | ||
Binding.prototype.mkdtemp = function(prefix, encoding, callback) { | ||
if (encoding && typeof encoding !== 'string') { | ||
callback = encoding; | ||
encoding = 'utf-8'; | ||
} | ||
return maybeCallback(callback, this, function() { | ||
prefix = prefix.replace(/X{0,6}$/, 'XXXXXX'); | ||
var parentPath = path.dirname(prefix); | ||
var parent = this._system.getItem(parentPath); | ||
if (!parent) { | ||
throw new FSError('ENOENT', prefix); | ||
} | ||
if (!(parent instanceof Directory)) { | ||
throw new FSError('ENOTDIR', prefix); | ||
} | ||
this.access(parentPath, parseInt('0002', 8)); | ||
var template = path.basename(prefix); | ||
var unique = false; | ||
var count = 0; | ||
var name; | ||
while (!unique && count < MAX_ATTEMPTS) { | ||
var position = template.length - 1; | ||
var replacement = ''; | ||
while (template.charAt(position) === 'X') { | ||
replacement += PATH_CHARS.charAt(Math.floor(PATH_CHARS.length * Math.random())); | ||
position -= 1; | ||
} | ||
var candidate = template.slice(0, position + 1) + replacement; | ||
if (!parent.getItem(candidate)) { | ||
name = candidate; | ||
unique = true; | ||
} | ||
count += 1; | ||
} | ||
if (!name) { | ||
throw new FSError('EEXIST', prefix); | ||
} | ||
var dir = new Directory(); | ||
parent.addItem(name, dir); | ||
var uniquePath = path.join(parentPath, name); | ||
if (encoding === 'buffer') { | ||
uniquePath = new Buffer(uniquePath); | ||
} | ||
return uniquePath; | ||
}); | ||
}; | ||
/** | ||
* Truncate a file. | ||
@@ -717,0 +776,0 @@ * @param {number} fd File descriptor. |
{ | ||
"name": "mock-fs", | ||
"description": "A configurable mock file system. You know, for testing.", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"main": "lib/index.js", | ||
@@ -38,4 +38,5 @@ "homepage": "https://github.com/tschaub/mock-fs", | ||
"mocha": "3.1.2", | ||
"rimraf": "2.5.4" | ||
"rimraf": "2.5.4", | ||
"semver": "^5.3.0" | ||
} | ||
} |
@@ -200,4 +200,2 @@ # `mock-fs` | ||
### 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. | ||
@@ -207,6 +205,2 @@ | ||
### `fs` overrides | ||
The following [`fs` functions](http://nodejs.org/api/fs.html) are overridden: `fs.ReadStream`, `fs.Stats`, `fs.WriteStream`, `fs.access`, `fs.accessSync`, `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`. | ||
Mock `fs.Stats` objects have the following properties: `dev`, `ino`, `nlink`, `mode`, `size`, `rdev`, `blksize`, `blocks`, `atime`, `ctime`, `mtime`, `birthtime`, `uid`, and `gid`. In addition, all of the `is*()` method are provided (e.g. `isDirectory()`, `isFile()`, et al.). | ||
@@ -216,6 +210,4 @@ | ||
The following `fs` functions are *not* currently mocked (if your tests use these, they will work against the real file system): `fs.FSWatcher`, `fs.unwatchFile`, `fs.watch`, and `fs.watchFile`. Pull requests welcome. | ||
Tested on Linux, OSX, and Windows using Node 0.10 through 6.x. Check the tickets for a list of [known issues](https://github.com/tschaub/mock-fs/issues). | ||
[](https://travis-ci.org/tschaub/mock-fs) |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var constants = require('constants'); | ||
var semver = require('semver'); | ||
@@ -18,2 +19,9 @@ | ||
exports.inVersion = function(range) { | ||
if (semver.satisfies(process.version, range)) { | ||
return {it: it, describe: describe}; | ||
} else { | ||
return {it: xit, describe: xdescribe}; | ||
} | ||
}; | ||
@@ -20,0 +28,0 @@ /** |
@@ -1104,2 +1104,31 @@ 'use strict'; | ||
describe('#mkdtemp()', function() { | ||
it('creates a new directory', function() { | ||
var binding = new Binding(system); | ||
var template = path.join('mock-dir', 'fooXXXXXX'); | ||
var dirPath = binding.mkdtemp(template); | ||
assert.notEqual(template, dirPath); | ||
var dir = system.getItem(dirPath); | ||
assert.instanceOf(dir, Directory); | ||
}); | ||
it('fails if parent does not exist', function() { | ||
var binding = new Binding(system); | ||
var dirPath = path.join('bogus', 'pathXXXXXX'); | ||
assert.throws(function() { | ||
binding.mkdtemp(dirPath); | ||
}); | ||
}); | ||
it('fails if file exists', function() { | ||
var binding = new Binding(system); | ||
var dirPath = path.join('mock-dir', 'one.txt', 'XXXXXX'); | ||
assert.throws(function() { | ||
binding.mkdtemp(dirPath); | ||
}); | ||
}); | ||
}); | ||
describe('#rmdir()', function() { | ||
@@ -1106,0 +1135,0 @@ |
Sorry, the diff of this file is too big to display
269623
7811
7
211