11import type { Stats } from 'fs'
22import fs , { readFileSync } from 'fs'
3- //@ts -ignore
4- import mutateFS from 'mutate-fs'
3+ import fsp from 'fs/promises'
54import { dirname , resolve } from 'path'
65import type { Test } from 'tap'
76import t from 'tap'
@@ -14,6 +13,14 @@ import { makeTar } from './fixtures/make-tar.js'
1413const __filename = fileURLToPath ( import . meta. url )
1514const __dirname = dirname ( __filename )
1615
16+ const poop = new Error ( 'poop' )
17+ const cbFail = ( ...args : any [ ] ) =>
18+ ( args [ args . length - 1 ] as Function ) ( poop )
19+ const syncFail = ( ) => {
20+ throw poop
21+ }
22+ const asyncFail = async ( ) => syncFail ( )
23+
1724const lp = JSON . parse (
1825 readFileSync ( __dirname + '/fixtures/parse/long-paths.json' , 'utf8' ) ,
1926) as (
@@ -181,9 +188,28 @@ t.test('bad args', t => {
181188 t . end ( )
182189} )
183190
184- t . test ( 'stat fails' , t => {
185- const poop = new Error ( 'poop' )
186- t . teardown ( mutateFS . statFail ( poop ) )
191+ t . test ( 'stat fails' , async t => {
192+ const { list } = await t . mockImport < typeof import ( '../src/list.js' ) > (
193+ '../src/list.js' ,
194+ {
195+ fs : t . createMock ( fs , {
196+ open : cbFail ,
197+ stat : cbFail ,
198+ lstat : cbFail ,
199+ fstat : cbFail ,
200+ openSync : syncFail ,
201+ statSync : syncFail ,
202+ lstatSync : syncFail ,
203+ fstatSync : syncFail ,
204+ } ) ,
205+ 'fs/promises' : t . createMock ( fsp , {
206+ open : asyncFail ,
207+ stat : asyncFail ,
208+ lstat : asyncFail ,
209+ fstat : asyncFail ,
210+ } ) ,
211+ } ,
212+ )
187213 t . test ( 'sync' , t => {
188214 t . plan ( 1 )
189215 t . throws ( ( ) => list ( { file : __filename , sync : true } ) , poop )
@@ -200,9 +226,19 @@ t.test('stat fails', t => {
200226} )
201227
202228t . test ( 'read fail' , t => {
203- t . test ( 'sync' , t => {
204- const poop = new Error ( 'poop' )
205- t . teardown ( mutateFS . fail ( 'read' , poop ) )
229+ t . test ( 'sync' , async t => {
230+ const { list } = await t . mockImport < typeof import ( '../src/list.js' ) > (
231+ '../src/list.js' ,
232+ {
233+ fs : t . createMock ( fs , {
234+ read : cbFail ,
235+ readSync : syncFail ,
236+ } ) ,
237+ 'fs/promises' : t . createMock ( fsp , {
238+ read : asyncFail ,
239+ } ) ,
240+ } ,
241+ )
206242 t . plan ( 1 )
207243 t . throws (
208244 ( ) =>
@@ -214,17 +250,39 @@ t.test('read fail', t => {
214250 poop ,
215251 )
216252 } )
217- t . test ( 'cb' , t => {
218- const poop = new Error ( 'poop' )
219- t . teardown ( mutateFS . fail ( 'read' , poop ) )
253+
254+ t . test ( 'cb' , async t => {
255+ const { list } = await t . mockImport < typeof import ( '../src/list.js' ) > (
256+ '../src/list.js' ,
257+ {
258+ fs : t . createMock ( fs , {
259+ read : cbFail ,
260+ readSync : syncFail ,
261+ } ) ,
262+ 'fs/promises' : t . createMock ( fsp , {
263+ read : asyncFail ,
264+ } ) ,
265+ } ,
266+ )
220267 t . plan ( 1 )
221- list ( { file : __filename } , er => t . equal ( er , poop ) )
268+ await list ( { file : __filename } , er => t . equal ( er , poop ) )
222269 } )
223- t . test ( 'promise' , t => {
224- const poop = new Error ( 'poop' )
225- t . teardown ( mutateFS . fail ( 'read' , poop ) )
270+
271+ t . test ( 'promise' , async t => {
272+ const { list } = await t . mockImport < typeof import ( '../src/list.js' ) > (
273+ '../src/list.js' ,
274+ {
275+ fs : t . createMock ( fs , {
276+ read : cbFail ,
277+ readSync : syncFail ,
278+ } ) ,
279+ 'fs/promises' : t . createMock ( fsp , {
280+ read : asyncFail ,
281+ } ) ,
282+ } ,
283+ )
226284 t . plan ( 1 )
227- list ( { file : __filename } ) . catch ( er => t . equal ( er , poop ) )
285+ await list ( { file : __filename } ) . catch ( er => t . equal ( er , poop ) )
228286 } )
229287 t . end ( )
230288} )
0 commit comments