Skip to content

Commit 55dbb99

Browse files
committed
remove some uses of mutate-fs
That module is not future-proof for ESM, or TS compatible.
1 parent 87cc309 commit 55dbb99

3 files changed

Lines changed: 89 additions & 26 deletions

File tree

src/pack.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class PackJob {
3333
import { Minipass } from 'minipass'
3434
import * as zlib from 'minizlib'
3535
import { Yallist } from 'yallist'
36-
import { ReadEntry } from './read-entry.js'
36+
import type { ReadEntry } from './read-entry.js'
3737
import type { WarnEvent } from './warn-method.js'
3838
import { warnMethod, type WarnData, type Warner } from './warn-method.js'
3939

@@ -227,10 +227,10 @@ export class Pack
227227
throw new Error('write after end')
228228
}
229229

230-
if (path instanceof ReadEntry) {
231-
this[ADDTARENTRY](path)
232-
} else {
230+
if (typeof path === 'string') {
233231
this[ADDFSENTRY](path)
232+
} else {
233+
this[ADDTARENTRY](path)
234234
}
235235
return this.flowing
236236
}

test/extract.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ const __filename = fileURLToPath(import.meta.url)
1717
const __dirname = path.dirname(__filename)
1818
const extractdir = path.resolve(__dirname, 'fixtures/extract')
1919
const tars = path.resolve(__dirname, 'fixtures/tars')
20-
//@ts-ignore
21-
import mutateFS from 'mutate-fs'
2220

2321
const tnock = (t: Test, host: string, opts?: nock.Options) => {
2422
nock.disableNetConnect()
@@ -355,15 +353,22 @@ t.test('nonexistent', async t => {
355353
await t.rejects(x({ file: 'does not exist' }))
356354
})
357355

358-
t.test('read fail', t => {
356+
t.test('read fail', async t => {
359357
const poop = new Error('poop')
360-
t.teardown(mutateFS.fail('read', poop))
358+
const { extract } = await t.mockImport<
359+
typeof import('../src/extract.js')
360+
>('../src/extract.js', {
361+
fs: t.createMock(fs, {
362+
readSync: () => {
363+
throw poop
364+
},
365+
}),
366+
})
361367

362368
t.throws(
363-
() => x({ maxReadSize: 10, sync: true, file: __filename }),
369+
() => extract({ maxReadSize: 10, sync: true, file: __filename }),
364370
poop,
365371
)
366-
t.end()
367372
})
368373

369374
t.test('sync gzip error edge case test', async t => {

test/list.ts

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Stats } from 'fs'
22
import fs, { readFileSync } from 'fs'
3-
//@ts-ignore
4-
import mutateFS from 'mutate-fs'
3+
import fsp from 'fs/promises'
54
import { dirname, resolve } from 'path'
65
import type { Test } from 'tap'
76
import t from 'tap'
@@ -14,6 +13,14 @@ import { makeTar } from './fixtures/make-tar.js'
1413
const __filename = fileURLToPath(import.meta.url)
1514
const __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+
1724
const 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

202228
t.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

Comments
 (0)