|
| 1 | +const { resolve } = require('path') |
| 2 | + |
| 3 | +const Arborist = require('@npmcli/arborist') |
| 4 | +const t = require('tap') |
| 5 | +const requireInject = require('require-inject') |
| 6 | + |
| 7 | +const redactCwd = (path) => { |
| 8 | + const normalizePath = p => p |
| 9 | + .replace(/\\+/g, '/') |
| 10 | + .replace(/\r\n/g, '\n') |
| 11 | + return normalizePath(path) |
| 12 | + .replace(new RegExp(normalizePath(process.cwd()), 'g'), '{CWD}') |
| 13 | +} |
| 14 | + |
| 15 | +t.cleanSnapshot = (str) => redactCwd(str) |
| 16 | + |
| 17 | +let reifyOutput |
| 18 | +const npm = { |
| 19 | + globalDir: null, |
| 20 | + prefix: null, |
| 21 | + flatOptions: {}, |
| 22 | + config: { |
| 23 | + get () { return false } |
| 24 | + } |
| 25 | +} |
| 26 | +const printLinks = async (opts) => { |
| 27 | + let res = '' |
| 28 | + const arb = new Arborist(opts) |
| 29 | + const tree = await arb.loadActual() |
| 30 | + const linkedItems = [...tree.inventory.values()] |
| 31 | + for (const item of linkedItems) { |
| 32 | + if (item.target) |
| 33 | + res += `${item.path} -> ${item.target.path}\n` |
| 34 | + } |
| 35 | + return res |
| 36 | +} |
| 37 | + |
| 38 | +const mocks = { |
| 39 | + '../../lib/npm.js': npm, |
| 40 | + '../../lib/utils/reify-output.js': () => reifyOutput() |
| 41 | +} |
| 42 | + |
| 43 | +const link = requireInject('../../lib/link.js', mocks) |
| 44 | + |
| 45 | +t.test('link to globalDir when in current working dir of pkg and no args', (t) => { |
| 46 | + t.plan(2) |
| 47 | + |
| 48 | + const testdir = t.testdir({ |
| 49 | + 'global-prefix': { |
| 50 | + lib: { |
| 51 | + node_modules: { |
| 52 | + a: { |
| 53 | + 'package.json': JSON.stringify({ |
| 54 | + name: 'a', |
| 55 | + version: '1.0.0' |
| 56 | + }) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | + 'test-pkg-link': { |
| 62 | + 'package.json': JSON.stringify({ |
| 63 | + name: 'test-pkg-link', |
| 64 | + version: '1.0.0' |
| 65 | + }) |
| 66 | + } |
| 67 | + }) |
| 68 | + npm.globalDir = resolve(testdir, 'global-prefix', 'lib', 'node_modules') |
| 69 | + npm.prefix = resolve(testdir, 'test-pkg-link') |
| 70 | + |
| 71 | + reifyOutput = async () => { |
| 72 | + reifyOutput = undefined |
| 73 | + |
| 74 | + const links = await printLinks({ |
| 75 | + path: resolve(npm.globalDir, '..'), |
| 76 | + global: true |
| 77 | + }) |
| 78 | + |
| 79 | + t.matchSnapshot(links, 'should create a global link to current pkg') |
| 80 | + } |
| 81 | + |
| 82 | + link([], (err) => { |
| 83 | + t.ifError(err, 'should not error out') |
| 84 | + }) |
| 85 | +}) |
| 86 | + |
| 87 | +t.test('link global linked pkg to local nm when using args', (t) => { |
| 88 | + t.plan(2) |
| 89 | + |
| 90 | + const testdir = t.testdir({ |
| 91 | + 'global-prefix': { |
| 92 | + lib: { |
| 93 | + node_modules: { |
| 94 | + '@myscope': { |
| 95 | + foo: { |
| 96 | + 'package.json': JSON.stringify({ |
| 97 | + name: '@myscope/foo', |
| 98 | + version: '1.0.0' |
| 99 | + }) |
| 100 | + }, |
| 101 | + bar: { |
| 102 | + 'package.json': JSON.stringify({ |
| 103 | + name: '@myscope/bar', |
| 104 | + version: '1.0.0' |
| 105 | + }) |
| 106 | + }, |
| 107 | + linked: t.fixture('symlink', '../../../../scoped-linked') |
| 108 | + }, |
| 109 | + a: { |
| 110 | + 'package.json': JSON.stringify({ |
| 111 | + name: 'a', |
| 112 | + version: '1.0.0' |
| 113 | + }) |
| 114 | + }, |
| 115 | + b: { |
| 116 | + 'package.json': JSON.stringify({ |
| 117 | + name: 'b', |
| 118 | + version: '1.0.0' |
| 119 | + }) |
| 120 | + }, |
| 121 | + 'test-pkg-link': t.fixture('symlink', '../../../test-pkg-link') |
| 122 | + } |
| 123 | + } |
| 124 | + }, |
| 125 | + 'test-pkg-link': { |
| 126 | + 'package.json': JSON.stringify({ |
| 127 | + name: 'test-pkg-link', |
| 128 | + version: '1.0.0' |
| 129 | + }) |
| 130 | + }, |
| 131 | + 'scoped-linked': { |
| 132 | + 'package.json': JSON.stringify({ |
| 133 | + name: '@myscope/linked', |
| 134 | + version: '1.0.0' |
| 135 | + }) |
| 136 | + }, |
| 137 | + 'my-project': { |
| 138 | + 'package.json': JSON.stringify({ |
| 139 | + name: 'my-project', |
| 140 | + version: '1.0.0', |
| 141 | + dependencies: { |
| 142 | + foo: '^1.0.0' |
| 143 | + } |
| 144 | + }), |
| 145 | + node_modules: { |
| 146 | + foo: { |
| 147 | + 'package.json': JSON.stringify({ |
| 148 | + name: 'foo', |
| 149 | + version: '1.0.0' |
| 150 | + }) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + }) |
| 155 | + npm.globalDir = resolve(testdir, 'global-prefix', 'lib', 'node_modules') |
| 156 | + npm.prefix = resolve(testdir, 'my-project') |
| 157 | + |
| 158 | + reifyOutput = async () => { |
| 159 | + reifyOutput = undefined |
| 160 | + |
| 161 | + const links = await printLinks({ |
| 162 | + path: npm.prefix |
| 163 | + }) |
| 164 | + |
| 165 | + t.matchSnapshot(links, 'should create a local symlink to global pkg') |
| 166 | + } |
| 167 | + |
| 168 | + // installs examples for: |
| 169 | + // - test-pkg-link: pkg linked to globalDir from local fs |
| 170 | + // - @myscope/linked: scoped pkg linked to globalDir from local fs |
| 171 | + // - @myscope/bar: prev installed scoped package available in globalDir |
| 172 | + // - a: prev installed package available in globalDir |
| 173 | + link(['test-pkg-link', '@myscope/linked', '@myscope/bar', 'a'], (err) => { |
| 174 | + t.ifError(err, 'should not error out') |
| 175 | + }) |
| 176 | +}) |
| 177 | + |
| 178 | +t.test('completion', (t) => { |
| 179 | + const testdir = t.testdir({ |
| 180 | + 'global-prefix': { |
| 181 | + lib: { |
| 182 | + node_modules: { |
| 183 | + foo: {}, |
| 184 | + bar: {}, |
| 185 | + lorem: {}, |
| 186 | + ipsum: {} |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + }) |
| 191 | + npm.globalDir = resolve(testdir, 'global-prefix', 'lib', 'node_modules') |
| 192 | + |
| 193 | + link.completion({}, (err, words) => { |
| 194 | + t.ifError(err, 'should not error out') |
| 195 | + t.deepEqual( |
| 196 | + words, |
| 197 | + ['bar', 'foo', 'ipsum', 'lorem'], |
| 198 | + 'should list all package names available in globalDir' |
| 199 | + ) |
| 200 | + t.end() |
| 201 | + }) |
| 202 | +}) |
| 203 | + |
| 204 | +t.test('--global option', (t) => { |
| 205 | + const _config = npm.config |
| 206 | + npm.config = { get () { return true } } |
| 207 | + link([], (err) => { |
| 208 | + npm.config = _config |
| 209 | + |
| 210 | + t.match( |
| 211 | + err.message, |
| 212 | + /link should never be --global/, |
| 213 | + 'should throw an useful error' |
| 214 | + ) |
| 215 | + |
| 216 | + t.end() |
| 217 | + }) |
| 218 | +}) |
0 commit comments