1const t = require('tap') 2const fs = require('fs/promises') 3const { join } = require('path') 4const { cleanNewlines } = require('../../fixtures/clean-snapshot') 5const tmock = require('../../fixtures/tmock') 6const mockNpm = require('../../fixtures/mock-npm') 7 8// windowwwwwwssss!!!!! 9const readRc = async (dir) => { 10 const res = await fs.readFile(join(dir, 'npmrc'), 'utf8').catch(() => '') 11 return cleanNewlines(res).trim() 12} 13 14const mockReififyFinish = async (t, { actualTree = {}, otherDirs = {}, ...config }) => { 15 const mock = await mockNpm(t, { 16 npm: ({ other }) => ({ 17 npmRoot: other, 18 }), 19 otherDirs: { 20 npmrc: `key=value`, 21 ...otherDirs, 22 }, 23 config, 24 }) 25 26 const reifyFinish = tmock(t, '{LIB}/utils/reify-finish.js', { 27 '{LIB}/utils/reify-output.js': () => {}, 28 }) 29 30 await reifyFinish(mock.npm, { 31 options: { global: mock.npm.global }, 32 actualTree: typeof actualTree === 'function' ? actualTree(mock) : actualTree, 33 }) 34 35 const builtinRc = { 36 raw: await readRc(mock.other), 37 data: Object.fromEntries(Object.entries(mock.npm.config.data.get('builtin').data)), 38 } 39 40 return { 41 builtinRc, 42 ...mock, 43 } 44} 45 46t.test('ok by default', async t => { 47 const mock = await mockReififyFinish(t, { 48 global: false, 49 }) 50 t.same(mock.builtinRc.raw, 'key=value') 51 t.strictSame(mock.builtinRc.data, { key: 'value' }) 52}) 53 54t.test('should not write if no global npm module', async t => { 55 const mock = await mockReififyFinish(t, { 56 global: true, 57 actualTree: { 58 inventory: new Map(), 59 }, 60 }) 61 t.same(mock.builtinRc.raw, 'key=value') 62 t.strictSame(mock.builtinRc.data, { key: 'value' }) 63}) 64 65t.test('should not write if builtin conf had load error', async t => { 66 const mock = await mockReififyFinish(t, { 67 global: true, 68 otherDirs: { 69 npmrc: {}, 70 }, 71 actualTree: { 72 inventory: new Map([['node_modules/npm', {}]]), 73 }, 74 }) 75 t.same(mock.builtinRc.raw, '') 76 t.strictSame(mock.builtinRc.data, {}) 77}) 78 79t.test('should write if everything above passes', async t => { 80 const mock = await mockReififyFinish(t, { 81 global: true, 82 otherDirs: { 83 'new-npm': {}, 84 }, 85 actualTree: ({ other }) => ({ 86 inventory: new Map([['node_modules/npm', { path: join(other, 'new-npm') }]]), 87 }), 88 }) 89 90 t.same(mock.builtinRc.raw, 'key=value') 91 t.strictSame(mock.builtinRc.data, { key: 'value' }) 92 93 const newFile = await readRc(join(mock.other, 'new-npm')) 94 t.equal(mock.builtinRc.raw, newFile) 95}) 96