11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst { resolve, join } = require('path')
31cb0ef41Sopenharmony_ciconst fs = require('fs')
41cb0ef41Sopenharmony_ciconst Arborist = require('@npmcli/arborist')
51cb0ef41Sopenharmony_ciconst { cleanCwd } = require('../../fixtures/clean-snapshot.js')
61cb0ef41Sopenharmony_ciconst mockNpm = require('../../fixtures/mock-npm')
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cit.cleanSnapshot = (str) => cleanCwd(str)
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst mockLink = async (t, { globalPrefixDir, ...opts } = {}) => {
111cb0ef41Sopenharmony_ci  const mock = await mockNpm(t, {
121cb0ef41Sopenharmony_ci    ...opts,
131cb0ef41Sopenharmony_ci    command: 'link',
141cb0ef41Sopenharmony_ci    globalPrefixDir,
151cb0ef41Sopenharmony_ci    mocks: {
161cb0ef41Sopenharmony_ci      ...opts.mocks,
171cb0ef41Sopenharmony_ci      '{LIB}/utils/reify-output.js': async () => {},
181cb0ef41Sopenharmony_ci    },
191cb0ef41Sopenharmony_ci  })
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  const printLinks = async ({ global = false } = {}) => {
221cb0ef41Sopenharmony_ci    let res = ''
231cb0ef41Sopenharmony_ci    const arb = new Arborist(global ? {
241cb0ef41Sopenharmony_ci      path: resolve(mock.npm.globalDir, '..'),
251cb0ef41Sopenharmony_ci      global: true,
261cb0ef41Sopenharmony_ci    } : { path: mock.prefix })
271cb0ef41Sopenharmony_ci    const tree = await arb.loadActual()
281cb0ef41Sopenharmony_ci    const linkedItems = [...tree.inventory.values()]
291cb0ef41Sopenharmony_ci      .sort((a, b) => a.pkgid.localeCompare(b.pkgid, 'en'))
301cb0ef41Sopenharmony_ci    for (const item of linkedItems) {
311cb0ef41Sopenharmony_ci      if (item.isLink) {
321cb0ef41Sopenharmony_ci        res += `${item.path} -> ${item.target.path}\n`
331cb0ef41Sopenharmony_ci      }
341cb0ef41Sopenharmony_ci    }
351cb0ef41Sopenharmony_ci    return res
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  return {
391cb0ef41Sopenharmony_ci    ...mock,
401cb0ef41Sopenharmony_ci    printLinks,
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cit.test('link to globalDir when in current working dir of pkg and no args', async t => {
451cb0ef41Sopenharmony_ci  const { link, printLinks } = await mockLink(t, {
461cb0ef41Sopenharmony_ci    globalPrefixDir: {
471cb0ef41Sopenharmony_ci      node_modules: {
481cb0ef41Sopenharmony_ci        a: {
491cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
501cb0ef41Sopenharmony_ci            name: 'a',
511cb0ef41Sopenharmony_ci            version: '1.0.0',
521cb0ef41Sopenharmony_ci          }),
531cb0ef41Sopenharmony_ci        },
541cb0ef41Sopenharmony_ci      },
551cb0ef41Sopenharmony_ci    },
561cb0ef41Sopenharmony_ci    prefixDir: {
571cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
581cb0ef41Sopenharmony_ci        name: 'test-pkg-link',
591cb0ef41Sopenharmony_ci        version: '1.0.0',
601cb0ef41Sopenharmony_ci      }),
611cb0ef41Sopenharmony_ci    },
621cb0ef41Sopenharmony_ci  })
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  await link.exec()
651cb0ef41Sopenharmony_ci  t.matchSnapshot(await printLinks({ global: true }), 'should create a global link to current pkg')
661cb0ef41Sopenharmony_ci})
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cit.test('link ws to globalDir when workspace specified and no args', async t => {
691cb0ef41Sopenharmony_ci  const { link, printLinks } = await mockLink(t, {
701cb0ef41Sopenharmony_ci    globalPrefixDir: {
711cb0ef41Sopenharmony_ci      node_modules: {
721cb0ef41Sopenharmony_ci        a: {
731cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
741cb0ef41Sopenharmony_ci            name: 'a',
751cb0ef41Sopenharmony_ci            version: '1.0.0',
761cb0ef41Sopenharmony_ci          }),
771cb0ef41Sopenharmony_ci        },
781cb0ef41Sopenharmony_ci      },
791cb0ef41Sopenharmony_ci    },
801cb0ef41Sopenharmony_ci    prefixDir: {
811cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
821cb0ef41Sopenharmony_ci        name: 'test-pkg-link',
831cb0ef41Sopenharmony_ci        version: '1.0.0',
841cb0ef41Sopenharmony_ci        workspaces: ['packages/*'],
851cb0ef41Sopenharmony_ci      }),
861cb0ef41Sopenharmony_ci      packages: {
871cb0ef41Sopenharmony_ci        a: {
881cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
891cb0ef41Sopenharmony_ci            name: 'a',
901cb0ef41Sopenharmony_ci            version: '1.0.0',
911cb0ef41Sopenharmony_ci          }),
921cb0ef41Sopenharmony_ci        },
931cb0ef41Sopenharmony_ci      },
941cb0ef41Sopenharmony_ci    },
951cb0ef41Sopenharmony_ci    config: { workspace: 'a' },
961cb0ef41Sopenharmony_ci  })
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  await link.exec()
991cb0ef41Sopenharmony_ci  t.matchSnapshot(await printLinks({ global: true }), 'should create a global link to current pkg')
1001cb0ef41Sopenharmony_ci})
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_cit.test('link global linked pkg to local nm when using args', async t => {
1031cb0ef41Sopenharmony_ci  const { link, printLinks } = await mockLink(t, {
1041cb0ef41Sopenharmony_ci    globalPrefixDir: {
1051cb0ef41Sopenharmony_ci      node_modules: {
1061cb0ef41Sopenharmony_ci        '@myscope': {
1071cb0ef41Sopenharmony_ci          foo: {
1081cb0ef41Sopenharmony_ci            'package.json': JSON.stringify({
1091cb0ef41Sopenharmony_ci              name: '@myscope/foo',
1101cb0ef41Sopenharmony_ci              version: '1.0.0',
1111cb0ef41Sopenharmony_ci            }),
1121cb0ef41Sopenharmony_ci          },
1131cb0ef41Sopenharmony_ci          bar: {
1141cb0ef41Sopenharmony_ci            'package.json': JSON.stringify({
1151cb0ef41Sopenharmony_ci              name: '@myscope/bar',
1161cb0ef41Sopenharmony_ci              version: '1.0.0',
1171cb0ef41Sopenharmony_ci            }),
1181cb0ef41Sopenharmony_ci          },
1191cb0ef41Sopenharmony_ci          linked: t.fixture('symlink', '../../../other/scoped-linked'),
1201cb0ef41Sopenharmony_ci        },
1211cb0ef41Sopenharmony_ci        a: {
1221cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
1231cb0ef41Sopenharmony_ci            name: 'a',
1241cb0ef41Sopenharmony_ci            version: '1.0.0',
1251cb0ef41Sopenharmony_ci          }),
1261cb0ef41Sopenharmony_ci        },
1271cb0ef41Sopenharmony_ci        b: {
1281cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
1291cb0ef41Sopenharmony_ci            name: 'b',
1301cb0ef41Sopenharmony_ci            version: '1.0.0',
1311cb0ef41Sopenharmony_ci          }),
1321cb0ef41Sopenharmony_ci        },
1331cb0ef41Sopenharmony_ci        'test-pkg-link': t.fixture('symlink', '../../other/test-pkg-link'),
1341cb0ef41Sopenharmony_ci      },
1351cb0ef41Sopenharmony_ci    },
1361cb0ef41Sopenharmony_ci    otherDirs: {
1371cb0ef41Sopenharmony_ci      'test-pkg-link': {
1381cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
1391cb0ef41Sopenharmony_ci          name: 'test-pkg-link',
1401cb0ef41Sopenharmony_ci          version: '1.0.0',
1411cb0ef41Sopenharmony_ci        }),
1421cb0ef41Sopenharmony_ci      },
1431cb0ef41Sopenharmony_ci      'link-me-too': {
1441cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
1451cb0ef41Sopenharmony_ci          name: 'link-me-too',
1461cb0ef41Sopenharmony_ci          version: '1.0.0',
1471cb0ef41Sopenharmony_ci        }),
1481cb0ef41Sopenharmony_ci      },
1491cb0ef41Sopenharmony_ci      'scoped-linked': {
1501cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
1511cb0ef41Sopenharmony_ci          name: '@myscope/linked',
1521cb0ef41Sopenharmony_ci          version: '1.0.0',
1531cb0ef41Sopenharmony_ci        }),
1541cb0ef41Sopenharmony_ci      },
1551cb0ef41Sopenharmony_ci    },
1561cb0ef41Sopenharmony_ci    prefixDir: {
1571cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
1581cb0ef41Sopenharmony_ci        name: 'my-project',
1591cb0ef41Sopenharmony_ci        version: '1.0.0',
1601cb0ef41Sopenharmony_ci        dependencies: {
1611cb0ef41Sopenharmony_ci          foo: '^1.0.0',
1621cb0ef41Sopenharmony_ci        },
1631cb0ef41Sopenharmony_ci      }),
1641cb0ef41Sopenharmony_ci      node_modules: {
1651cb0ef41Sopenharmony_ci        foo: {
1661cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
1671cb0ef41Sopenharmony_ci            name: 'foo',
1681cb0ef41Sopenharmony_ci            version: '1.0.0',
1691cb0ef41Sopenharmony_ci          }),
1701cb0ef41Sopenharmony_ci        },
1711cb0ef41Sopenharmony_ci      },
1721cb0ef41Sopenharmony_ci    },
1731cb0ef41Sopenharmony_ci  })
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci  // installs examples for:
1761cb0ef41Sopenharmony_ci  // - test-pkg-link: pkg linked to globalDir from local fs
1771cb0ef41Sopenharmony_ci  // - @myscope/linked: scoped pkg linked to globalDir from local fs
1781cb0ef41Sopenharmony_ci  // - @myscope/bar: prev installed scoped package available in globalDir
1791cb0ef41Sopenharmony_ci  // - a: prev installed package available in globalDir
1801cb0ef41Sopenharmony_ci  // - file:./link-me-too: pkg that needs to be reified in globalDir first
1811cb0ef41Sopenharmony_ci  await link.exec([
1821cb0ef41Sopenharmony_ci    'test-pkg-link',
1831cb0ef41Sopenharmony_ci    '@myscope/linked',
1841cb0ef41Sopenharmony_ci    '@myscope/bar',
1851cb0ef41Sopenharmony_ci    'a',
1861cb0ef41Sopenharmony_ci    'file:../other/link-me-too',
1871cb0ef41Sopenharmony_ci  ])
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci  t.matchSnapshot(await printLinks(), 'should create a local symlink to global pkg')
1901cb0ef41Sopenharmony_ci})
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_cit.test('link global linked pkg to local workspace using args', async t => {
1931cb0ef41Sopenharmony_ci  const { link, printLinks } = await mockLink(t, {
1941cb0ef41Sopenharmony_ci    globalPrefixDir: {
1951cb0ef41Sopenharmony_ci      node_modules: {
1961cb0ef41Sopenharmony_ci        '@myscope': {
1971cb0ef41Sopenharmony_ci          foo: {
1981cb0ef41Sopenharmony_ci            'package.json': JSON.stringify({
1991cb0ef41Sopenharmony_ci              name: '@myscope/foo',
2001cb0ef41Sopenharmony_ci              version: '1.0.0',
2011cb0ef41Sopenharmony_ci            }),
2021cb0ef41Sopenharmony_ci          },
2031cb0ef41Sopenharmony_ci          bar: {
2041cb0ef41Sopenharmony_ci            'package.json': JSON.stringify({
2051cb0ef41Sopenharmony_ci              name: '@myscope/bar',
2061cb0ef41Sopenharmony_ci              version: '1.0.0',
2071cb0ef41Sopenharmony_ci            }),
2081cb0ef41Sopenharmony_ci          },
2091cb0ef41Sopenharmony_ci          linked: t.fixture('symlink', '../../../other/scoped-linked'),
2101cb0ef41Sopenharmony_ci        },
2111cb0ef41Sopenharmony_ci        a: {
2121cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
2131cb0ef41Sopenharmony_ci            name: 'a',
2141cb0ef41Sopenharmony_ci            version: '1.0.0',
2151cb0ef41Sopenharmony_ci          }),
2161cb0ef41Sopenharmony_ci        },
2171cb0ef41Sopenharmony_ci        b: {
2181cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
2191cb0ef41Sopenharmony_ci            name: 'b',
2201cb0ef41Sopenharmony_ci            version: '1.0.0',
2211cb0ef41Sopenharmony_ci          }),
2221cb0ef41Sopenharmony_ci        },
2231cb0ef41Sopenharmony_ci        'test-pkg-link': t.fixture('symlink', '../../other/test-pkg-link'),
2241cb0ef41Sopenharmony_ci      },
2251cb0ef41Sopenharmony_ci    },
2261cb0ef41Sopenharmony_ci    otherDirs: {
2271cb0ef41Sopenharmony_ci      'test-pkg-link': {
2281cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
2291cb0ef41Sopenharmony_ci          name: 'test-pkg-link',
2301cb0ef41Sopenharmony_ci          version: '1.0.0',
2311cb0ef41Sopenharmony_ci        }),
2321cb0ef41Sopenharmony_ci      },
2331cb0ef41Sopenharmony_ci      'link-me-too': {
2341cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
2351cb0ef41Sopenharmony_ci          name: 'link-me-too',
2361cb0ef41Sopenharmony_ci          version: '1.0.0',
2371cb0ef41Sopenharmony_ci        }),
2381cb0ef41Sopenharmony_ci      },
2391cb0ef41Sopenharmony_ci      'scoped-linked': {
2401cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
2411cb0ef41Sopenharmony_ci          name: '@myscope/linked',
2421cb0ef41Sopenharmony_ci          version: '1.0.0',
2431cb0ef41Sopenharmony_ci        }),
2441cb0ef41Sopenharmony_ci      },
2451cb0ef41Sopenharmony_ci    },
2461cb0ef41Sopenharmony_ci    prefixDir: {
2471cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
2481cb0ef41Sopenharmony_ci        name: 'my-project',
2491cb0ef41Sopenharmony_ci        version: '1.0.0',
2501cb0ef41Sopenharmony_ci        workspaces: ['packages/*'],
2511cb0ef41Sopenharmony_ci      }),
2521cb0ef41Sopenharmony_ci      packages: {
2531cb0ef41Sopenharmony_ci        x: {
2541cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
2551cb0ef41Sopenharmony_ci            name: 'x',
2561cb0ef41Sopenharmony_ci            version: '1.0.0',
2571cb0ef41Sopenharmony_ci            dependencies: {
2581cb0ef41Sopenharmony_ci              foo: '^1.0.0',
2591cb0ef41Sopenharmony_ci            },
2601cb0ef41Sopenharmony_ci          }),
2611cb0ef41Sopenharmony_ci        },
2621cb0ef41Sopenharmony_ci      },
2631cb0ef41Sopenharmony_ci      node_modules: {
2641cb0ef41Sopenharmony_ci        foo: {
2651cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
2661cb0ef41Sopenharmony_ci            name: 'foo',
2671cb0ef41Sopenharmony_ci            version: '1.0.0',
2681cb0ef41Sopenharmony_ci          }),
2691cb0ef41Sopenharmony_ci        },
2701cb0ef41Sopenharmony_ci      },
2711cb0ef41Sopenharmony_ci    },
2721cb0ef41Sopenharmony_ci    config: { workspace: 'x' },
2731cb0ef41Sopenharmony_ci  })
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci  // installs examples for:
2761cb0ef41Sopenharmony_ci  // - test-pkg-link: pkg linked to globalDir from local fs
2771cb0ef41Sopenharmony_ci  // - @myscope/linked: scoped pkg linked to globalDir from local fs
2781cb0ef41Sopenharmony_ci  // - @myscope/bar: prev installed scoped package available in globalDir
2791cb0ef41Sopenharmony_ci  // - a: prev installed package available in globalDir
2801cb0ef41Sopenharmony_ci  // - file:./link-me-too: pkg that needs to be reified in globalDir first
2811cb0ef41Sopenharmony_ci  await link.exec([
2821cb0ef41Sopenharmony_ci    'test-pkg-link',
2831cb0ef41Sopenharmony_ci    '@myscope/linked',
2841cb0ef41Sopenharmony_ci    '@myscope/bar',
2851cb0ef41Sopenharmony_ci    'a',
2861cb0ef41Sopenharmony_ci    'file:../other/link-me-too',
2871cb0ef41Sopenharmony_ci  ])
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci  t.matchSnapshot(await printLinks(), 'should create a local symlink to global pkg')
2901cb0ef41Sopenharmony_ci})
2911cb0ef41Sopenharmony_ci
2921cb0ef41Sopenharmony_cit.test('link pkg already in global space', async t => {
2931cb0ef41Sopenharmony_ci  const { npm, link, printLinks, prefix } = await mockLink(t, {
2941cb0ef41Sopenharmony_ci    globalPrefixDir: {
2951cb0ef41Sopenharmony_ci      node_modules: {
2961cb0ef41Sopenharmony_ci        '@myscope': {
2971cb0ef41Sopenharmony_ci          linked: t.fixture('symlink', '../../../other/scoped-linked'),
2981cb0ef41Sopenharmony_ci        },
2991cb0ef41Sopenharmony_ci      },
3001cb0ef41Sopenharmony_ci    },
3011cb0ef41Sopenharmony_ci    otherDirs: {
3021cb0ef41Sopenharmony_ci      'scoped-linked': {
3031cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
3041cb0ef41Sopenharmony_ci          name: '@myscope/linked',
3051cb0ef41Sopenharmony_ci          version: '1.0.0',
3061cb0ef41Sopenharmony_ci        }),
3071cb0ef41Sopenharmony_ci      },
3081cb0ef41Sopenharmony_ci    },
3091cb0ef41Sopenharmony_ci    prefixDir: {
3101cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
3111cb0ef41Sopenharmony_ci        name: 'my-project',
3121cb0ef41Sopenharmony_ci        version: '1.0.0',
3131cb0ef41Sopenharmony_ci      }),
3141cb0ef41Sopenharmony_ci    },
3151cb0ef41Sopenharmony_ci  })
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci  // XXX: how to convert this to a config that gets passed in?
3181cb0ef41Sopenharmony_ci  npm.config.find = () => 'default'
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ci  await link.exec(['@myscope/linked'])
3211cb0ef41Sopenharmony_ci
3221cb0ef41Sopenharmony_ci  t.equal(
3231cb0ef41Sopenharmony_ci    require(resolve(prefix, 'package.json')).dependencies,
3241cb0ef41Sopenharmony_ci    undefined,
3251cb0ef41Sopenharmony_ci    'should not save to package.json upon linking'
3261cb0ef41Sopenharmony_ci  )
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ci  t.matchSnapshot(await printLinks(), 'should create a local symlink to global pkg')
3291cb0ef41Sopenharmony_ci})
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_cit.test('link pkg already in global space when prefix is a symlink', async t => {
3321cb0ef41Sopenharmony_ci  const { npm, link, printLinks, prefix } = await mockLink(t, {
3331cb0ef41Sopenharmony_ci    globalPrefixDir: t.fixture('symlink', './other/real-global-prefix'),
3341cb0ef41Sopenharmony_ci    otherDirs: {
3351cb0ef41Sopenharmony_ci      // mockNpm does this automatically but only for globalPrefixDir so here we
3361cb0ef41Sopenharmony_ci      // need to do it manually since we are making a symlink somewhere else
3371cb0ef41Sopenharmony_ci      'real-global-prefix': mockNpm.setGlobalNodeModules({
3381cb0ef41Sopenharmony_ci        node_modules: {
3391cb0ef41Sopenharmony_ci          '@myscope': {
3401cb0ef41Sopenharmony_ci            linked: t.fixture('symlink', '../../../scoped-linked'),
3411cb0ef41Sopenharmony_ci          },
3421cb0ef41Sopenharmony_ci        },
3431cb0ef41Sopenharmony_ci      }),
3441cb0ef41Sopenharmony_ci      'scoped-linked': {
3451cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
3461cb0ef41Sopenharmony_ci          name: '@myscope/linked',
3471cb0ef41Sopenharmony_ci          version: '1.0.0',
3481cb0ef41Sopenharmony_ci        }),
3491cb0ef41Sopenharmony_ci      },
3501cb0ef41Sopenharmony_ci    },
3511cb0ef41Sopenharmony_ci    prefixDir: {
3521cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
3531cb0ef41Sopenharmony_ci        name: 'my-project',
3541cb0ef41Sopenharmony_ci        version: '1.0.0',
3551cb0ef41Sopenharmony_ci      }),
3561cb0ef41Sopenharmony_ci    },
3571cb0ef41Sopenharmony_ci  })
3581cb0ef41Sopenharmony_ci
3591cb0ef41Sopenharmony_ci  npm.config.find = () => 'default'
3601cb0ef41Sopenharmony_ci
3611cb0ef41Sopenharmony_ci  await link.exec(['@myscope/linked'])
3621cb0ef41Sopenharmony_ci
3631cb0ef41Sopenharmony_ci  t.equal(
3641cb0ef41Sopenharmony_ci    require(resolve(prefix, 'package.json')).dependencies,
3651cb0ef41Sopenharmony_ci    undefined,
3661cb0ef41Sopenharmony_ci    'should not save to package.json upon linking'
3671cb0ef41Sopenharmony_ci  )
3681cb0ef41Sopenharmony_ci
3691cb0ef41Sopenharmony_ci  t.matchSnapshot(await printLinks(), 'should create a local symlink to global pkg')
3701cb0ef41Sopenharmony_ci})
3711cb0ef41Sopenharmony_ci
3721cb0ef41Sopenharmony_cit.test('should not save link to package file', async t => {
3731cb0ef41Sopenharmony_ci  const { link, prefix } = await mockLink(t, {
3741cb0ef41Sopenharmony_ci    globalPrefixDir: {
3751cb0ef41Sopenharmony_ci      node_modules: {
3761cb0ef41Sopenharmony_ci        '@myscope': {
3771cb0ef41Sopenharmony_ci          linked: t.fixture('symlink', '../../../other/scoped-linked'),
3781cb0ef41Sopenharmony_ci        },
3791cb0ef41Sopenharmony_ci      },
3801cb0ef41Sopenharmony_ci    },
3811cb0ef41Sopenharmony_ci    otherDirs: {
3821cb0ef41Sopenharmony_ci      'scoped-linked': {
3831cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
3841cb0ef41Sopenharmony_ci          name: '@myscope/linked',
3851cb0ef41Sopenharmony_ci          version: '1.0.0',
3861cb0ef41Sopenharmony_ci        }),
3871cb0ef41Sopenharmony_ci      },
3881cb0ef41Sopenharmony_ci    },
3891cb0ef41Sopenharmony_ci    prefixDir: {
3901cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
3911cb0ef41Sopenharmony_ci        name: 'my-project',
3921cb0ef41Sopenharmony_ci        version: '1.0.0',
3931cb0ef41Sopenharmony_ci      }),
3941cb0ef41Sopenharmony_ci    },
3951cb0ef41Sopenharmony_ci    config: { save: false },
3961cb0ef41Sopenharmony_ci  })
3971cb0ef41Sopenharmony_ci
3981cb0ef41Sopenharmony_ci  await link.exec(['@myscope/linked'])
3991cb0ef41Sopenharmony_ci  t.match(
4001cb0ef41Sopenharmony_ci    require(resolve(prefix, 'package.json')).dependencies,
4011cb0ef41Sopenharmony_ci    undefined,
4021cb0ef41Sopenharmony_ci    'should not save to package.json upon linking'
4031cb0ef41Sopenharmony_ci  )
4041cb0ef41Sopenharmony_ci})
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_cit.test('should not prune dependencies when linking packages', async t => {
4071cb0ef41Sopenharmony_ci  const { link, prefix } = await mockLink(t, {
4081cb0ef41Sopenharmony_ci    globalPrefixDir: {
4091cb0ef41Sopenharmony_ci      node_modules: {
4101cb0ef41Sopenharmony_ci        linked: t.fixture('symlink', '../../other/linked'),
4111cb0ef41Sopenharmony_ci      },
4121cb0ef41Sopenharmony_ci    },
4131cb0ef41Sopenharmony_ci    otherDirs: {
4141cb0ef41Sopenharmony_ci      linked: {
4151cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
4161cb0ef41Sopenharmony_ci          name: 'linked',
4171cb0ef41Sopenharmony_ci          version: '1.0.0',
4181cb0ef41Sopenharmony_ci        }),
4191cb0ef41Sopenharmony_ci      },
4201cb0ef41Sopenharmony_ci    },
4211cb0ef41Sopenharmony_ci    prefixDir: {
4221cb0ef41Sopenharmony_ci      node_modules: {
4231cb0ef41Sopenharmony_ci        foo: {
4241cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({ name: 'foo', version: '1.0.0' }),
4251cb0ef41Sopenharmony_ci        },
4261cb0ef41Sopenharmony_ci      },
4271cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
4281cb0ef41Sopenharmony_ci        name: 'my-project',
4291cb0ef41Sopenharmony_ci        version: '1.0.0',
4301cb0ef41Sopenharmony_ci      }),
4311cb0ef41Sopenharmony_ci    },
4321cb0ef41Sopenharmony_ci  })
4331cb0ef41Sopenharmony_ci
4341cb0ef41Sopenharmony_ci  await link.exec(['linked'])
4351cb0ef41Sopenharmony_ci
4361cb0ef41Sopenharmony_ci  t.ok(
4371cb0ef41Sopenharmony_ci    fs.statSync(resolve(prefix, 'node_modules/foo')),
4381cb0ef41Sopenharmony_ci    'should not prune any extraneous dep when running npm link'
4391cb0ef41Sopenharmony_ci  )
4401cb0ef41Sopenharmony_ci})
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_cit.test('completion', async t => {
4431cb0ef41Sopenharmony_ci  const { link } = await mockLink(t, {
4441cb0ef41Sopenharmony_ci    globalPrefixDir: {
4451cb0ef41Sopenharmony_ci      node_modules: {
4461cb0ef41Sopenharmony_ci        foo: {},
4471cb0ef41Sopenharmony_ci        bar: {},
4481cb0ef41Sopenharmony_ci        lorem: {},
4491cb0ef41Sopenharmony_ci        ipsum: {},
4501cb0ef41Sopenharmony_ci      },
4511cb0ef41Sopenharmony_ci    },
4521cb0ef41Sopenharmony_ci  })
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_ci  const words = await link.completion({})
4551cb0ef41Sopenharmony_ci
4561cb0ef41Sopenharmony_ci  t.same(
4571cb0ef41Sopenharmony_ci    words,
4581cb0ef41Sopenharmony_ci    ['bar', 'foo', 'ipsum', 'lorem'],
4591cb0ef41Sopenharmony_ci    'should list all package names available in globalDir'
4601cb0ef41Sopenharmony_ci  )
4611cb0ef41Sopenharmony_ci})
4621cb0ef41Sopenharmony_ci
4631cb0ef41Sopenharmony_cit.test('--global option', async t => {
4641cb0ef41Sopenharmony_ci  const { link } = await mockLink(t, {
4651cb0ef41Sopenharmony_ci    config: { global: true },
4661cb0ef41Sopenharmony_ci  })
4671cb0ef41Sopenharmony_ci  await t.rejects(
4681cb0ef41Sopenharmony_ci    link.exec([]),
4691cb0ef41Sopenharmony_ci    /link should never be --global/,
4701cb0ef41Sopenharmony_ci    'should throw an useful error'
4711cb0ef41Sopenharmony_ci  )
4721cb0ef41Sopenharmony_ci})
4731cb0ef41Sopenharmony_ci
4741cb0ef41Sopenharmony_cit.test('hash character in working directory path', async t => {
4751cb0ef41Sopenharmony_ci  const { link, printLinks } = await mockLink(t, {
4761cb0ef41Sopenharmony_ci    globalPrefixDir: {
4771cb0ef41Sopenharmony_ci      node_modules: {
4781cb0ef41Sopenharmony_ci        a: {
4791cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
4801cb0ef41Sopenharmony_ci            name: 'a',
4811cb0ef41Sopenharmony_ci            version: '1.0.0',
4821cb0ef41Sopenharmony_ci          }),
4831cb0ef41Sopenharmony_ci        },
4841cb0ef41Sopenharmony_ci      },
4851cb0ef41Sopenharmony_ci    },
4861cb0ef41Sopenharmony_ci    otherDirs: {
4871cb0ef41Sopenharmony_ci      'i_like_#_in_my_paths': {
4881cb0ef41Sopenharmony_ci        'test-pkg-link': {
4891cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
4901cb0ef41Sopenharmony_ci            name: 'test-pkg-link',
4911cb0ef41Sopenharmony_ci            version: '1.0.0',
4921cb0ef41Sopenharmony_ci          }),
4931cb0ef41Sopenharmony_ci        },
4941cb0ef41Sopenharmony_ci      },
4951cb0ef41Sopenharmony_ci    },
4961cb0ef41Sopenharmony_ci    chdir: ({ other }) => join(other, 'i_like_#_in_my_paths', 'test-pkg-link'),
4971cb0ef41Sopenharmony_ci  })
4981cb0ef41Sopenharmony_ci  await link.exec([])
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ci  t.matchSnapshot(await printLinks({ global: true }),
5011cb0ef41Sopenharmony_ci    'should create a global link to current pkg, even within path with hash')
5021cb0ef41Sopenharmony_ci})
5031cb0ef41Sopenharmony_ci
5041cb0ef41Sopenharmony_cit.test('test linked installed as symlinks', async t => {
5051cb0ef41Sopenharmony_ci  const { link, prefix, printLinks } = await mockLink(t, {
5061cb0ef41Sopenharmony_ci    otherDirs: {
5071cb0ef41Sopenharmony_ci      mylink: {
5081cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
5091cb0ef41Sopenharmony_ci          name: 'mylink',
5101cb0ef41Sopenharmony_ci          version: '1.0.0',
5111cb0ef41Sopenharmony_ci        }),
5121cb0ef41Sopenharmony_ci      },
5131cb0ef41Sopenharmony_ci    },
5141cb0ef41Sopenharmony_ci  })
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_ci  await link.exec([
5171cb0ef41Sopenharmony_ci    join('file:../other/mylink'),
5181cb0ef41Sopenharmony_ci  ])
5191cb0ef41Sopenharmony_ci
5201cb0ef41Sopenharmony_ci  t.ok(fs.lstatSync(join(prefix, 'node_modules', 'mylink')).isSymbolicLink(),
5211cb0ef41Sopenharmony_ci    'linked path should by symbolic link'
5221cb0ef41Sopenharmony_ci  )
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ci  t.matchSnapshot(await printLinks(), 'linked package should not be installed')
5251cb0ef41Sopenharmony_ci})
526