Skip to content

Commit

Permalink
addressing comments, fix sbom, revert server, cleanup refiy fs mock
Browse files Browse the repository at this point in the history
  • Loading branch information
reggi committed May 28, 2024
1 parent e3a33ee commit a5fb49f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
2 changes: 1 addition & 1 deletion test/lib/commands/sbom.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const mockSbom = async (t, { mocks, config, ...opts } = {}) => {
},
command: 'sbom',
mocks: {
path: {
'node:path': {
...require('node:path'),
sep: '/',
},
Expand Down
7 changes: 4 additions & 3 deletions workspaces/arborist/test/arborist/rebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ const server = require('node:http').createServer(() => {
throw new Error('rebuild should not hit the registry')
})
t.before(() => new Promise(res => {
server.listen(PORT, res)
server.listen(PORT, () => {
t.teardown(() => server.close())
res()
})
}))

t.teardown(() => server.close())

const registry = `http://localhost:${PORT}`
const newArb = opt => new Arborist({ ...opt, registry })

Expand Down
38 changes: 5 additions & 33 deletions workspaces/arborist/test/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,19 @@ let failRm = false
let failRename = null
let failRenameOnce = null
let failMkdir = null
const { rename: realRename, rm: realRm, mkdir: realMkdir } = fs
const fsMock = {
...fs,
mkdir (...args) {
if (failMkdir) {
process.nextTick(() => args.pop()(failMkdir))
return
}

return realMkdir(...args)
},
rename (...args) {
if (failRename) {
process.nextTick(() => args.pop()(failRename))
} else if (failRenameOnce) {
const er = failRenameOnce
failRenameOnce = null
process.nextTick(() => args.pop()(er))
} else {
return realRename(...args)
}
},
rm (...args) {
if (failRm) {
process.nextTick(() => args.pop()(new Error('rm fail')))
return
}

realRm(...args)
},
}
const fspMock = {
...fsp,
mkdir: async (...args) => {
fs.appendFileSync('reggi-log.txt', 'fspNodeMock.mkdir\n')
if (failMkdir) {
throw failMkdir
}

return fsp.mkdir(...args)
},
rename: async (...args) => {
fs.appendFileSync('reggi-log.txt', 'fspNodeMock.rename\n')
if (failRename) {
throw failRename
} else if (failRenameOnce) {
Expand All @@ -63,19 +35,19 @@ const fspMock = {
}
},
rm: async (...args) => {
fs.appendFileSync('reggi-log.txt', 'fspNodeMock.rm\n')
if (failRm) {
throw new Error('rm fail')
}

return fsp.rm(...args)
},
}

// need this to be injected so that it doesn't pull from main cache
const { moveFile } = t.mock('@npmcli/fs', { 'node:fs/promises': fspMock, 'fs/promises': fspMock })
const { moveFile } = t.mock('@npmcli/fs', { 'fs/promises': fspMock })
const mocks = {
fs: fsMock,
'node:fs/promises': fspMock,
'fs/promises': fspMock,
'@npmcli/fs': { ...npmFs, moveFile },
}

Expand Down

0 comments on commit a5fb49f

Please sign in to comment.