@@ -7,6 +7,8 @@ const { overrideNode } = require('../../lib/node/fs')
77
88overrideNode ( )
99
10+ // lstatSync
11+
1012tap . test ( 'lstatSync' , function ( test ) {
1113 const stat = fs . lstatSync ( path . resolve ( 'test' ) )
1214 test . ok ( stat , 'lstatSync should return valid result' )
@@ -22,6 +24,8 @@ tap.test('lstatSync', function (test) {
2224 test . end ( )
2325} )
2426
27+ // lstat
28+
2529tap . test ( 'lstat' , function ( test ) {
2630 fs . lstat ( path . resolve ( 'test' ) , ( err , stat ) => {
2731 test . notOk ( err , `lstat should not fail with error` )
@@ -38,6 +42,8 @@ tap.test('lstat', function (test) {
3842 } )
3943} )
4044
45+ // statSync
46+
4147tap . test ( 'statSync' , function ( test ) {
4248 const stat = fs . statSync ( path . resolve ( 'test' ) )
4349 tap . ok ( stat , 'statSync should return valid result' )
@@ -53,6 +59,8 @@ tap.test('statSync', function (test) {
5359 test . end ( )
5460} )
5561
62+ // stat
63+
5664tap . test ( 'stat' , function ( test ) {
5765 fs . stat ( path . resolve ( 'test' ) , ( err , stat ) => {
5866 test . notOk ( err , `stat should not fail with error` )
@@ -69,12 +77,31 @@ tap.test('stat', function (test) {
6977 } )
7078} )
7179
80+ // skipping statSyncNoException
81+
82+ // realpathSync
83+
7284tap . test ( 'realpathSync' , function ( test ) {
7385 const rp = fs . realpathSync ( '.' )
7486 test . equal ( rp , process . cwd ( ) , 'realpathSync should return cwd' )
87+ // test exception
88+ try {
89+ fs . realpathSync ( `/non-existing-dir-${ Date . now ( ) } ` )
90+ test . fail ( `realpathSync should throw` )
91+ } catch ( e ) { }
7592 test . end ( )
7693} )
7794
95+ tap . test ( 'realpathSync' , function ( test ) {
96+ try {
97+ fs . realpathSync ( `/non-existing-dir-${ Date . now ( ) } ` )
98+ test . fail ( `realpathSync should throw` )
99+ } catch ( e ) { }
100+ test . end ( )
101+ } )
102+
103+ // realpath
104+
78105tap . test ( 'realpath' , function ( test ) {
79106 fs . realpath ( '.' , ( err , rp ) => {
80107 test . notOk ( err , 'realpath should not fail' )
@@ -83,8 +110,224 @@ tap.test('realpath', function (test) {
83110 } )
84111} )
85112
113+ tap . test ( 'realpath' , function ( test ) {
114+ fs . realpath ( `/non-existing-dir-${ Date . now ( ) } ` , ( err , rp ) => {
115+ test . ok ( err , 'realpath should fail with error' )
116+ test . end ( )
117+ } )
118+ } )
119+
120+ // skip standard deprecated fs.exists
121+
122+ // existsSync
123+
86124tap . test ( 'existsSync' , function ( test ) {
87125 const flag = fs . existsSync ( path . resolve ( 'test' ) )
88126 test . equal ( flag , true , 'exists should return true' )
89127 test . end ( )
90128} )
129+
130+ tap . test ( 'existsSync' , function ( test ) {
131+ const flag = fs . existsSync ( `/non-existing-dir-${ Date . now ( ) } ` )
132+ test . equal ( flag , false , 'exists should return false' )
133+ test . end ( )
134+ } )
135+
136+ // access
137+
138+ tap . test ( 'access' , function ( test ) {
139+ fs . access ( path . resolve ( 'test' ) , ( err ) => {
140+ test . notOk ( err , 'access should return OK' )
141+ test . end ( )
142+ } )
143+ } )
144+
145+ tap . test ( 'access' , function ( test ) {
146+ fs . access ( path . resolve ( 'test' ) , fs . constants . F_OK , ( err , result ) => {
147+ test . notOk ( err , 'access should return OK with F_OK mode' )
148+ test . end ( )
149+ } )
150+ } )
151+
152+ tap . test ( 'access' , function ( test ) {
153+ fs . access ( path . resolve ( 'package.json' ) , fs . constants . R_OK , ( err ) => {
154+ test . notOk ( err , 'access should return OK with R_OK mode' )
155+ test . end ( )
156+ } )
157+ } )
158+
159+ tap . test ( 'access' , function ( test ) {
160+ fs . access ( path . resolve ( `/non-existing-dir-${ Date . now ( ) } ` ) , fs . constants . R_OK , ( err ) => {
161+ test . equal ( err . code , 'ENOENT' , 'access fail with ENOENT' )
162+ test . end ( )
163+ } )
164+ } )
165+
166+ tap . test ( 'access' , function ( test ) {
167+ fs . access ( path . resolve ( `/etc/passwd` ) , fs . constants . W_OK , ( err ) => {
168+ test . equal ( err . code , 'EACCES' , 'access fail with EACCES for W_OK on /etc/passwd' )
169+ test . end ( )
170+ } )
171+ } )
172+
173+ // accessSync
174+
175+ tap . test ( 'accessSync' , function ( test ) {
176+ try {
177+ fs . accessSync ( path . resolve ( 'test' ) )
178+ test . end ( )
179+ } catch ( err ) {
180+ test . fail ( 'accessSync should return OK' )
181+ }
182+ } )
183+
184+ tap . test ( 'accessSync' , function ( test ) {
185+ try {
186+ fs . accessSync ( path . resolve ( 'test' ) , fs . constants . F_OK )
187+ test . end ( )
188+ } catch ( err ) {
189+ test . fail ( 'accessSync should return OK with F_OK mode' )
190+ }
191+ } )
192+
193+ tap . test ( 'accessSync' , function ( test ) {
194+ try {
195+ fs . accessSync ( path . resolve ( 'package.json' ) , fs . constants . R_OK )
196+ test . end ( )
197+ } catch ( err ) {
198+ test . fail ( 'accessSync should return OK with R_OK mode' )
199+ }
200+ } )
201+
202+ tap . test ( 'accessSync' , function ( test ) {
203+ try {
204+ fs . accessSync ( path . resolve ( `/non-existing-dir-${ Date . now ( ) } ` ) , fs . constants . R_OK )
205+ test . fail ( 'accessSync should fail with ENOENT' )
206+ } catch ( err ) {
207+ test . end ( )
208+ }
209+ } )
210+
211+ tap . test ( 'accessSync' , function ( test ) {
212+ try {
213+ fs . accessSync ( path . resolve ( `/etc/passwd` ) , fs . constants . W_OK )
214+ test . fail ( 'accessSync should fail with EACCES for W_OK on /etc/passwd' )
215+ } catch ( err ) {
216+ test . end ( )
217+ }
218+ } )
219+
220+ // readFile
221+
222+ tap . test ( 'readFile' , function ( test ) {
223+ fs . readFile ( path . resolve ( 'package.json' ) , ( err , data ) => {
224+ test . notOk ( err , 'readFile should be OK' )
225+ try {
226+ test . equal ( Buffer . isBuffer ( data ) , true , 'readFile should load package.json as Buffer' )
227+ JSON . parse ( data )
228+ test . end ( )
229+ } catch ( err ) {
230+ test . fail ( 'readFile should load package.json' )
231+ }
232+ } )
233+ } )
234+
235+ tap . test ( 'readFile' , function ( test ) {
236+ fs . readFile ( path . resolve ( 'package.json' ) , 'utf8' , ( err , data ) => {
237+ test . notOk ( err , 'readFile utf8 encoding should be OK' )
238+ try {
239+ test . equal ( typeof data , 'string' , 'readFile utf8 encoding should load package.json as string' )
240+ JSON . parse ( data )
241+ test . end ( )
242+ } catch ( err ) {
243+ test . fail ( 'readFile utf8 encoding should load package.json' )
244+ }
245+ } )
246+ } )
247+
248+ // readFileSync
249+
250+ tap . test ( 'readFileSync' , function ( test ) {
251+ try {
252+ const data = fs . readFileSync ( path . resolve ( 'package.json' ) )
253+ test . equal ( Buffer . isBuffer ( data ) , true , 'readFileSync should load package.json as Buffer' )
254+ JSON . parse ( data )
255+ test . end ( )
256+ } catch ( err ) {
257+ test . fail ( 'readFileSync should load package.json' )
258+ }
259+ } )
260+
261+ tap . test ( 'readFileSync' , function ( test ) {
262+ try {
263+ const data = fs . readFileSync ( path . resolve ( 'package.json' ) , 'utf8' )
264+ test . equal ( typeof data , 'string' , 'readFileSync utf8 encoding should load package.json as string' )
265+ JSON . parse ( data )
266+ test . end ( )
267+ } catch ( err ) {
268+ test . fail ( 'readFileSync utf8 encoding should load package.json' )
269+ }
270+ } )
271+
272+ // readdir
273+
274+ tap . test ( 'readdir' , function ( test ) {
275+ fs . readdir ( path . resolve ( 'test/fixtures/fs/readdir' ) , ( err , files ) => {
276+ test . notOk ( err , 'readdir should return no error' )
277+ test . equal ( files . sort ( ) . join ( ',' ) , 'bar,foo,tmp' , 'readdir should read dir entries' )
278+ test . end ( )
279+ } )
280+ } )
281+
282+ // readdirSync
283+
284+ tap . test ( 'readdirSync' , function ( test ) {
285+ try {
286+ const files = fs . readdirSync ( path . resolve ( 'test/fixtures/fs/readdir' ) )
287+ test . equal ( files . sort ( ) . join ( ',' ) , 'bar,foo,tmp' , 'readdirSync should read dir entries' )
288+ test . end ( )
289+ } catch ( err ) {
290+ test . fail ( err , 'readdirSync should return no error' )
291+ }
292+ } )
293+
294+ // chmod
295+
296+ function testChmod ( test , filename , mode ) {
297+ const verify = ( expectMode , cb ) => {
298+ fs . stat ( filename , ( err , stat ) => {
299+ test . notOk ( err , `stat ${ filename } failed` )
300+ const newMode = stat . mode & 0o777
301+ test . equal ( newMode , expectMode , `stat not agree with chmod on mode: ${ newMode } vs ${ expectMode } ` )
302+ cb ( )
303+ } )
304+ }
305+
306+ fs . stat ( filename , ( err , stat ) => {
307+ test . notOk ( err , `stat ${ filename } failed` )
308+ const origMode = stat . mode & 0o777
309+ fs . chmod ( filename , mode , ( err ) => {
310+ test . notOk ( err , `a. chmod ${ mode } failed ${ err } ` )
311+ verify ( mode , ( ) => {
312+ fs . chmod ( filename , origMode , ( err ) => {
313+ test . notOk ( err , `b. chmod ${ mode } failed ${ err } ` )
314+ verify ( origMode , ( ) => {
315+ test . end ( )
316+ } )
317+ } )
318+ } )
319+ } )
320+ } )
321+ }
322+
323+ tap . test ( 'chmod' , function ( test ) {
324+ testChmod ( test , path . resolve ( 'test/fixtures/fs' ) , 0o777 )
325+ } )
326+
327+ // open
328+
329+ // openSync
330+
331+ // createReadStream
332+
333+ // createWriteStream
0 commit comments