Skip to content

Commit

Permalink
fix: 🐛 throw when creating root directory
Browse files Browse the repository at this point in the history
closes #325
  • Loading branch information
streamich committed Feb 9, 2019
1 parent 01a2f35 commit f77fa8b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/__tests__/volume/__snapshots__/mkdirSync.test.ts.snap
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`mkdirSync throws when creating root directory 1`] = `"EISDIR: illegal operation on a directory, mkdir"`;

exports[`mkdirSync throws when re-creating existing directory 1`] = `"EEXIST: file already exists, mkdir '/new-dir'"`;
53 changes: 53 additions & 0 deletions src/__tests__/volume/mkdirSync.test.ts
@@ -0,0 +1,53 @@
import { create } from '../util';

describe('mkdirSync', () => {
it('can create a directory', () => {
const vol = create();

vol.mkdirSync('/new-dir');
const stat = vol.statSync('/new-dir');

expect(stat.isDirectory()).toBe(true);
});

it('root directory is directory', () => {
const vol = create();
const stat = vol.statSync('/');

expect(stat.isDirectory()).toBe(true);
});

it('throws when re-creating existing directory', () => {
const vol = create();

vol.mkdirSync('/new-dir');

let error;
try {
vol.mkdirSync('/new-dir');
} catch (err) {
error = err;
}

expect(error).toBeInstanceOf(Error);
expect(error.message).toMatchSnapshot();
});

/**
* See issue #325
* https://github.com/streamich/memfs/issues/325
*/
it('throws when creating root directory', () => {
const vol = create();

let error;
try {
vol.mkdirSync('/');
} catch (err) {
error = err;
}

expect(error).toBeInstanceOf(Error);
expect(error.message).toMatchSnapshot();
});
});
6 changes: 6 additions & 0 deletions src/volume.ts
Expand Up @@ -1768,6 +1768,12 @@ export class Volume {

private mkdirBase(filename: string, modeNum: number) {
const steps = filenameToSteps(filename);

// This will throw if user tries to create root dir `fs.mkdirSync('/')`.
if (!steps.length) {
throwError(EISDIR, 'mkdir');
}

const dir = this.getLinkParentAsDirOrThrow(filename, 'mkdir');

// Check path already exists.
Expand Down

0 comments on commit f77fa8b

Please sign in to comment.