@@ -10,7 +10,20 @@ var rimraf = require('rimraf');
10
10
11
11
var mkdirp = require ( '../mkdirp' ) ;
12
12
13
- describe ( 'mkdirp' , function ( ) {
13
+ var log = {
14
+ expected : function ( expected ) {
15
+ if ( process . env . VERBOSE ) {
16
+ console . log ( 'Expected mode:' , expected . toString ( 8 ) ) ;
17
+ }
18
+ } ,
19
+ found : function ( found ) {
20
+ if ( process . env . VERBOSE ) {
21
+ console . log ( 'Found mode' , found . toString ( 8 ) ) ;
22
+ }
23
+ }
24
+ }
25
+
26
+ function suite ( ) {
14
27
var MASK_MODE = parseInt ( '7777' , 8 ) ;
15
28
var DEFAULT_DIR_MODE = parseInt ( '0777' , 8 ) ;
16
29
var isWindows = os . platform ( ) === 'win32' ;
@@ -34,16 +47,30 @@ describe('mkdirp', function () {
34
47
return mode & MASK_MODE ;
35
48
}
36
49
37
- function statMode ( outputPath ) {
38
- return masked ( fs . lstatSync ( outputPath ) . mode ) ;
50
+ function createdMode ( outputPath ) {
51
+ var mode = masked ( fs . lstatSync ( outputPath ) . mode ) ;
52
+ log . found ( mode ) ;
53
+ return mode ;
39
54
}
40
55
41
- function applyUmask ( mode ) {
56
+ function expectedMode ( mode ) {
42
57
if ( typeof mode !== 'number' ) {
43
58
mode = parseInt ( mode , 8 ) ;
44
59
}
45
60
46
- return mode & ~ process . umask ( ) ;
61
+ log . expected ( mode ) ;
62
+ return mode ;
63
+ }
64
+
65
+ function expectedDefaultMode ( ) {
66
+ // Set to use to "get" it
67
+ var current = process . umask ( 0 ) ;
68
+ // Then set it back for the next test
69
+ process . umask ( current ) ;
70
+
71
+ var mode = ( DEFAULT_DIR_MODE & ~ current ) ;
72
+ log . expected ( mode ) ;
73
+ return mode ;
47
74
}
48
75
49
76
beforeEach ( cleanup ) ;
@@ -64,7 +91,7 @@ describe('mkdirp', function () {
64
91
it ( 'makes a single directory' , function ( done ) {
65
92
mkdirp ( outputDirpath , function ( err ) {
66
93
expect ( err ) . toBeFalsy ( ) ;
67
- expect ( statMode ( outputDirpath ) ) . toBeDefined ( ) ;
94
+ expect ( createdMode ( outputDirpath ) ) . toBeDefined ( ) ;
68
95
69
96
done ( ) ;
70
97
} ) ;
@@ -76,11 +103,9 @@ describe('mkdirp', function () {
76
103
return ;
77
104
}
78
105
79
- var defaultMode = applyUmask ( DEFAULT_DIR_MODE ) ;
80
-
81
106
mkdirp ( outputDirpath , function ( err ) {
82
107
expect ( err ) . toBeFalsy ( ) ;
83
- expect ( statMode ( outputDirpath ) ) . toEqual ( defaultMode ) ;
108
+ expect ( createdMode ( outputDirpath ) ) . toEqual ( expectedDefaultMode ( ) ) ;
84
109
85
110
done ( ) ;
86
111
} ) ;
@@ -89,7 +114,7 @@ describe('mkdirp', function () {
89
114
it ( 'makes multiple directories' , function ( done ) {
90
115
mkdirp ( outputNestedDirpath , function ( err ) {
91
116
expect ( err ) . toBeFalsy ( ) ;
92
- expect ( statMode ( outputNestedDirpath ) ) . toBeDefined ( ) ;
117
+ expect ( createdMode ( outputNestedDirpath ) ) . toBeDefined ( ) ;
93
118
94
119
done ( ) ;
95
120
} ) ;
@@ -101,27 +126,57 @@ describe('mkdirp', function () {
101
126
return ;
102
127
}
103
128
104
- var defaultMode = applyUmask ( DEFAULT_DIR_MODE ) ;
105
-
106
129
mkdirp ( outputNestedDirpath , function ( err ) {
107
130
expect ( err ) . toBeFalsy ( ) ;
108
- expect ( statMode ( outputNestedDirpath ) ) . toEqual ( defaultMode ) ;
131
+ expect ( createdMode ( outputNestedDirpath ) ) . toEqual ( expectedDefaultMode ( ) ) ;
132
+
133
+ done ( ) ;
134
+ } ) ;
135
+ } ) ;
136
+
137
+ it ( 'makes directory with custom mode as string' , function ( done ) {
138
+ if ( isWindows ) {
139
+ this . skip ( ) ;
140
+ return ;
141
+ }
142
+
143
+ var mode = '777' ;
144
+
145
+ mkdirp ( outputDirpath , mode , function ( err ) {
146
+ expect ( err ) . toBeFalsy ( ) ;
147
+ expect ( createdMode ( outputDirpath ) ) . toEqual ( expectedMode ( mode ) ) ;
109
148
110
149
done ( ) ;
111
150
} ) ;
112
151
} ) ;
113
152
114
- it ( 'makes directory with custom mode' , function ( done ) {
153
+ it ( 'makes directory with custom mode as octal ' , function ( done ) {
115
154
if ( isWindows ) {
116
155
this . skip ( ) ;
117
156
return ;
118
157
}
119
158
120
- var mode = applyUmask ( '700' ) ;
159
+ var mode = parseInt ( '777' , 8 ) ;
121
160
122
161
mkdirp ( outputDirpath , mode , function ( err ) {
123
162
expect ( err ) . toBeFalsy ( ) ;
124
- expect ( statMode ( outputDirpath ) ) . toEqual ( mode ) ;
163
+ expect ( createdMode ( outputDirpath ) ) . toEqual ( expectedMode ( mode ) ) ;
164
+
165
+ done ( ) ;
166
+ } ) ;
167
+ } ) ;
168
+
169
+ it ( 'does not mask a custom mode' , function ( done ) {
170
+ if ( isWindows ) {
171
+ this . skip ( ) ;
172
+ return ;
173
+ }
174
+
175
+ var mode = parseInt ( '777' , 8 ) ;
176
+
177
+ mkdirp ( outputDirpath , mode , function ( err ) {
178
+ expect ( err ) . toBeFalsy ( ) ;
179
+ expect ( createdMode ( outputDirpath ) ) . toEqual ( mode ) ;
125
180
126
181
done ( ) ;
127
182
} ) ;
@@ -133,11 +188,11 @@ describe('mkdirp', function () {
133
188
return ;
134
189
}
135
190
136
- var mode = applyUmask ( '2700' ) ;
191
+ var mode = '2700' ;
137
192
138
193
mkdirp ( outputDirpath , mode , function ( err ) {
139
194
expect ( err ) . toBeFalsy ( ) ;
140
- expect ( statMode ( outputDirpath ) ) . toEqual ( mode ) ;
195
+ expect ( createdMode ( outputDirpath ) ) . toEqual ( expectedMode ( mode ) ) ;
141
196
142
197
done ( ) ;
143
198
} ) ;
@@ -149,14 +204,14 @@ describe('mkdirp', function () {
149
204
return ;
150
205
}
151
206
152
- var mode = applyUmask ( '700' ) ;
207
+ var mode = '777' ;
153
208
154
209
mkdirp ( outputDirpath , mode , function ( err ) {
155
210
expect ( err ) . toBeFalsy ( ) ;
156
211
157
212
mkdirp ( outputDirpath , function ( err2 ) {
158
213
expect ( err2 ) . toBeFalsy ( ) ;
159
- expect ( statMode ( outputDirpath ) ) . toEqual ( mode ) ;
214
+ expect ( createdMode ( outputDirpath ) ) . toEqual ( expectedMode ( mode ) ) ;
160
215
161
216
done ( ) ;
162
217
} ) ;
@@ -169,11 +224,11 @@ describe('mkdirp', function () {
169
224
return ;
170
225
}
171
226
172
- var mode = applyUmask ( '700' ) ;
227
+ var mode = '777' ;
173
228
174
229
mkdirp ( outputNestedDirpath , mode , function ( err ) {
175
230
expect ( err ) . toBeFalsy ( ) ;
176
- expect ( statMode ( outputNestedDirpath ) ) . toEqual ( mode ) ;
231
+ expect ( createdMode ( outputNestedDirpath ) ) . toEqual ( expectedMode ( mode ) ) ;
177
232
178
233
done ( ) ;
179
234
} ) ;
@@ -186,13 +241,13 @@ describe('mkdirp', function () {
186
241
}
187
242
188
243
var intermediateDirpath = path . dirname ( outputNestedDirpath ) ;
189
- var mode = applyUmask ( '700' ) ;
190
- var defaultMode = applyUmask ( DEFAULT_DIR_MODE ) ;
244
+ var mode = '777' ;
191
245
192
246
mkdirp ( outputNestedDirpath , mode , function ( err ) {
193
247
expect ( err ) . toBeFalsy ( ) ;
194
- expect ( statMode ( outputDirpath ) ) . toEqual ( defaultMode ) ;
195
- expect ( statMode ( intermediateDirpath ) ) . toEqual ( defaultMode ) ;
248
+ expect ( createdMode ( outputDirpath ) ) . toEqual ( expectedDefaultMode ( ) ) ;
249
+ expect ( createdMode ( intermediateDirpath ) ) . toEqual ( expectedDefaultMode ( ) ) ;
250
+ expect ( createdMode ( outputNestedDirpath ) ) . toEqual ( expectedMode ( mode ) ) ;
196
251
197
252
done ( ) ;
198
253
} ) ;
@@ -204,16 +259,15 @@ describe('mkdirp', function () {
204
259
return ;
205
260
}
206
261
207
- var mode = applyUmask ( '700' ) ;
208
- var defaultMode = applyUmask ( DEFAULT_DIR_MODE ) ;
262
+ var mode = '777' ;
209
263
210
264
mkdirp ( outputDirpath , function ( err ) {
211
265
expect ( err ) . toBeFalsy ( ) ;
212
- expect ( statMode ( outputDirpath ) ) . toEqual ( defaultMode ) ;
266
+ expect ( createdMode ( outputDirpath ) ) . toEqual ( expectedDefaultMode ( ) ) ;
213
267
214
268
mkdirp ( outputDirpath , mode , function ( err2 ) {
215
269
expect ( err2 ) . toBeFalsy ( ) ;
216
- expect ( statMode ( outputDirpath ) ) . toEqual ( mode ) ;
270
+ expect ( createdMode ( outputDirpath ) ) . toEqual ( expectedMode ( mode ) ) ;
217
271
218
272
done ( ) ;
219
273
} ) ;
@@ -243,19 +297,20 @@ describe('mkdirp', function () {
243
297
return ;
244
298
}
245
299
246
- var mode = applyUmask ( '700' ) ;
300
+ var mode = '777' ;
247
301
248
302
mkdirp ( outputDirpath , function ( err ) {
249
303
expect ( err ) . toBeFalsy ( ) ;
250
304
251
305
fs . writeFile ( outputNestedPath , contents , function ( err2 ) {
252
306
expect ( err2 ) . toBeFalsy ( ) ;
253
307
254
- var expectedMode = statMode ( outputNestedPath ) ;
308
+ var existingMode = createdMode ( outputNestedPath ) ;
309
+ expect ( existingMode ) . not . toEqual ( mode ) ;
255
310
256
311
mkdirp ( outputNestedPath , mode , function ( err3 ) {
257
312
expect ( err3 ) . toBeDefined ( ) ;
258
- expect ( statMode ( outputNestedPath ) ) . toEqual ( expectedMode ) ;
313
+ expect ( createdMode ( outputNestedPath ) ) . toEqual ( existingMode ) ;
259
314
260
315
done ( ) ;
261
316
} ) ;
@@ -300,7 +355,7 @@ describe('mkdirp', function () {
300
355
return ;
301
356
}
302
357
303
- var mode = applyUmask ( '700' ) ;
358
+ var mode = '777' ;
304
359
305
360
mkdirp ( outputDirpath , mode , function ( err ) {
306
361
expect ( err ) . toBeFalsy ( ) ;
@@ -315,4 +370,25 @@ describe('mkdirp', function () {
315
370
} ) ;
316
371
} ) ;
317
372
} ) ;
373
+ }
374
+
375
+ describe ( 'mkdirp' , suite ) ;
376
+
377
+ describe ( 'mkdirp with umask' , function ( ) {
378
+
379
+ var startingUmask ;
380
+ before ( function ( done ) {
381
+ startingUmask = process . umask ( parseInt ( '066' , 8 ) ) ;
382
+
383
+ done ( ) ;
384
+ } ) ;
385
+
386
+ after ( function ( done ) {
387
+ process . umask ( startingUmask ) ;
388
+
389
+ done ( ) ;
390
+ } )
391
+
392
+ // Initialize the normal suite
393
+ suite ( ) ;
318
394
} ) ;
0 commit comments