File tree Expand file tree Collapse file tree 3 files changed +64
-0
lines changed Expand file tree Collapse file tree 3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change 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'"` ;
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments