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

Default directory population #71

Closed
gustavnikolaj opened this issue Dec 19, 2015 · 2 comments
Closed

Default directory population #71

gustavnikolaj opened this issue Dec 19, 2015 · 2 comments

Comments

@gustavnikolaj
Copy link

Hi @tschaub

Thanks for your awesome work on mock-fs.

I have used it to create a plugin for the unexpected assertion framework. It is still not ready for primetime, but I'm using it a few projects myself, and it's already proven really useful.

One of unexpected's greatest strengths are it's async assertions, which in combination with the plugin mentioned before, allows me to write assertions like this:

it('should read from the mock-fs', function () {
    return expect(function (cb) {
        fs.readFile('/data/foobar.txt', cb);
    }, 'with fs mocked out', {
        '/data': {
            'foobar.txt': 'Hello world',
        }
    }, 'to call the callback without error').then(function (fileContent) {
        return expect(fileContent, 'to equal', 'Hello world');
    });
});

I'm using mock-fs to create the filesystem and mountfs, to mount the filesystem on a path specified in the object given to the assertion.

The reason why it's preferable to me, to mount the mocks as subtrees of the filesystem instead of in the root is, that require uses fs to load modules. If I'm testing a module that does lazy loading of modules, it would not work, unless I also added those files to the file system.

Also, wrapping mock-fs into an assertion like this, makes it so that the life time of the mock is tightly coupled to the lifetime of the assertion.

My problem is, that mock-fs populates the mocked fs with some defualt folders.

// populate with default directories
var defaults = [os.tmpdir && os.tmpdir() || os.tmpDir(), process.cwd()];
defaults.forEach(function(dir) {
var parts = getPathParts(dir);
var directory = root;
var i, ii, name, candidate;
for (i = 0, ii = parts.length; i < ii; ++i) {
name = parts[i];
candidate = directory.getItem(name);
if (!candidate) {
directory = directory.addItem(name, new Directory());
} else if (candidate instanceof Directory) {
directory = candidate;
} else {
throw new Error('Failed to create directory: ' + dir);
}
}
});

I would like this to be configurable. If you do an fs.readdir on /data in the above example, you would not just get foobar.txt but also home and tmp (assuming that you are working on a linux machine and you run your tests from somewhere in the /home folder).

It is a very sensible default for all cases but this specific one where it's mounted on a real fs. :-)

@tschaub
Copy link
Owner

tschaub commented Dec 20, 2015

Thanks for the suggestion @gustavnikolaj. I agree it makes sense to have the default directory population be configurable.

Let me know if you are able to test out the changes in #72 or if this looks like it would work for you.

Unexpected and your plugin look nice.

@gustavnikolaj
Copy link
Author

Thanks a lot for your quick response! I have tested it out and it works fine for me :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants