Skip to content

Throw error when a directory is opened - #242

Merged
tschaub merged 2 commits into
tschaub:masterfrom
maxwellgerber:patch/open-directory-error
Aug 9, 2018
Merged

Throw error when a directory is opened#242
tschaub merged 2 commits into
tschaub:masterfrom
maxwellgerber:patch/open-directory-error

Conversation

@maxwellgerber

Copy link
Copy Markdown

We should throw an error when someone attempts to open a mocked directory. When we try to write to a directory currently, we don't do this properly and it causes a confusing error to be thrown.

Node Version: v8.11.3

const mockFS = require('mock-fs');
const fs = require('fs');

mockFS({
   '/my/dir': {FOO: ''}
});

fs.writeFileSync('/my/dir', 'bar');

Results in

TypeError: item.setContent is not a function
    at Binding.<anonymous> (.../node_modules/mock-fs/lib/binding.js:442:12)
    at maybeCallback (.../node_modules/mock-fs/lib/binding.js:53:17)
    at Binding.open (.../node_modules/mock-fs/lib/binding.js:402:10)
    ...

Because we try to zero out the file content here, but there is no content to modify on a directory.

With this patch the same code results in

Error: EISDIR, illegal operation on a directory '/my/dir'
    at Binding.<anonymous> (.../node_modules/mock-fs/lib/binding.js:464:13)
    at maybeCallback (.../node_modules/mock-fs/lib/binding.js:61:17)
    at Binding.open (.../node_modules/mock-fs/lib/binding.js:424:10)

@tschaub

tschaub commented Aug 6, 2018

Copy link
Copy Markdown
Owner

Thanks @maxwellgerber. I noticed the same thing when writing tests for #243. I hadn't seen this PR yet and implemented a (less complete) fix there. I'll pull in this change when I get a chance. If you're able to add additional tests, that would be great.

@tschaub
tschaub merged commit 076fca8 into tschaub:master Aug 9, 2018
@tschaub

tschaub commented Aug 9, 2018

Copy link
Copy Markdown
Owner

Thanks for the contribution @maxwellgerber!

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

Successfully merging this pull request may close these issues.

2 participants