Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 馃悰 throw when creating root directory #326

Merged
merged 2 commits into from Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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', filename);
}

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

// Check path already exists.
Expand Down