tschaub / mock-fs

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

fs.readdir doesn't fail if directory cannot be accessed #294

Closed warpdesign closed 4 years ago

warpdesign commented 4 years ago

I created a directory with mode 0o000. When using node's fs.readdir, an error with code EACCES is thrown but when mocked, it succeeds with ['0'] as result.

I am using OSX Catalina.

Code to reproduce the problem:

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

console.log('');

try {
  fs.rmdirSync('/tmp/denied');
} catch(e) {

}

fs.mkdirSync('/tmp/denied', 0o000);

const mockFs = () => {
  mock({
    ['/tmp/denied']: mock.directory({
      mode: 0o000,
      items: [
        { file: 'file' }
      ]
    })
  });
}

const read = async (shouldMock) => { 
  if (shouldMock) {
    console.log('mocked fs');
    console.log('=========');
      mockFs();
  } else {
    console.log('native fs');
    console.log('=========');
  }

  try {
    const files = await util.promisify(fs.readdir)('/tmp/denied');
    console.log('success', files);
  } catch(err) {
    console.log('error', err.code);
  }
}

async function test() {
  await read(false);
  console.log('');
  await read(true);
}

test();

When running it on a terminal, I get the following output:

native fs
========
error EACCES

mocked fs
========
success [ '0' ]

Node.js version: 12.14.1 mock-fs version: 4.11.0

tschaub commented 4 years ago

Fix published in mock-fs@4.12.0. Thanks, @warpdesign.