tschaub / mock-fs

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

Using complex paths as keys fails when under absolute parent path #354

Open BadIdeaException opened 2 years ago

BadIdeaException commented 2 years ago

The following:

mockfs({
    'foo':
        'bar/baz': ''
    }
});

correctly produces a folder called foo containing folder called bar containing a file called baz in the mocked file system. However, when using an absolute path as the parent

mockfs({
    '/foo': // Notice the leading slash
        'bar/baz': ''
    }
});

I get a (top-level) folder /foo, that contains a file called bar/baz. I would have expected a folder named barwith a file named baz.

It appears that key names are not getting correctly parsed when inside an absolute parent folder.

3cp commented 2 years ago
const mockfs = require('mock-fs');
const fs = require('fs');

mockfs({
    'foo': {
        'bar/baz' : 'hello'
    }
});

const c = fs.readdirSync('foo');
console.log(c);

It outputs

[ 'bar/baz' ]

Not [ 'bar' ].

So the behaviour is same for 'foo' and '/foo'. You probably got the comparison wrong.

I checked our code, currently we only support first level deep dir name like mockfs({ 'a/b/c' : {...}}), but not on any nested levels.

But I agree it's a good idea to do the same thing on all levels.