Skip to content

Commit bd2b7a3

Browse files
committedApr 27, 2022
chore: remove require-inject
1 parent a359d25 commit bd2b7a3

8 files changed

+55
-63
lines changed
 

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
"devDependencies": {
7171
"@npmcli/eslint-config": "^3.0.1",
7272
"@npmcli/template-oss": "3.4.1",
73-
"require-inject": "^1.4.4",
7473
"tap": "^16.0.0"
7574
},
7675
"tap": {

‎test/content.read.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const util = require('util')
44

55
const fs = require('fs')
66
const path = require('path')
7-
const requireInject = require('require-inject')
87
const ssri = require('ssri')
98
const t = require('tap')
109
const readFile = util.promisify(fs.readFile)
@@ -20,8 +19,8 @@ const permissionError = new Error('EPERM')
2019
permissionError.code = 'EPERM'
2120

2221
// helpers
23-
const getRead = (opts) => requireInject('../lib/content/read', opts)
24-
const getReadLstatFailure = (err) => getRead({
22+
const getRead = (t, opts) => t.mock('../lib/content/read', opts)
23+
const getReadLstatFailure = (t, err) => getRead(t, {
2524
fs: Object.assign({}, require('fs'), {
2625
lstat (path, cb) {
2726
cb(err)
@@ -196,7 +195,7 @@ t.test('read: errors if content size does not match size option', function (t) {
196195
t.test('read: error while parsing provided integrity data', function (t) {
197196
const CACHE = t.testdir()
198197
const INTEGRITY = 'sha1-deadbeef'
199-
const mockedRead = getRead({
198+
const mockedRead = getRead(t, {
200199
ssri: {
201200
parse (sri) {
202201
throw genericError
@@ -217,7 +216,7 @@ t.test('read: unknown error parsing nested integrity data', function (t) {
217216
const INTEGRITY = 'sha1-deadbeef sha1-13371337'
218217

219218
// patches method in order to force a last error scenario
220-
const mockedRead = getRead({
219+
const mockedRead = getRead(t, {
221220
ssri: {
222221
parse (sri) {
223222
if (sri !== INTEGRITY) {
@@ -263,7 +262,7 @@ t.test('read: returns only first result if other hashes fails', function (t) {
263262

264263
t.test('read: opening large files', function (t) {
265264
const CACHE = t.testdir()
266-
const mockedRead = getRead({
265+
const mockedRead = getRead(t, {
267266
fs: Object.assign({}, require('fs'), {
268267
lstat (path, cb) {
269268
cb(null, { size: Number.MAX_SAFE_INTEGER })
@@ -295,7 +294,7 @@ t.test('read.sync: unknown error parsing nested integrity data', (t) => {
295294
const INTEGRITY = 'sha1-deadbeef sha1-13371337'
296295

297296
// patches method in order to force a last error scenario
298-
const mockedRead = getRead({
297+
const mockedRead = getRead(t, {
299298
ssri: {
300299
parse (sri) {
301300
if (sri !== INTEGRITY) {
@@ -374,7 +373,7 @@ t.test('hasContent: tests content existence', (t) => {
374373
t.test('hasContent: permission error', (t) => {
375374
const CACHE = t.testdir()
376375
// setup a syntetic permission error
377-
const mockedRead = getReadLstatFailure(permissionError)
376+
const mockedRead = getReadLstatFailure(t, permissionError)
378377

379378
t.plan(1)
380379
t.rejects(
@@ -386,7 +385,7 @@ t.test('hasContent: permission error', (t) => {
386385

387386
t.test('hasContent: generic error', (t) => {
388387
const CACHE = t.testdir()
389-
const mockedRead = getReadLstatFailure(genericError)
388+
const mockedRead = getReadLstatFailure(t, genericError)
390389

391390
t.plan(1)
392391
t.resolves(
@@ -430,7 +429,7 @@ t.test('hasContent.sync: checks content existence synchronously', (t) => {
430429

431430
t.test('hasContent.sync: permission error', (t) => {
432431
const CACHE = t.testdir()
433-
const mockedRead = getReadLstatFailure(permissionError)
432+
const mockedRead = getReadLstatFailure(t, permissionError)
434433

435434
t.throws(
436435
() => mockedRead.hasContent.sync(CACHE, 'sha1-deadbeef sha1-13371337'),
@@ -442,7 +441,7 @@ t.test('hasContent.sync: permission error', (t) => {
442441

443442
t.test('hasContent.sync: generic error', (t) => {
444443
const CACHE = t.testdir()
445-
const mockedRead = getReadLstatFailure(genericError)
444+
const mockedRead = getReadLstatFailure(t, genericError)
446445

447446
t.notOk(
448447
mockedRead.hasContent.sync(CACHE, 'sha1-deadbeef sha1-13371337'),

‎test/content.write.chownr.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ process.getuid = () => 0
88
const path = require('path')
99
const fs = require('fs')
1010

11-
const requireInject = require('require-inject')
1211
const ssri = require('ssri')
1312
const t = require('tap')
1413

@@ -39,7 +38,7 @@ t.test('infers ownership from cache folder owner', (t) => {
3938
const CONTENT = 'foobarbaz'
4039
const INTEGRITY = ssri.fromData(CONTENT)
4140
const updatedPaths = []
42-
const write = requireInject('../lib/content/write', {
41+
const write = t.mock('../lib/content/write', {
4342
chownr: function (p, uid, gid, cb) {
4443
process.nextTick(function () {
4544
const rel = path.relative(CACHE, p)

‎test/entry-index.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const path = require('path')
55

66
const ssri = require('ssri')
77
const t = require('tap')
8-
const requireInject = require('require-inject')
98

109
const index = require('../lib/entry-index')
1110
const CacheContent = require('./util/cache-content')
@@ -16,8 +15,8 @@ genericError.code = 'ERR'
1615
const missingFileError = new Error('ENOENT')
1716
missingFileError.code = 'ENOENT'
1817

19-
const getEntryIndex = (opts) => requireInject('../lib/entry-index', opts)
20-
const getEntryIndexReadFileFailure = (err) => getEntryIndex({
18+
const getEntryIndex = (t, opts) => t.mock('../lib/entry-index', opts)
19+
const getEntryIndexReadFileFailure = (t, err) => getEntryIndex(t, {
2120
fs: Object.assign({}, fs, {
2221
readFile: (path, encode, cb) => {
2322
cb(err)
@@ -33,7 +32,7 @@ const getEntryIndexFixOwnerFailure = (err) => {
3332
chownr.sync = () => {
3433
throw err
3534
}
36-
return getEntryIndex({
35+
return getEntryIndex(t, {
3736
'../lib/util/fix-owner': {
3837
mkdirfix: require('../lib/util/fix-owner').mkdirfix,
3938
chownr,
@@ -179,7 +178,7 @@ t.test('compact: error in moveFile removes temp', async (t) => {
179178
index.insert(cache, KEY, INTEGRITY, { metadata: { rev: 1 } }),
180179
])
181180

182-
const { compact } = getEntryIndex({
181+
const { compact } = getEntryIndex(t, {
183182
'@npmcli/move-file': () => Promise.reject(new Error('foo')),
184183
})
185184
const filter = (entryA, entryB) => entryA.metadata.rev === entryB.metadata.rev
@@ -257,7 +256,7 @@ t.test('delete: removeFully deletes the index entirely', async (t) => {
257256
t.test('find: error on parsing json data', (t) => {
258257
const cache = t.testdir(cacheContent)
259258
// mocks readFile in order to return a borked json payload
260-
const { find } = getEntryIndex({
259+
const { find } = getEntryIndex(t, {
261260
fs: Object.assign({}, require('fs'), {
262261
readFile: (path, encode, cb) => {
263262
cb(null, '\ncec8d2e4685534ed189b563c8ee1cb1cb7c72874\t{"""// foo')
@@ -275,7 +274,7 @@ t.test('find: error on parsing json data', (t) => {
275274

276275
t.test('find: unknown error on finding entries', (t) => {
277276
const cache = t.testdir(cacheContent)
278-
const { find } = getEntryIndexReadFileFailure(genericError)
277+
const { find } = getEntryIndexReadFileFailure(t, genericError)
279278

280279
t.plan(1)
281280
t.rejects(
@@ -291,7 +290,7 @@ t.test('find.sync: retrieve from bucket containing multiple entries', (t) => {
291290
'\na7eb00332fe51ff62b1bdb1564855f2624f16f34\t{"key":"foo", "integrity": "foo"}',
292291
'\n46b1607f427665a99668c02d3a4cc52061afd83a\t{"key":"bar", "integrity": "bar"}',
293292
]
294-
const { find } = getEntryIndex({
293+
const { find } = getEntryIndex(t, {
295294
fs: Object.assign({}, require('fs'), {
296295
readFileSync: (path, encode) => entries.join(''),
297296
}),
@@ -307,7 +306,7 @@ t.test('find.sync: retrieve from bucket containing multiple entries', (t) => {
307306

308307
t.test('find.sync: unknown error on finding entries', (t) => {
309308
const cache = t.testdir(cacheContent)
310-
const { find } = getEntryIndexReadFileFailure(genericError)
309+
const { find } = getEntryIndexReadFileFailure(t, genericError)
311310

312311
t.throws(
313312
() => find.sync(cache, KEY),
@@ -319,7 +318,7 @@ t.test('find.sync: unknown error on finding entries', (t) => {
319318

320319
t.test('find.sync: retrieve entry with invalid content', (t) => {
321320
const cache = t.testdir(cacheContent)
322-
const { find } = getEntryIndex({
321+
const { find } = getEntryIndex(t, {
323322
fs: Object.assign({}, require('fs'), {
324323
readFileSync: (path, encode) =>
325324
'\nb6589fc6ab0dc82cf12099d1c2d40ab994e8410c\t0',
@@ -384,7 +383,7 @@ t.test('lsStream: unknown error reading files', (t) => {
384383
const cache = t.testdir(cacheContent)
385384
index.insert.sync(cache, KEY, INTEGRITY)
386385

387-
const { lsStream } = getEntryIndexReadFileFailure(genericError)
386+
const { lsStream } = getEntryIndexReadFileFailure(t, genericError)
388387

389388
lsStream(cache)
390389
.on('error', err => {
@@ -397,7 +396,7 @@ t.test('lsStream: missing files error', (t) => {
397396
const cache = t.testdir(cacheContent)
398397
index.insert.sync(cache, KEY, INTEGRITY)
399398

400-
const { lsStream } = getEntryIndexReadFileFailure(missingFileError)
399+
const { lsStream } = getEntryIndexReadFileFailure(t, missingFileError)
401400

402401
lsStream(cache)
403402
.on('error', () => {
@@ -412,7 +411,7 @@ t.test('lsStream: missing files error', (t) => {
412411

413412
t.test('lsStream: unknown error reading dirs', (t) => {
414413
const cache = t.testdir(cacheContent)
415-
const { lsStream } = getEntryIndex({
414+
const { lsStream } = getEntryIndex(t, {
416415
fs: Object.assign({}, require('fs'), {
417416
readdir: (path, cb) => {
418417
cb(genericError)

‎test/util.fix-owner.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const os = require('os')
44

55
const t = require('tap')
6-
const requireInject = require('require-inject')
76
const uniqueFilename = require('unique-filename')
87

98
// defines reusable errors
@@ -24,13 +23,13 @@ const patchesGetuid = (t) => {
2423
process.getuid = getuid
2524
})
2625
}
27-
const getFixOwner = (opts) => requireInject('../lib/util/fix-owner', opts)
26+
const getFixOwner = (t, opts) => t.mock('../lib/util/fix-owner', opts)
2827

2928
// chownr and chownr.fix error handling tests
3029

3130
t.test('attempt to chownr existing path', (t) => {
3231
patchesGetuid(t)
33-
const fixOwner = getFixOwner({
32+
const fixOwner = getFixOwner(t, {
3433
chownr: function chownr (path, uid, gid, cb) {
3534
cb(missingFileError)
3635
},
@@ -46,7 +45,7 @@ t.test('attempt to chownr existing path', (t) => {
4645

4746
t.test('attempt to chownr unknown error', (t) => {
4847
patchesGetuid(t)
49-
const fixOwner = getFixOwner({
48+
const fixOwner = getFixOwner(t, {
5049
chownr: function chownr (path, uid, gid, cb) {
5150
cb(genericError)
5251
},
@@ -59,7 +58,7 @@ t.test('attempt to chownr unknown error', (t) => {
5958

6059
t.test('attempt to chownr using same user', (t) => {
6160
patchesGetuid(t)
62-
const fixOwner = getFixOwner({
61+
const fixOwner = getFixOwner(t, {
6362
'infer-owner': () => Promise.resolve({
6463
uid: process.getuid(),
6564
gid: process.getgid(),
@@ -84,7 +83,7 @@ t.test('calls setuid setgid to replace user', (t) => {
8483
process.stuid = setuid
8584
process.stgid = setgid
8685
})
87-
const fixOwner = getFixOwner({
86+
const fixOwner = getFixOwner(t, {
8887
'infer-owner': () => {
8988
process.setuid(process.getuid())
9089
process.setgid(process.getgid())
@@ -122,7 +121,7 @@ t.test('attempt to chownr.sync existing path', (t) => {
122121
chownr.sync = () => {
123122
throw missingFileError
124123
}
125-
const fixOwner = getFixOwner({
124+
const fixOwner = getFixOwner(t, {
126125
chownr,
127126
'infer-owner': { sync: () => ({}) },
128127
})
@@ -137,7 +136,7 @@ t.test('attempt to chownr.sync unknown error', (t) => {
137136
chownr.sync = () => {
138137
throw genericError
139138
}
140-
const fixOwner = getFixOwner({
139+
const fixOwner = getFixOwner(t, {
141140
chownr,
142141
'infer-owner': { sync: () => ({}) },
143142
})
@@ -148,7 +147,7 @@ t.test('attempt to chownr.sync unknown error', (t) => {
148147

149148
t.test('attempt to chownr.sync using same user', (t) => {
150149
patchesGetuid(t)
151-
const fixOwner = getFixOwner({
150+
const fixOwner = getFixOwner(t, {
152151
'infer-owner': {
153152
sync: () => ({
154153
uid: process.getuid(),
@@ -187,7 +186,7 @@ t.test('uses infer-owner ids instead of process-retrieved if valid', (t) => {
187186
t.equal(uid, 501, 'should match uid')
188187
t.equal(gid, 20, 'should match gid')
189188
}
190-
const fixOwner = getFixOwner({
189+
const fixOwner = getFixOwner(t, {
191190
chownr,
192191
'infer-owner': {
193192
sync: () => ({
@@ -203,7 +202,7 @@ t.test('uses infer-owner ids instead of process-retrieved if valid', (t) => {
203202
// mkdirfix and mkdirfix.sync error handling tests
204203

205204
t.test('attempt to mkdirfix existing path', (t) => {
206-
const fixOwner = getFixOwner({
205+
const fixOwner = getFixOwner(t, {
207206
mkdirp: () => Promise.reject(pathExistsError),
208207
})
209208

@@ -215,7 +214,7 @@ t.test('attempt to mkdirfix existing path', (t) => {
215214
})
216215

217216
t.test('attempt to mkdirfix unknown error', (t) => {
218-
const fixOwner = getFixOwner({
217+
const fixOwner = getFixOwner(t, {
219218
mkdirp: () => Promise.reject(genericError),
220219
})
221220

@@ -228,7 +227,7 @@ t.test('attempt to mkdirfix.sync existing path', (t) => {
228227
mkdirp.sync = () => {
229228
throw pathExistsError
230229
}
231-
const fixOwner = getFixOwner({ mkdirp })
230+
const fixOwner = getFixOwner(t, { mkdirp })
232231

233232
t.notOk(fixOwner.mkdirfix.sync(CACHE, filename), 'should not throw if path exists')
234233
t.end()
@@ -239,7 +238,7 @@ t.test('attempt to mkdirfix.sync unknown error', (t) => {
239238
mkdirp.sync = () => {
240239
throw genericError
241240
}
242-
const fixOwner = getFixOwner({ mkdirp })
241+
const fixOwner = getFixOwner(t, { mkdirp })
243242

244243
t.throws(
245244
() => fixOwner.mkdirfix.sync(CACHE, filename),
@@ -252,7 +251,7 @@ t.test('attempt to mkdirfix.sync unknown error', (t) => {
252251
t.test('attempt to mkdirfix.sync but no dir created', (t) => {
253252
function mkdirp () {}
254253
mkdirp.sync = () => {}
255-
const fixOwner = getFixOwner({ mkdirp })
254+
const fixOwner = getFixOwner(t, { mkdirp })
256255

257256
t.notOk(fixOwner.mkdirfix.sync(CACHE, filename), 'should not throw')
258257
t.end()

‎test/util.move-file.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const util = require('util')
44

55
const fs = require('fs')
66
const path = require('path')
7-
const requireInject = require('require-inject')
8-
const { test } = require('tap')
7+
const t = require('tap')
98

109
const moveFile = require('../lib/util/move-file')
1110

@@ -16,7 +15,7 @@ const close = util.promisify(fs.close)
1615
const readdir = util.promisify(fs.readdir)
1716
const chmod = util.promisify(fs.chmod)
1817

19-
test('move a file', function (t) {
18+
t.test('move a file', function (t) {
2019
const testDir = t.testdir({
2120
src: 'foo',
2221
})
@@ -33,7 +32,7 @@ test('move a file', function (t) {
3332
})
3433
})
3534

36-
test('does not clobber existing files', function (t) {
35+
t.test('does not clobber existing files', function (t) {
3736
const testDir = t.testdir({
3837
src: 'foo',
3938
dest: 'bar',
@@ -51,7 +50,7 @@ test('does not clobber existing files', function (t) {
5150
})
5251
})
5352

54-
test('does not move a file into an existing directory', function (t) {
53+
t.test('does not move a file into an existing directory', function (t) {
5554
const testDir = t.testdir({
5655
src: 'foo',
5756
dest: {},
@@ -65,7 +64,7 @@ test('does not move a file into an existing directory', function (t) {
6564
})
6665
})
6766

68-
test('does not error if destination file is open', function (t) {
67+
t.test('does not error if destination file is open', function (t) {
6968
const testDir = t.testdir({
7069
src: 'foo',
7170
dest: 'bar',
@@ -89,7 +88,7 @@ test('does not error if destination file is open', function (t) {
8988
})
9089
})
9190

92-
test('fallback to renaming on missing files post-move', function (t) {
91+
t.test('fallback to renaming on missing files post-move', function (t) {
9392
const testDir = t.testdir({
9493
src: 'foo',
9594
})
@@ -168,9 +167,9 @@ test('fallback to renaming on missing files post-move', function (t) {
168167
}
169168
},
170169
}
171-
const mockedMoveFile = requireInject.withEmptyCache('../lib/util/move-file', {
170+
const mockedMoveFile = t.mock('../lib/util/move-file', {
172171
fs: mockFS,
173-
'@npmcli/move-file': requireInject.withEmptyCache('@npmcli/move-file', {
172+
'@npmcli/move-file': t.mock('@npmcli/move-file', {
174173
fs: mockFS,
175174
}),
176175
})
@@ -193,7 +192,7 @@ test('fallback to renaming on missing files post-move', function (t) {
193192
})
194193
})
195194

196-
test('verify weird EPERM on Windows behavior', t => {
195+
t.test('verify weird EPERM on Windows behavior', t => {
197196
const gfsLink = fs.link
198197
global.__CACACHE_TEST_FAKE_WINDOWS__ = true
199198
const gfs = require('fs')
@@ -222,7 +221,7 @@ test('verify weird EPERM on Windows behavior', t => {
222221
}, 'src file did get deleted'))
223222
})
224223

225-
test(
224+
t.test(
226225
'errors if dest is not writable',
227226
{
228227
skip: process.platform === 'win32',

‎test/util.tmp.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const util = require('util')
4-
const requireInject = require('require-inject')
54

65
const fs = require('fs')
76
const path = require('path')
@@ -12,7 +11,7 @@ const CACHE = t.testdir()
1211
const mockedFixOwner = () => Promise.resolve(1)
1312
// temporarily points to original mkdirfix implementation
1413
mockedFixOwner.mkdirfix = require('../lib/util/fix-owner').mkdirfix
15-
const tmp = requireInject('../lib/util/tmp', {
14+
const tmp = t.mock('../lib/util/tmp', {
1615
'../lib/util/fix-owner': mockedFixOwner,
1716
})
1817

‎test/verify.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const contentPath = require('../lib/content/path')
66
const index = require('../lib/entry-index')
77
const fs = require('fs')
88
const path = require('path')
9-
const requireInject = require('require-inject')
109
const t = require('tap')
1110
const ssri = require('ssri')
1211

@@ -30,7 +29,7 @@ const genericError = new Error('ERR')
3029
genericError.code = 'ERR'
3130

3231
// helpers
33-
const getVerify = (opts) => requireInject('../lib/verify', opts)
32+
const getVerify = (t, opts) => t.mock('../lib/verify', opts)
3433

3534
function mockCache (t) {
3635
const cacheContent = CacheContent({
@@ -267,7 +266,7 @@ t.test('fixes permissions and users on cache contents')
267266
t.test('missing file error when validating cache content', (t) => {
268267
const missingFileError = new Error('ENOENT')
269268
missingFileError.code = 'ENOENT'
270-
const mockVerify = getVerify({
269+
const mockVerify = getVerify(t, {
271270
fs: Object.assign({}, fs, {
272271
stat: (path, cb) => {
273272
cb(missingFileError)
@@ -290,7 +289,7 @@ t.test('missing file error when validating cache content', (t) => {
290289
})
291290

292291
t.test('unknown error when validating content', (t) => {
293-
const mockVerify = getVerify({
292+
const mockVerify = getVerify(t, {
294293
fs: Object.assign({}, fs, {
295294
stat: (path, cb) => {
296295
cb(genericError)
@@ -309,7 +308,7 @@ t.test('unknown error when validating content', (t) => {
309308
})
310309

311310
t.test('unknown error when checking sri stream', (t) => {
312-
const mockVerify = getVerify({
311+
const mockVerify = getVerify(t, {
313312
ssri: Object.assign({}, ssri, {
314313
checkStream: () => Promise.reject(genericError),
315314
}),
@@ -329,7 +328,7 @@ t.test('unknown error when rebuilding bucket', (t) => {
329328
// rebuild bucket uses stat after content-validation
330329
// shouldFail controls the right time to mock the error
331330
let shouldFail = false
332-
const mockVerify = getVerify({
331+
const mockVerify = getVerify(t, {
333332
fs: Object.assign({}, fs, {
334333
stat: (path, cb) => {
335334
if (shouldFail) {
@@ -401,7 +400,7 @@ t.test('re-builds the index with the size parameter', (t) => {
401400
})
402401

403402
t.test('hash collisions', (t) => {
404-
const mockVerify = getVerify({
403+
const mockVerify = getVerify(t, {
405404
'../lib/entry-index': Object.assign({}, index, {
406405
hashKey: () => 'aaa',
407406
}),
@@ -432,7 +431,7 @@ t.test('hash collisions', (t) => {
432431
})
433432

434433
t.test('hash collisions excluded', (t) => {
435-
const mockVerify = getVerify({
434+
const mockVerify = getVerify(t, {
436435
'../lib/entry-index': Object.assign({}, index, {
437436
hashKey: () => 'aaa',
438437
}),

0 commit comments

Comments
 (0)
Please sign in to comment.