tschaub / mock-fs

Configurable mock for the fs module
https://npmjs.org/package/mock-fs
Other
908 stars 86 forks source link

EBADF reading directory with readFileSync() #285

Closed mattdeluco closed 4 years ago

mattdeluco commented 4 years ago

The attached mocha test will fail with Error: EBADF: bad file descriptor, read.

While searching the issues I noticed some relating to graceful-fs - while it's technically in my node_modules dependency tree, I don't believe it's loaded during the test. I think it's only used for the vscode eslint extension.

This is on macOS 10.15.2, Node 12.14.0, mock-fs 4.10.4.

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

describe('Test Suite', () => {
  before(() => {
    mockFS({
      root: {}
    });
  });

  after(() => {
    mockFS.restore();
  });

  it('should mock the file system', () => {
    let foo = fs.readFileSync('root');
  });
});
mattdeluco commented 4 years ago

Sorry, reading through the NodeJS API docs for fs I noticed that readFile() should return an error if the file is a directory. Which is odd, because I've got code that seems to work when I use readFileSync() to loop over directory contents (which is why I posted here before checking the Node API.)

3cp commented 4 years ago

There is still a small mismatch, Nodejs fails EISDIR not EBADF when reading dir, at least for nodejs v12. I will follow up.