|
| 1 | +const { resolve } = require('path') |
| 2 | +const t = require('tap') |
| 3 | +const requireInject = require('require-inject') |
| 4 | + |
| 5 | +const noop = () => null |
| 6 | +const npm = { |
| 7 | + globalDir: '', |
| 8 | + flatOptions: { |
| 9 | + depth: 0, |
| 10 | + global: false, |
| 11 | + }, |
| 12 | + prefix: '', |
| 13 | +} |
| 14 | +const mocks = { |
| 15 | + npmlog: { warn () {} }, |
| 16 | + '@npmcli/arborist': class { |
| 17 | + reify () {} |
| 18 | + }, |
| 19 | + '../../lib/npm.js': npm, |
| 20 | + '../../lib/utils/reify-finish.js': noop, |
| 21 | + '../../lib/utils/usage.js': () => 'usage instructions', |
| 22 | +} |
| 23 | + |
| 24 | +t.afterEach(cb => { |
| 25 | + npm.prefix = '' |
| 26 | + npm.flatOptions.global = false |
| 27 | + npm.globalDir = '' |
| 28 | + cb() |
| 29 | +}) |
| 30 | + |
| 31 | +t.test('no args', t => { |
| 32 | + t.plan(3) |
| 33 | + |
| 34 | + npm.prefix = '/project/a' |
| 35 | + |
| 36 | + class Arborist { |
| 37 | + constructor (args) { |
| 38 | + t.deepEqual( |
| 39 | + args, |
| 40 | + { ...npm.flatOptions, path: npm.prefix }, |
| 41 | + 'should call arborist contructor with expected args' |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + reify ({ update }) { |
| 46 | + t.equal(update, true, 'should update all deps') |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + const update = requireInject('../../lib/update.js', { |
| 51 | + ...mocks, |
| 52 | + '../../lib/utils/reify-finish.js': (arb) => { |
| 53 | + t.isLike(arb, Arborist, 'should reify-finish with arborist instance') |
| 54 | + }, |
| 55 | + '@npmcli/arborist': Arborist, |
| 56 | + }) |
| 57 | + |
| 58 | + update([], err => { |
| 59 | + if (err) |
| 60 | + throw err |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +t.test('with args', t => { |
| 65 | + t.plan(3) |
| 66 | + |
| 67 | + npm.prefix = '/project/a' |
| 68 | + |
| 69 | + class Arborist { |
| 70 | + constructor (args) { |
| 71 | + t.deepEqual( |
| 72 | + args, |
| 73 | + { ...npm.flatOptions, path: npm.prefix }, |
| 74 | + 'should call arborist contructor with expected args' |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + reify ({ update }) { |
| 79 | + t.deepEqual(update, ['ipt'], 'should update listed deps') |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const update = requireInject('../../lib/update.js', { |
| 84 | + ...mocks, |
| 85 | + '../../lib/utils/reify-finish.js': (arb) => { |
| 86 | + t.isLike(arb, Arborist, 'should reify-finish with arborist instance') |
| 87 | + }, |
| 88 | + '@npmcli/arborist': Arborist, |
| 89 | + }) |
| 90 | + |
| 91 | + update(['ipt'], err => { |
| 92 | + if (err) |
| 93 | + throw err |
| 94 | + }) |
| 95 | +}) |
| 96 | + |
| 97 | +t.test('update --depth=<number>', t => { |
| 98 | + t.plan(2) |
| 99 | + |
| 100 | + npm.prefix = '/project/a' |
| 101 | + npm.flatOptions.depth = 1 |
| 102 | + |
| 103 | + const update = requireInject('../../lib/update.js', { |
| 104 | + ...mocks, |
| 105 | + npmlog: { |
| 106 | + warn: (title, msg) => { |
| 107 | + t.equal(title, 'update', 'should print expected title') |
| 108 | + t.match( |
| 109 | + msg, |
| 110 | + /The --depth option no longer has any effect/, |
| 111 | + 'should print expected warning message' |
| 112 | + ) |
| 113 | + }, |
| 114 | + }, |
| 115 | + }) |
| 116 | + |
| 117 | + update([], err => { |
| 118 | + if (err) |
| 119 | + throw err |
| 120 | + }) |
| 121 | +}) |
| 122 | + |
| 123 | +t.test('update --global', t => { |
| 124 | + t.plan(2) |
| 125 | + |
| 126 | + const normalizePath = p => p.replace(/\\+/g, '/') |
| 127 | + const redactCwd = (path) => normalizePath(path) |
| 128 | + .replace(new RegExp(normalizePath(process.cwd()), 'g'), '{CWD}') |
| 129 | + |
| 130 | + npm.prefix = '/project/a' |
| 131 | + npm.globalDir = resolve(process.cwd(), 'global/lib/node_modules') |
| 132 | + npm.flatOptions.global = true |
| 133 | + |
| 134 | + class Arborist { |
| 135 | + constructor (args) { |
| 136 | + const { path, ...opts } = args |
| 137 | + t.deepEqual( |
| 138 | + opts, |
| 139 | + npm.flatOptions, |
| 140 | + 'should call arborist contructor with expected options' |
| 141 | + ) |
| 142 | + |
| 143 | + t.equal( |
| 144 | + redactCwd(path), |
| 145 | + '{CWD}/global/lib', |
| 146 | + 'should run with expected prefix' |
| 147 | + ) |
| 148 | + } |
| 149 | + |
| 150 | + reify () {} |
| 151 | + } |
| 152 | + |
| 153 | + const update = requireInject('../../lib/update.js', { |
| 154 | + ...mocks, |
| 155 | + '@npmcli/arborist': Arborist, |
| 156 | + }) |
| 157 | + |
| 158 | + update([], err => { |
| 159 | + if (err) |
| 160 | + throw err |
| 161 | + }) |
| 162 | +}) |
0 commit comments