11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst fs = require('fs/promises')
31cb0ef41Sopenharmony_ciconst { join } = require('path')
41cb0ef41Sopenharmony_ciconst { cleanNewlines } = require('../../fixtures/clean-snapshot')
51cb0ef41Sopenharmony_ciconst tmock = require('../../fixtures/tmock')
61cb0ef41Sopenharmony_ciconst mockNpm = require('../../fixtures/mock-npm')
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// windowwwwwwssss!!!!!
91cb0ef41Sopenharmony_ciconst readRc = async (dir) => {
101cb0ef41Sopenharmony_ci  const res = await fs.readFile(join(dir, 'npmrc'), 'utf8').catch(() => '')
111cb0ef41Sopenharmony_ci  return cleanNewlines(res).trim()
121cb0ef41Sopenharmony_ci}
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst mockReififyFinish = async (t, { actualTree = {}, otherDirs = {}, ...config }) => {
151cb0ef41Sopenharmony_ci  const mock = await mockNpm(t, {
161cb0ef41Sopenharmony_ci    npm: ({ other }) => ({
171cb0ef41Sopenharmony_ci      npmRoot: other,
181cb0ef41Sopenharmony_ci    }),
191cb0ef41Sopenharmony_ci    otherDirs: {
201cb0ef41Sopenharmony_ci      npmrc: `key=value`,
211cb0ef41Sopenharmony_ci      ...otherDirs,
221cb0ef41Sopenharmony_ci    },
231cb0ef41Sopenharmony_ci    config,
241cb0ef41Sopenharmony_ci  })
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  const reifyFinish = tmock(t, '{LIB}/utils/reify-finish.js', {
271cb0ef41Sopenharmony_ci    '{LIB}/utils/reify-output.js': () => {},
281cb0ef41Sopenharmony_ci  })
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  await reifyFinish(mock.npm, {
311cb0ef41Sopenharmony_ci    options: { global: mock.npm.global },
321cb0ef41Sopenharmony_ci    actualTree: typeof actualTree === 'function' ? actualTree(mock) : actualTree,
331cb0ef41Sopenharmony_ci  })
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  const builtinRc = {
361cb0ef41Sopenharmony_ci    raw: await readRc(mock.other),
371cb0ef41Sopenharmony_ci    data: Object.fromEntries(Object.entries(mock.npm.config.data.get('builtin').data)),
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  return {
411cb0ef41Sopenharmony_ci    builtinRc,
421cb0ef41Sopenharmony_ci    ...mock,
431cb0ef41Sopenharmony_ci  }
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cit.test('ok by default', async t => {
471cb0ef41Sopenharmony_ci  const mock = await mockReififyFinish(t, {
481cb0ef41Sopenharmony_ci    global: false,
491cb0ef41Sopenharmony_ci  })
501cb0ef41Sopenharmony_ci  t.same(mock.builtinRc.raw, 'key=value')
511cb0ef41Sopenharmony_ci  t.strictSame(mock.builtinRc.data, { key: 'value' })
521cb0ef41Sopenharmony_ci})
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cit.test('should not write if no global npm module', async t => {
551cb0ef41Sopenharmony_ci  const mock = await mockReififyFinish(t, {
561cb0ef41Sopenharmony_ci    global: true,
571cb0ef41Sopenharmony_ci    actualTree: {
581cb0ef41Sopenharmony_ci      inventory: new Map(),
591cb0ef41Sopenharmony_ci    },
601cb0ef41Sopenharmony_ci  })
611cb0ef41Sopenharmony_ci  t.same(mock.builtinRc.raw, 'key=value')
621cb0ef41Sopenharmony_ci  t.strictSame(mock.builtinRc.data, { key: 'value' })
631cb0ef41Sopenharmony_ci})
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_cit.test('should not write if builtin conf had load error', async t => {
661cb0ef41Sopenharmony_ci  const mock = await mockReififyFinish(t, {
671cb0ef41Sopenharmony_ci    global: true,
681cb0ef41Sopenharmony_ci    otherDirs: {
691cb0ef41Sopenharmony_ci      npmrc: {},
701cb0ef41Sopenharmony_ci    },
711cb0ef41Sopenharmony_ci    actualTree: {
721cb0ef41Sopenharmony_ci      inventory: new Map([['node_modules/npm', {}]]),
731cb0ef41Sopenharmony_ci    },
741cb0ef41Sopenharmony_ci  })
751cb0ef41Sopenharmony_ci  t.same(mock.builtinRc.raw, '')
761cb0ef41Sopenharmony_ci  t.strictSame(mock.builtinRc.data, {})
771cb0ef41Sopenharmony_ci})
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_cit.test('should write if everything above passes', async t => {
801cb0ef41Sopenharmony_ci  const mock = await mockReififyFinish(t, {
811cb0ef41Sopenharmony_ci    global: true,
821cb0ef41Sopenharmony_ci    otherDirs: {
831cb0ef41Sopenharmony_ci      'new-npm': {},
841cb0ef41Sopenharmony_ci    },
851cb0ef41Sopenharmony_ci    actualTree: ({ other }) => ({
861cb0ef41Sopenharmony_ci      inventory: new Map([['node_modules/npm', { path: join(other, 'new-npm') }]]),
871cb0ef41Sopenharmony_ci    }),
881cb0ef41Sopenharmony_ci  })
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  t.same(mock.builtinRc.raw, 'key=value')
911cb0ef41Sopenharmony_ci  t.strictSame(mock.builtinRc.data, { key: 'value' })
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  const newFile = await readRc(join(mock.other, 'new-npm'))
941cb0ef41Sopenharmony_ci  t.equal(mock.builtinRc.raw, newFile)
951cb0ef41Sopenharmony_ci})
96