node.js fs mixins for sinon-doublist: stubFile(), stubTree()
JavaScript
Permalink
Failed to load latest commit information.
config/grunt chore(component): migrate to NPM-only deps Jun 11, 2015
docs feat(FileStub#readdir): Accept a string argument Jun 11, 2013
lib/sinon-doublist-fs fix(lib,test): regressions Jun 11, 2015
test fix(lib,test): regressions Jun 11, 2015
.gitignore chore(component): remove artifacts Jun 11, 2015
.travis.yml fix(travis): use specific/known-good NPM packages Jun 11, 2015
CHANGELOG.md docs(changelog): add 0.6.0 notes Jun 11, 2015
Gruntfile.js style(eslint): migration Jun 11, 2015
LICENSE Init repo Mar 20, 2013
README.md fix(test): co-fs test case did not run Apr 30, 2014
index.js
package.json chore(npm): upgrade dev deps Jun 11, 2015

README.md

sinon-doublist-fs

node.js fs mixins for sinon-doublist: stubFile(), stubTree()

Build Status

Examples

All sinon sandboxes except fake timers will be created by the both mixin approaches below. (sinon docs)

Mixin with auto-sandboxing (recommended)

sinonDoublist(sinon, 'mocha');
sinonDoublistFs('mocha');

describe('myFunction', function() {
  it('should do something', function() {
    // this.stubFile()
    // this.stubTree()
  });
});

Mixin w/ manual sandboxing

describe('myFunction', function() {
  beforeEach(function() {
    sinonDoublist(sinon, this);
    sinonDoublistFs(this);
  });

  afterEach(function() {
    this.sandbox.restore();
  });

  it('should do something', function() {
    // this.spyFile()
    // this.stubTree()
  });
});

Fake a large file

describe('#validate()', function() {
 it('should detect file that is too large', function() {
    var filename = '/path/to/file';
    this.stubFile(filename).stat('size', 1024 * 1024 * 1024).make();
    var myLib = new MyLib(filename);
    myLib.validate().should.equal(false);
  });
});

Fake a file tree with leaf attributes

/**
 * /root/a
 * /root/a/b
 * /root/a/b2
 * /root/a2
 * /root/a3
 * /root/a3/b4
 * /root/a3/b4/c
 */
this.stubFile('/root').readdir([
  this.stubFile('/root/a').readdir([
    this.stubFile('/root/a/b').size(100),
    this.stubFile('/root/a/b2').size(50)
  ]),
  this.stubFile('/root/a2').size(10),
  this.stubFile('/root/a3').readdir([
    this.stubFile('/root/a3/b4').readdir([
      this.stubFile('/root/a3/b4/c').size(20)
    ])
  ])
]).make();

Fake a file tree from a sparse path list

Creates the same hierarchy as the stubFile() example above. However, ancestor directories are stubbed automatically.

this.stubTree([
  '/root/a/b2',
  '/root/a2',
  '/root/a3/b4/c'
]);

fs coverage

File stubs created by stubFile() / stubTree(), and configured via .stat() and others, will be reflected/modifiable by:

  • fs.writeFile*
  • fs.readFile*
  • fs.exists*
  • fs.readdir*
  • fs.stat* / fs.lstat*
    • Including isFile() / isDirectory() responses
  • fs.unlink*
  • fs.renameSync

If a file stub does not exist for a given path, we fallback to the real fs method. To override this behavior:

  • sinonDoublistFs.realFsFallback = 0
    • Do nothing (async methods will hang).
  • sinonDoublistFs.realFsFallback = 2
    • Throw an Error, ex. existsSync, no such file stub '/path/to/file'

co-fs compatibility

co-fs wrappers just need to be added after sinon-doublist-fs stubbing. See test/lib/co-fs.js.

Installation

NPM

npm install sinon-doublist-fs

API

Documentation

License

MIT

Tests

npm test