@@ -74,31 +74,37 @@ async function getHandle(dest) {
74
74
{
75
75
async function doTest ( ) {
76
76
tmpdir . refresh ( ) ;
77
+
77
78
const dest = path . resolve ( tmpDir , 'baz.js' ) ;
78
- await copyFile ( fixtures . path ( 'baz.js' ) , dest ) ;
79
- await access ( dest , 'r' ) ;
80
79
81
- const handle = await open ( dest , 'r+' ) ;
82
- assert . strictEqual ( typeof handle , 'object' ) ;
80
+ // handle is object
81
+ {
82
+ const handle = await getHandle ( dest ) ;
83
+ assert . strictEqual ( typeof handle , 'object' ) ;
84
+ }
83
85
84
- let stats = await handle . stat ( ) ;
85
- verifyStatObject ( stats ) ;
86
- assert . strictEqual ( stats . size , 35 ) ;
86
+ // file stats
87
+ {
88
+ const handle = await getHandle ( dest ) ;
89
+ let stats = await handle . stat ( ) ;
90
+ verifyStatObject ( stats ) ;
91
+ assert . strictEqual ( stats . size , 35 ) ;
87
92
88
- await handle . truncate ( 1 ) ;
93
+ await handle . truncate ( 1 ) ;
89
94
90
- stats = await handle . stat ( ) ;
91
- verifyStatObject ( stats ) ;
92
- assert . strictEqual ( stats . size , 1 ) ;
95
+ stats = await handle . stat ( ) ;
96
+ verifyStatObject ( stats ) ;
97
+ assert . strictEqual ( stats . size , 1 ) ;
93
98
94
- stats = await stat ( dest ) ;
95
- verifyStatObject ( stats ) ;
99
+ stats = await stat ( dest ) ;
100
+ verifyStatObject ( stats ) ;
96
101
97
- stats = await handle . stat ( ) ;
98
- verifyStatObject ( stats ) ;
102
+ stats = await handle . stat ( ) ;
103
+ verifyStatObject ( stats ) ;
99
104
100
- await handle . datasync ( ) ;
101
- await handle . sync ( ) ;
105
+ await handle . datasync ( ) ;
106
+ await handle . sync ( ) ;
107
+ }
102
108
103
109
// test fs.read promises when length to read is zero bytes
104
110
{
@@ -113,115 +119,140 @@ async function getHandle(dest) {
113
119
await unlink ( dest ) ;
114
120
}
115
121
116
- const buf = Buffer . from ( 'hello fsPromises' ) ;
117
- const bufLen = buf . length ;
118
- await handle . write ( buf ) ;
119
- const ret = await handle . read ( Buffer . alloc ( bufLen ) , 0 , bufLen , 0 ) ;
120
- assert . strictEqual ( ret . bytesRead , bufLen ) ;
121
- assert . deepStrictEqual ( ret . buffer , buf ) ;
122
-
123
- const buf2 = Buffer . from ( 'hello FileHandle' ) ;
124
- const buf2Len = buf2 . length ;
125
- await handle . write ( buf2 , 0 , buf2Len , 0 ) ;
126
- const ret2 = await handle . read ( Buffer . alloc ( buf2Len ) , 0 , buf2Len , 0 ) ;
127
- assert . strictEqual ( ret2 . bytesRead , buf2Len ) ;
128
- assert . deepStrictEqual ( ret2 . buffer , buf2 ) ;
129
- await truncate ( dest , 5 ) ;
130
- assert . deepStrictEqual ( ( await readFile ( dest ) ) . toString ( ) , 'hello' ) ;
131
-
132
- await chmod ( dest , 0o666 ) ;
133
- await handle . chmod ( 0o666 ) ;
134
-
135
- await chmod ( dest , ( 0o10777 ) ) ;
136
- await handle . chmod ( 0o10777 ) ;
137
-
138
- if ( ! common . isWindows ) {
139
- await chown ( dest , process . getuid ( ) , process . getgid ( ) ) ;
140
- await handle . chown ( process . getuid ( ) , process . getgid ( ) ) ;
122
+ // bytes written to file match buffer
123
+ {
124
+ const handle = await getHandle ( dest ) ;
125
+ const buf = Buffer . from ( 'hello fsPromises' ) ;
126
+ const bufLen = buf . length ;
127
+ await handle . write ( buf ) ;
128
+ const ret = await handle . read ( Buffer . alloc ( bufLen ) , 0 , bufLen , 0 ) ;
129
+ assert . strictEqual ( ret . bytesRead , bufLen ) ;
130
+ assert . deepStrictEqual ( ret . buffer , buf ) ;
141
131
}
142
132
143
- assert . rejects (
144
- async ( ) => {
145
- await chown ( dest , 1 , - 1 ) ;
146
- } ,
147
- {
148
- code : 'ERR_OUT_OF_RANGE' ,
149
- name : 'RangeError [ERR_OUT_OF_RANGE]' ,
150
- message : 'The value of "gid" is out of range. ' +
151
- 'It must be >= 0 && < 4294967296. Received -1'
152
- } ) ;
133
+ // truncate file to specified length
134
+ {
135
+ const handle = await getHandle ( dest ) ;
136
+ const buf = Buffer . from ( 'hello FileHandle' ) ;
137
+ const bufLen = buf . length ;
138
+ await handle . write ( buf , 0 , bufLen , 0 ) ;
139
+ const ret = await handle . read ( Buffer . alloc ( bufLen ) , 0 , bufLen , 0 ) ;
140
+ assert . strictEqual ( ret . bytesRead , bufLen ) ;
141
+ assert . deepStrictEqual ( ret . buffer , buf ) ;
142
+ await truncate ( dest , 5 ) ;
143
+ assert . deepStrictEqual ( ( await readFile ( dest ) ) . toString ( ) , 'hello' ) ;
144
+ }
153
145
154
- assert . rejects (
155
- async ( ) => {
156
- await handle . chown ( 1 , - 1 ) ;
157
- } ,
158
- {
159
- code : 'ERR_OUT_OF_RANGE' ,
160
- name : 'RangeError [ERR_OUT_OF_RANGE]' ,
161
- message : 'The value of "gid" is out of range. ' +
162
- 'It must be >= 0 && < 4294967296. Received -1'
163
- } ) ;
146
+ // invalid change of ownership
147
+ {
148
+ const handle = await getHandle ( dest ) ;
164
149
165
- await utimes ( dest , new Date ( ) , new Date ( ) ) ;
166
-
167
- try {
168
- await handle . utimes ( new Date ( ) , new Date ( ) ) ;
169
- } catch ( err ) {
170
- // Some systems do not have futimes. If there is an error,
171
- // expect it to be ENOSYS
172
- common . expectsError ( {
173
- code : 'ENOSYS' ,
174
- type : Error
175
- } ) ( err ) ;
150
+ await chmod ( dest , 0o666 ) ;
151
+ await handle . chmod ( 0o666 ) ;
152
+
153
+ await chmod ( dest , ( 0o10777 ) ) ;
154
+ await handle . chmod ( 0o10777 ) ;
155
+
156
+ if ( ! common . isWindows ) {
157
+ await chown ( dest , process . getuid ( ) , process . getgid ( ) ) ;
158
+ await handle . chown ( process . getuid ( ) , process . getgid ( ) ) ;
159
+ }
160
+
161
+ assert . rejects (
162
+ async ( ) => {
163
+ await chown ( dest , 1 , - 1 ) ;
164
+ } ,
165
+ {
166
+ code : 'ERR_OUT_OF_RANGE' ,
167
+ name : 'RangeError [ERR_OUT_OF_RANGE]' ,
168
+ message : 'The value of "gid" is out of range. ' +
169
+ 'It must be >= 0 && < 4294967296. Received -1'
170
+ } ) ;
171
+
172
+ assert . rejects (
173
+ async ( ) => {
174
+ await handle . chown ( 1 , - 1 ) ;
175
+ } ,
176
+ {
177
+ code : 'ERR_OUT_OF_RANGE' ,
178
+ name : 'RangeError [ERR_OUT_OF_RANGE]' ,
179
+ message : 'The value of "gid" is out of range. ' +
180
+ 'It must be >= 0 && < 4294967296. Received -1'
181
+ } ) ;
176
182
}
177
183
178
- await handle . close ( ) ;
184
+ // set modification times
185
+ {
186
+ const handle = await getHandle ( dest ) ;
179
187
180
- const newPath = path . resolve ( tmpDir , 'baz2.js' ) ;
181
- await rename ( dest , newPath ) ;
182
- stats = await stat ( newPath ) ;
183
- verifyStatObject ( stats ) ;
188
+ await utimes ( dest , new Date ( ) , new Date ( ) ) ;
184
189
185
- if ( common . canCreateSymLink ( ) ) {
186
- const newLink = path . resolve ( tmpDir , 'baz3.js' ) ;
187
- await symlink ( newPath , newLink ) ;
188
- if ( ! common . isWindows ) {
189
- await lchown ( newLink , process . getuid ( ) , process . getgid ( ) ) ;
190
+ try {
191
+ await handle . utimes ( new Date ( ) , new Date ( ) ) ;
192
+ } catch ( err ) {
193
+ // Some systems do not have futimes. If there is an error,
194
+ // expect it to be ENOSYS
195
+ common . expectsError ( {
196
+ code : 'ENOSYS' ,
197
+ type : Error
198
+ } ) ( err ) ;
190
199
}
191
- stats = await lstat ( newLink ) ;
192
- verifyStatObject ( stats ) ;
193
200
194
- assert . strictEqual ( newPath . toLowerCase ( ) ,
195
- ( await realpath ( newLink ) ) . toLowerCase ( ) ) ;
196
- assert . strictEqual ( newPath . toLowerCase ( ) ,
197
- ( await readlink ( newLink ) ) . toLowerCase ( ) ) ;
201
+ await handle . close ( ) ;
202
+ }
203
+
204
+ // create symlink
205
+ {
206
+ const newPath = path . resolve ( tmpDir , 'baz2.js' ) ;
207
+ await rename ( dest , newPath ) ;
208
+ let stats = await stat ( newPath ) ;
209
+ verifyStatObject ( stats ) ;
198
210
199
- const newMode = 0o666 ;
200
- if ( common . isOSX ) {
201
- // lchmod is only available on macOS
202
- await lchmod ( newLink , newMode ) ;
211
+ if ( common . canCreateSymLink ( ) ) {
212
+ const newLink = path . resolve ( tmpDir , 'baz3.js' ) ;
213
+ await symlink ( newPath , newLink ) ;
214
+ if ( ! common . isWindows ) {
215
+ await lchown ( newLink , process . getuid ( ) , process . getgid ( ) ) ;
216
+ }
203
217
stats = await lstat ( newLink ) ;
204
- assert . strictEqual ( stats . mode & 0o777 , newMode ) ;
205
- } else {
206
- await Promise . all ( [
207
- assert . rejects (
208
- lchmod ( newLink , newMode ) ,
209
- common . expectsError ( {
210
- code : 'ERR_METHOD_NOT_IMPLEMENTED' ,
211
- type : Error ,
212
- message : 'The lchmod() method is not implemented'
213
- } )
214
- )
215
- ] ) ;
218
+ verifyStatObject ( stats ) ;
219
+
220
+ assert . strictEqual ( newPath . toLowerCase ( ) ,
221
+ ( await realpath ( newLink ) ) . toLowerCase ( ) ) ;
222
+ assert . strictEqual ( newPath . toLowerCase ( ) ,
223
+ ( await readlink ( newLink ) ) . toLowerCase ( ) ) ;
224
+
225
+ const newMode = 0o666 ;
226
+ if ( common . isOSX ) {
227
+ // lchmod is only available on macOS
228
+ await lchmod ( newLink , newMode ) ;
229
+ stats = await lstat ( newLink ) ;
230
+ assert . strictEqual ( stats . mode & 0o777 , newMode ) ;
231
+ } else {
232
+ await Promise . all ( [
233
+ assert . rejects (
234
+ lchmod ( newLink , newMode ) ,
235
+ common . expectsError ( {
236
+ code : 'ERR_METHOD_NOT_IMPLEMENTED' ,
237
+ type : Error ,
238
+ message : 'The lchmod() method is not implemented'
239
+ } )
240
+ )
241
+ ] ) ;
242
+ }
243
+
244
+ await unlink ( newLink ) ;
216
245
}
217
-
218
- await unlink ( newLink ) ;
219
246
}
220
247
221
- const newLink2 = path . resolve ( tmpDir , 'baz4.js' ) ;
222
- await link ( newPath , newLink2 ) ;
248
+ // create hard link
249
+ {
250
+ const newPath = path . resolve ( tmpDir , 'baz2.js' ) ;
251
+ const newLink = path . resolve ( tmpDir , 'baz4.js' ) ;
252
+ await link ( newPath , newLink ) ;
223
253
224
- await unlink ( newLink2 ) ;
254
+ await unlink ( newLink ) ;
255
+ }
225
256
226
257
// testing readdir lists both files and directories
227
258
{
@@ -231,7 +262,7 @@ async function getHandle(dest) {
231
262
await mkdir ( newDir ) ;
232
263
await writeFile ( newFile , 'DAWGS WIN!' , 'utf8' ) ;
233
264
234
- stats = await stat ( newDir ) ;
265
+ const stats = await stat ( newDir ) ;
235
266
assert ( stats . isDirectory ( ) ) ;
236
267
const list = await readdir ( tmpDir ) ;
237
268
assert . notStrictEqual ( list . indexOf ( 'dir' ) , - 1 ) ;
@@ -244,23 +275,23 @@ async function getHandle(dest) {
244
275
{
245
276
const dir = path . join ( tmpDir , nextdir ( ) ) ;
246
277
await mkdir ( dir , 777 ) ;
247
- stats = await stat ( dir ) ;
278
+ const stats = await stat ( dir ) ;
248
279
assert ( stats . isDirectory ( ) ) ;
249
280
}
250
281
251
282
// mkdir when options is string.
252
283
{
253
284
const dir = path . join ( tmpDir , nextdir ( ) ) ;
254
285
await mkdir ( dir , '777' ) ;
255
- stats = await stat ( dir ) ;
286
+ const stats = await stat ( dir ) ;
256
287
assert ( stats . isDirectory ( ) ) ;
257
288
}
258
289
259
290
// mkdirp when folder does not yet exist.
260
291
{
261
292
const dir = path . join ( tmpDir , nextdir ( ) , nextdir ( ) ) ;
262
293
await mkdir ( dir , { recursive : true } ) ;
263
- stats = await stat ( dir ) ;
294
+ const stats = await stat ( dir ) ;
264
295
assert ( stats . isDirectory ( ) ) ;
265
296
}
266
297
@@ -283,15 +314,15 @@ async function getHandle(dest) {
283
314
{
284
315
const dir = path . resolve ( tmpDir , `${ nextdir ( ) } /./${ nextdir ( ) } ` ) ;
285
316
await mkdir ( dir , { recursive : true } ) ;
286
- stats = await stat ( dir ) ;
317
+ const stats = await stat ( dir ) ;
287
318
assert ( stats . isDirectory ( ) ) ;
288
319
}
289
320
290
321
// mkdirp ../
291
322
{
292
323
const dir = path . resolve ( tmpDir , `${ nextdir ( ) } /../${ nextdir ( ) } ` ) ;
293
324
await mkdir ( dir , { recursive : true } ) ;
294
- stats = await stat ( dir ) ;
325
+ const stats = await stat ( dir ) ;
295
326
assert ( stats . isDirectory ( ) ) ;
296
327
}
297
328
@@ -313,15 +344,18 @@ async function getHandle(dest) {
313
344
} ) ;
314
345
}
315
346
316
- await mkdtemp ( path . resolve ( tmpDir , 'FOO' ) ) ;
317
- assert . rejects (
318
- // mkdtemp() expects to get a string prefix.
319
- async ( ) => mkdtemp ( 1 ) ,
320
- {
321
- code : 'ERR_INVALID_ARG_TYPE' ,
322
- name : 'TypeError [ERR_INVALID_ARG_TYPE]'
323
- }
324
- ) ;
347
+ // mkdtemp with invalid numeric prefix
348
+ {
349
+ await mkdtemp ( path . resolve ( tmpDir , 'FOO' ) ) ;
350
+ assert . rejects (
351
+ // mkdtemp() expects to get a string prefix.
352
+ async ( ) => mkdtemp ( 1 ) ,
353
+ {
354
+ code : 'ERR_INVALID_ARG_TYPE' ,
355
+ name : 'TypeError [ERR_INVALID_ARG_TYPE]'
356
+ }
357
+ ) ;
358
+ }
325
359
326
360
}
327
361
0 commit comments