Skip to content

Commit f77fa8b

Browse files
committed
fix: 🐛 throw when creating root directory
closes #325
1 parent 01a2f35 commit f77fa8b

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`mkdirSync throws when creating root directory 1`] = `"EISDIR: illegal operation on a directory, mkdir"`;
4+
5+
exports[`mkdirSync throws when re-creating existing directory 1`] = `"EEXIST: file already exists, mkdir '/new-dir'"`;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { create } from '../util';
2+
3+
describe('mkdirSync', () => {
4+
it('can create a directory', () => {
5+
const vol = create();
6+
7+
vol.mkdirSync('/new-dir');
8+
const stat = vol.statSync('/new-dir');
9+
10+
expect(stat.isDirectory()).toBe(true);
11+
});
12+
13+
it('root directory is directory', () => {
14+
const vol = create();
15+
const stat = vol.statSync('/');
16+
17+
expect(stat.isDirectory()).toBe(true);
18+
});
19+
20+
it('throws when re-creating existing directory', () => {
21+
const vol = create();
22+
23+
vol.mkdirSync('/new-dir');
24+
25+
let error;
26+
try {
27+
vol.mkdirSync('/new-dir');
28+
} catch (err) {
29+
error = err;
30+
}
31+
32+
expect(error).toBeInstanceOf(Error);
33+
expect(error.message).toMatchSnapshot();
34+
});
35+
36+
/**
37+
* See issue #325
38+
* https://github.com/streamich/memfs/issues/325
39+
*/
40+
it('throws when creating root directory', () => {
41+
const vol = create();
42+
43+
let error;
44+
try {
45+
vol.mkdirSync('/');
46+
} catch (err) {
47+
error = err;
48+
}
49+
50+
expect(error).toBeInstanceOf(Error);
51+
expect(error.message).toMatchSnapshot();
52+
});
53+
});

src/volume.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,12 @@ export class Volume {
17681768

17691769
private mkdirBase(filename: string, modeNum: number) {
17701770
const steps = filenameToSteps(filename);
1771+
1772+
// This will throw if user tries to create root dir `fs.mkdirSync('/')`.
1773+
if (!steps.length) {
1774+
throwError(EISDIR, 'mkdir');
1775+
}
1776+
17711777
const dir = this.getLinkParentAsDirOrThrow(filename, 'mkdir');
17721778

17731779
// Check path already exists.

0 commit comments

Comments
 (0)