mockfs
Work in progress
MockFS - Mocking FS module implementation for testing purpouses.
Basic idea is to declare file system contents via JSON spec, mount it, and use through real fs.*
functions like an ordinary one.
var fs = require('fs'),
MockFS = require('mockfs'),
spec, mfs, fd;
spec = {
time: 'Tue May 07 2013 17:09:57 GMT+0400'
ctime: new Date(),
items: {
'file-buffer': new Buffer('qwerty'),
'file-base64': new Buffer('cXdlcnR5', 'base64'),
'file-string': 'qwerty',
'file-alt': {
uid: 'johndoe',
gid: 300,
mode: 0766,
atime: new Date(),
mtime: 1000255364,
ctime: "-500"
content: 'asobject'
},
'dir': {
atime: 'Tue May 07 2013 17:09:57 GMT+0400'
mtime: "+500",
items: {
'file-in-dir': 'inside directory'
}
}
};
mfs = new MockFS(spec);
mfs.mount('/mnt/mock');
fs.existsSync('/mnt/mock/file-buffer');
fs.readFileSync('/mnt/mock/file-string').toString();
fs.readFile('/mnt/mock/dir/file-in-dir', function(e, r){
Buffer.isBuffer(r);
r.toString();
});
fs.open('/mnt/mock/file-base64', 'r', function(e, fd){
if(fd) {
var buf = new Buffer(100);
fs.read(fd, buf, 0, 100, null, function(e, bytesRead){
console.log(bytesRead);
console.log(buf.toString('utf8', 0, bytesRead));
fs.closeSync(fd);
});
}
});
mfs.umount('/mnt/mock');
fs.existsSync('/mnt/mock/file-buffer');
Implemented by wrapping bundled fs
module's basic functions (file descriptors handling, stat, rename/delete files/directories).
So, functions as createReadStream
, appendFileSync
and so are supported "out of the box" without any wrapping.
Currently, NodeJS v0.8+ is supported.
TODO
- Support legacy interfaces
- Access rights check (read/write/search permissions)
- *chown(Sync), *chmod(Sync) functions
- Links support
- (un)watch(File) support
mirror
utility to create MockFS specs from real file systems- Integrate with some date parsing library for convinient atime/ctime/mtime specification
Roadmap
v0.2
- Access rights check (read/write/search permissions)
- *chown(Sync), *chmod(Sync) functions
v0.3
- Links support
- (un)watch(File) support
v1.0
- Support legacy interfaces
- Pass NodeJS test suite
Similar libraries