Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposes the root mocked filesytem if the mock is currently applied. (Fixes #90) #194

Merged
merged 1 commit into from
Feb 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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