Skip to content

Commit

Permalink
Merge pull request #194 from ciaranj/expose_root_of_mock_filesystem
Browse files Browse the repository at this point in the history
Exposes the root mocked filesytem if the mock is currently applied.
  • Loading branch information
tschaub authored Feb 5, 2017
2 parents b3d19b3 + 6a06eda commit 10f98fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var realProcessProps = {
chdir: process.chdir
};


function overrideBinding(binding) {
for (var key in binding) {
if (typeof binding[key] === 'function') {
Expand All @@ -30,9 +29,16 @@ function overrideProcess(cwd, chdir) {
}

function restoreBinding() {
for (var key in realBindingProps) {
var key;
for (key in realBindingProps) {
realBinding[key] = realBindingProps[key];
}
// Delete excess keys that came in when the binding was originally applied.
for (key in realBinding) {
if (typeof realBindingProps[key] === 'undefined') {
delete realBinding[key];
}
}
}

function restoreProcess() {
Expand All @@ -53,6 +59,7 @@ function restoreProcess() {
var exports = module.exports = function mock(config, options) {
var system = FileSystem.create(config, options);
var binding = new Binding(system);

overrideBinding(binding);

var currentPath = process.cwd();
Expand All @@ -69,6 +76,17 @@ var exports = module.exports = function mock(config, options) {
);
};

/**
* Get hold of the mocked filesystem's 'root'
* If fs hasn't currently been replaced, this will return an empty object
*/
exports.getMockRoot = function() {
if (typeof realBinding.getSystem === 'undefined') {
return {};
} else {
return realBinding.getSystem().getRoot();
}
};

/**
* Restore the fs bindings for the real file system.
Expand Down
8 changes: 8 additions & 0 deletions test/lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ describe('The API', function() {
mock.restore();
});

it('provides direct access to the internal filesystem object', function() {
mock();
var root = mock.getMockRoot();
assert.notDeepEqual(root, {});
mock.restore();
assert.deepEqual(mock.getMockRoot(), {});
});

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

Expand Down

0 comments on commit 10f98fc

Please sign in to comment.