11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst fs = require('fs')
31cb0ef41Sopenharmony_ciconst { resolve } = require('path')
41cb0ef41Sopenharmony_ciconst _mockNpm = require('../../fixtures/mock-npm')
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst mockNpm = async (t, opts = {}) => {
71cb0ef41Sopenharmony_ci  const res = await _mockNpm(t, {
81cb0ef41Sopenharmony_ci    ...opts,
91cb0ef41Sopenharmony_ci    mocks: {
101cb0ef41Sopenharmony_ci      ...opts.mocks,
111cb0ef41Sopenharmony_ci      '{LIB}/utils/reify-finish.js': async () => {},
121cb0ef41Sopenharmony_ci    },
131cb0ef41Sopenharmony_ci  })
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  return {
161cb0ef41Sopenharmony_ci    ...res,
171cb0ef41Sopenharmony_ci    uninstall: (args) => res.npm.exec('uninstall', args),
181cb0ef41Sopenharmony_ci  }
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cit.test('remove single installed lib', async t => {
221cb0ef41Sopenharmony_ci  const { uninstall, prefix } = await mockNpm(t, {
231cb0ef41Sopenharmony_ci    prefixDir: {
241cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
251cb0ef41Sopenharmony_ci        name: 'test-rm-single-lib',
261cb0ef41Sopenharmony_ci        version: '1.0.0',
271cb0ef41Sopenharmony_ci        dependencies: {
281cb0ef41Sopenharmony_ci          a: '*',
291cb0ef41Sopenharmony_ci          b: '*',
301cb0ef41Sopenharmony_ci        },
311cb0ef41Sopenharmony_ci      }),
321cb0ef41Sopenharmony_ci      node_modules: {
331cb0ef41Sopenharmony_ci        a: {
341cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
351cb0ef41Sopenharmony_ci            name: 'a',
361cb0ef41Sopenharmony_ci            version: '1.0.0',
371cb0ef41Sopenharmony_ci          }),
381cb0ef41Sopenharmony_ci        },
391cb0ef41Sopenharmony_ci        b: {
401cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
411cb0ef41Sopenharmony_ci            name: 'b',
421cb0ef41Sopenharmony_ci            version: '1.0.0',
431cb0ef41Sopenharmony_ci          }),
441cb0ef41Sopenharmony_ci        },
451cb0ef41Sopenharmony_ci      },
461cb0ef41Sopenharmony_ci      'package-lock.json': JSON.stringify({
471cb0ef41Sopenharmony_ci        name: 'test-rm-single-lib',
481cb0ef41Sopenharmony_ci        version: '1.0.0',
491cb0ef41Sopenharmony_ci        lockfileVersion: 2,
501cb0ef41Sopenharmony_ci        requires: true,
511cb0ef41Sopenharmony_ci        packages: {
521cb0ef41Sopenharmony_ci          '': {
531cb0ef41Sopenharmony_ci            name: 'test-rm-single-lib',
541cb0ef41Sopenharmony_ci            version: '1.0.0',
551cb0ef41Sopenharmony_ci            dependencies: {
561cb0ef41Sopenharmony_ci              a: '*',
571cb0ef41Sopenharmony_ci            },
581cb0ef41Sopenharmony_ci          },
591cb0ef41Sopenharmony_ci          'node_modules/a': {
601cb0ef41Sopenharmony_ci            version: '1.0.0',
611cb0ef41Sopenharmony_ci          },
621cb0ef41Sopenharmony_ci          'node_modules/b': {
631cb0ef41Sopenharmony_ci            version: '1.0.0',
641cb0ef41Sopenharmony_ci          },
651cb0ef41Sopenharmony_ci        },
661cb0ef41Sopenharmony_ci        dependencies: {
671cb0ef41Sopenharmony_ci          a: {
681cb0ef41Sopenharmony_ci            version: '1.0.0',
691cb0ef41Sopenharmony_ci          },
701cb0ef41Sopenharmony_ci          b: {
711cb0ef41Sopenharmony_ci            version: '1.0.0',
721cb0ef41Sopenharmony_ci          },
731cb0ef41Sopenharmony_ci        },
741cb0ef41Sopenharmony_ci      }),
751cb0ef41Sopenharmony_ci    },
761cb0ef41Sopenharmony_ci  })
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  const b = resolve(prefix, 'node_modules/b')
791cb0ef41Sopenharmony_ci  t.ok(fs.statSync(b))
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  await uninstall(['b'])
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  t.throws(() => fs.statSync(b), 'should have removed package from npm')
841cb0ef41Sopenharmony_ci})
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_cit.test('remove multiple installed libs', async t => {
871cb0ef41Sopenharmony_ci  const { uninstall, prefix } = await mockNpm(t, {
881cb0ef41Sopenharmony_ci    prefixDir: {
891cb0ef41Sopenharmony_ci      node_modules: {
901cb0ef41Sopenharmony_ci        a: {
911cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
921cb0ef41Sopenharmony_ci            name: 'a',
931cb0ef41Sopenharmony_ci            version: '1.0.0',
941cb0ef41Sopenharmony_ci          }),
951cb0ef41Sopenharmony_ci        },
961cb0ef41Sopenharmony_ci        b: {
971cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
981cb0ef41Sopenharmony_ci            name: 'b',
991cb0ef41Sopenharmony_ci            version: '1.0.0',
1001cb0ef41Sopenharmony_ci          }),
1011cb0ef41Sopenharmony_ci        },
1021cb0ef41Sopenharmony_ci      },
1031cb0ef41Sopenharmony_ci      'package-lock.json': JSON.stringify({
1041cb0ef41Sopenharmony_ci        name: 'test-rm-single-lib',
1051cb0ef41Sopenharmony_ci        version: '1.0.0',
1061cb0ef41Sopenharmony_ci        lockfileVersion: 2,
1071cb0ef41Sopenharmony_ci        requires: true,
1081cb0ef41Sopenharmony_ci        packages: {
1091cb0ef41Sopenharmony_ci          '': {
1101cb0ef41Sopenharmony_ci            name: 'test-rm-single-lib',
1111cb0ef41Sopenharmony_ci            version: '1.0.0',
1121cb0ef41Sopenharmony_ci            dependencies: {
1131cb0ef41Sopenharmony_ci              a: '*',
1141cb0ef41Sopenharmony_ci            },
1151cb0ef41Sopenharmony_ci          },
1161cb0ef41Sopenharmony_ci          'node_modules/a': {
1171cb0ef41Sopenharmony_ci            version: '1.0.0',
1181cb0ef41Sopenharmony_ci          },
1191cb0ef41Sopenharmony_ci          'node_modules/b': {
1201cb0ef41Sopenharmony_ci            version: '1.0.0',
1211cb0ef41Sopenharmony_ci          },
1221cb0ef41Sopenharmony_ci        },
1231cb0ef41Sopenharmony_ci        dependencies: {
1241cb0ef41Sopenharmony_ci          a: {
1251cb0ef41Sopenharmony_ci            version: '1.0.0',
1261cb0ef41Sopenharmony_ci          },
1271cb0ef41Sopenharmony_ci          b: {
1281cb0ef41Sopenharmony_ci            version: '1.0.0',
1291cb0ef41Sopenharmony_ci          },
1301cb0ef41Sopenharmony_ci        },
1311cb0ef41Sopenharmony_ci      }),
1321cb0ef41Sopenharmony_ci    },
1331cb0ef41Sopenharmony_ci  })
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  const a = resolve(prefix, 'node_modules/a')
1361cb0ef41Sopenharmony_ci  const b = resolve(prefix, 'node_modules/b')
1371cb0ef41Sopenharmony_ci  t.ok(fs.statSync(a))
1381cb0ef41Sopenharmony_ci  t.ok(fs.statSync(b))
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  await uninstall(['b'])
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  t.throws(() => fs.statSync(a), 'should have removed a package from nm')
1431cb0ef41Sopenharmony_ci  t.throws(() => fs.statSync(b), 'should have removed b package from nm')
1441cb0ef41Sopenharmony_ci})
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_cit.test('no args local', async t => {
1471cb0ef41Sopenharmony_ci  const { uninstall } = await mockNpm(t)
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci  await t.rejects(
1501cb0ef41Sopenharmony_ci    uninstall([]),
1511cb0ef41Sopenharmony_ci    /Must provide a package name to remove/,
1521cb0ef41Sopenharmony_ci    'should throw package name required error'
1531cb0ef41Sopenharmony_ci  )
1541cb0ef41Sopenharmony_ci})
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_cit.test('no args global', async t => {
1571cb0ef41Sopenharmony_ci  const { uninstall, npm } = await mockNpm(t, {
1581cb0ef41Sopenharmony_ci    prefixDir: {
1591cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
1601cb0ef41Sopenharmony_ci        name: 'a',
1611cb0ef41Sopenharmony_ci        version: '1.0.0',
1621cb0ef41Sopenharmony_ci      }),
1631cb0ef41Sopenharmony_ci    },
1641cb0ef41Sopenharmony_ci    globalPrefixDir: {
1651cb0ef41Sopenharmony_ci      node_modules: {
1661cb0ef41Sopenharmony_ci        a: t.fixture('symlink', '../../prefix'),
1671cb0ef41Sopenharmony_ci      },
1681cb0ef41Sopenharmony_ci    },
1691cb0ef41Sopenharmony_ci    config: { global: true },
1701cb0ef41Sopenharmony_ci  })
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  const a = resolve(npm.globalDir, 'a')
1731cb0ef41Sopenharmony_ci  t.ok(fs.statSync(a))
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci  await uninstall([])
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci  t.throws(() => fs.statSync(a), 'should have removed global nm symlink')
1781cb0ef41Sopenharmony_ci})
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_cit.test('no args global but no package.json', async t => {
1811cb0ef41Sopenharmony_ci  const { uninstall } = await mockNpm(t, {
1821cb0ef41Sopenharmony_ci    config: { global: true },
1831cb0ef41Sopenharmony_ci  })
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci  await t.rejects(
1861cb0ef41Sopenharmony_ci    uninstall([]),
1871cb0ef41Sopenharmony_ci    /npm uninstall/
1881cb0ef41Sopenharmony_ci  )
1891cb0ef41Sopenharmony_ci})
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_cit.test('non ENOENT error reading from localPrefix package.json', async t => {
1921cb0ef41Sopenharmony_ci  const { uninstall } = await mockNpm(t, {
1931cb0ef41Sopenharmony_ci    config: { global: true },
1941cb0ef41Sopenharmony_ci    prefixDir: { 'package.json': 'not[json]' },
1951cb0ef41Sopenharmony_ci  })
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci  await t.rejects(
1981cb0ef41Sopenharmony_ci    uninstall([]),
1991cb0ef41Sopenharmony_ci    { code: 'EJSONPARSE' },
2001cb0ef41Sopenharmony_ci    'should throw non ENOENT error'
2011cb0ef41Sopenharmony_ci  )
2021cb0ef41Sopenharmony_ci})
203