|
| 1 | +const t = require('tap') |
| 2 | +const requireInject = require('require-inject') |
| 3 | + |
| 4 | +const npm = { |
| 5 | + lockfileVersion: 2, |
| 6 | + globalDir: '', |
| 7 | + flatOptions: { |
| 8 | + depth: 0, |
| 9 | + global: false, |
| 10 | + }, |
| 11 | + prefix: '', |
| 12 | +} |
| 13 | +const tree = { |
| 14 | + meta: { |
| 15 | + hiddenLockfile: null, |
| 16 | + loadedFromDisk: false, |
| 17 | + filename: '', |
| 18 | + originalLockfileVersion: 2, |
| 19 | + save () {}, |
| 20 | + }, |
| 21 | +} |
| 22 | +const mocks = { |
| 23 | + npmlog: { notice () {} }, |
| 24 | + '@npmcli/arborist': class { |
| 25 | + loadVirtual () { |
| 26 | + return tree |
| 27 | + } |
| 28 | + |
| 29 | + loadActual () { |
| 30 | + return tree |
| 31 | + } |
| 32 | + }, |
| 33 | + '../../lib/npm.js': npm, |
| 34 | + '../../lib/utils/usage.js': () => 'usage instructions', |
| 35 | +} |
| 36 | + |
| 37 | +t.afterEach(cb => { |
| 38 | + npm.prefix = '' |
| 39 | + npm.flatOptions.global = false |
| 40 | + npm.globalDir = '' |
| 41 | + cb() |
| 42 | +}) |
| 43 | + |
| 44 | +t.test('no args', t => { |
| 45 | + t.plan(4) |
| 46 | + |
| 47 | + npm.prefix = '/project/a' |
| 48 | + |
| 49 | + class Arborist { |
| 50 | + constructor (args) { |
| 51 | + t.deepEqual( |
| 52 | + args, |
| 53 | + { ...npm.flatOptions, path: npm.prefix }, |
| 54 | + 'should call arborist contructor with expected args' |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + async loadVirtual () { |
| 59 | + t.ok('should load virtual tree') |
| 60 | + return { |
| 61 | + ...tree, |
| 62 | + meta: { |
| 63 | + ...tree.meta, |
| 64 | + save () { |
| 65 | + t.ok('should save the lockfile') |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + const npmlog = { |
| 73 | + notice (title, msg) { |
| 74 | + t.equal( |
| 75 | + msg, |
| 76 | + 'created a lockfile as npm-shrinkwrap.json', |
| 77 | + 'should log notice msg that file was successfully created' |
| 78 | + ) |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + const shrinkwrap = requireInject('../../lib/shrinkwrap.js', { |
| 83 | + ...mocks, |
| 84 | + npmlog, |
| 85 | + '@npmcli/arborist': Arborist, |
| 86 | + }) |
| 87 | + |
| 88 | + shrinkwrap([], err => { |
| 89 | + if (err) |
| 90 | + throw err |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +t.test('no virtual tree', t => { |
| 95 | + t.plan(4) |
| 96 | + |
| 97 | + npm.prefix = '/project/a' |
| 98 | + |
| 99 | + class Arborist { |
| 100 | + constructor (args) { |
| 101 | + t.deepEqual( |
| 102 | + args, |
| 103 | + { ...npm.flatOptions, path: npm.prefix }, |
| 104 | + 'should call arborist contructor with expected args' |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + async loadVirtual () { |
| 109 | + throw new Error('ERR') |
| 110 | + } |
| 111 | + |
| 112 | + async loadActual () { |
| 113 | + t.ok('should load actual tree') |
| 114 | + return { |
| 115 | + ...tree, |
| 116 | + meta: { |
| 117 | + ...tree.meta, |
| 118 | + save () { |
| 119 | + t.ok('should save the lockfile') |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + const npmlog = { |
| 127 | + notice (title, msg) { |
| 128 | + t.equal( |
| 129 | + msg, |
| 130 | + 'created a lockfile as npm-shrinkwrap.json', |
| 131 | + 'should log notice msg that file was successfully created' |
| 132 | + ) |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + const shrinkwrap = requireInject('../../lib/shrinkwrap.js', { |
| 137 | + ...mocks, |
| 138 | + npmlog, |
| 139 | + '@npmcli/arborist': Arborist, |
| 140 | + }) |
| 141 | + |
| 142 | + shrinkwrap([], err => { |
| 143 | + if (err) |
| 144 | + throw err |
| 145 | + }) |
| 146 | +}) |
| 147 | + |
| 148 | +t.test('existing package-json file', t => { |
| 149 | + t.plan(5) |
| 150 | + |
| 151 | + npm.prefix = '/project/a' |
| 152 | + |
| 153 | + class Arborist { |
| 154 | + constructor (args) { |
| 155 | + t.deepEqual( |
| 156 | + args, |
| 157 | + { ...npm.flatOptions, path: npm.prefix }, |
| 158 | + 'should call arborist contructor with expected args' |
| 159 | + ) |
| 160 | + } |
| 161 | + |
| 162 | + async loadVirtual () { |
| 163 | + t.ok('should load virtual tree') |
| 164 | + return { |
| 165 | + ...tree, |
| 166 | + meta: { |
| 167 | + hiddenLockfile: false, |
| 168 | + loadedFromDisk: true, |
| 169 | + filename: 'package-lock.json', |
| 170 | + save () { |
| 171 | + t.ok('should save the lockfile') |
| 172 | + }, |
| 173 | + }, |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + const npmlog = { |
| 179 | + notice (title, msg) { |
| 180 | + t.equal( |
| 181 | + msg, |
| 182 | + 'package-lock.json has been renamed to npm-shrinkwrap.json', |
| 183 | + 'should log notice msg that file was renamed' |
| 184 | + ) |
| 185 | + }, |
| 186 | + } |
| 187 | + |
| 188 | + const fs = { |
| 189 | + promises: { |
| 190 | + unlink (filename) { |
| 191 | + t.equal(filename, 'package-lock.json', 'should remove old lockfile') |
| 192 | + }, |
| 193 | + }, |
| 194 | + } |
| 195 | + |
| 196 | + const shrinkwrap = requireInject('../../lib/shrinkwrap.js', { |
| 197 | + ...mocks, |
| 198 | + fs, |
| 199 | + npmlog, |
| 200 | + '@npmcli/arborist': Arborist, |
| 201 | + }) |
| 202 | + |
| 203 | + shrinkwrap([], err => { |
| 204 | + if (err) |
| 205 | + throw err |
| 206 | + }) |
| 207 | +}) |
| 208 | + |
| 209 | +t.test('update shrinkwrap file version', t => { |
| 210 | + t.plan(4) |
| 211 | + |
| 212 | + npm.prefix = '/project/a' |
| 213 | + |
| 214 | + class Arborist { |
| 215 | + constructor (args) { |
| 216 | + t.deepEqual( |
| 217 | + args, |
| 218 | + { ...npm.flatOptions, path: npm.prefix }, |
| 219 | + 'should call arborist contructor with expected args' |
| 220 | + ) |
| 221 | + } |
| 222 | + |
| 223 | + async loadVirtual () { |
| 224 | + t.ok('should load virtual tree') |
| 225 | + return { |
| 226 | + ...tree, |
| 227 | + meta: { |
| 228 | + hiddenLockfile: false, |
| 229 | + loadedFromDisk: true, |
| 230 | + filename: 'npm-shrinkwrap.json', |
| 231 | + originalLockfileVersion: 1, |
| 232 | + save () { |
| 233 | + t.ok('should save the lockfile') |
| 234 | + }, |
| 235 | + }, |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + const npmlog = { |
| 241 | + notice (title, msg) { |
| 242 | + t.equal( |
| 243 | + msg, |
| 244 | + 'npm-shrinkwrap.json updated to version 2', |
| 245 | + 'should log notice msg that file was updated' |
| 246 | + ) |
| 247 | + }, |
| 248 | + } |
| 249 | + |
| 250 | + const shrinkwrap = requireInject('../../lib/shrinkwrap.js', { |
| 251 | + ...mocks, |
| 252 | + npmlog, |
| 253 | + '@npmcli/arborist': Arborist, |
| 254 | + }) |
| 255 | + |
| 256 | + shrinkwrap([], err => { |
| 257 | + if (err) |
| 258 | + throw err |
| 259 | + }) |
| 260 | +}) |
| 261 | + |
| 262 | +t.test('update to date shrinkwrap file', t => { |
| 263 | + t.plan(4) |
| 264 | + |
| 265 | + npm.prefix = '/project/a' |
| 266 | + |
| 267 | + class Arborist { |
| 268 | + constructor (args) { |
| 269 | + t.deepEqual( |
| 270 | + args, |
| 271 | + { ...npm.flatOptions, path: npm.prefix }, |
| 272 | + 'should call arborist contructor with expected args' |
| 273 | + ) |
| 274 | + } |
| 275 | + |
| 276 | + async loadVirtual () { |
| 277 | + t.ok('should load virtual tree') |
| 278 | + return { |
| 279 | + ...tree, |
| 280 | + meta: { |
| 281 | + hiddenLockfile: false, |
| 282 | + loadedFromDisk: true, |
| 283 | + filename: 'npm-shrinkwrap.json', |
| 284 | + originalLockfileVersion: 2, |
| 285 | + save () { |
| 286 | + t.ok('should save the lockfile') |
| 287 | + }, |
| 288 | + }, |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + const npmlog = { |
| 294 | + notice (title, msg) { |
| 295 | + t.equal( |
| 296 | + msg, |
| 297 | + 'npm-shrinkwrap.json up to date', |
| 298 | + 'should log notice msg shrinkwrap up to date' |
| 299 | + ) |
| 300 | + }, |
| 301 | + } |
| 302 | + |
| 303 | + const shrinkwrap = requireInject('../../lib/shrinkwrap.js', { |
| 304 | + ...mocks, |
| 305 | + npmlog, |
| 306 | + '@npmcli/arborist': Arborist, |
| 307 | + }) |
| 308 | + |
| 309 | + shrinkwrap([], err => { |
| 310 | + if (err) |
| 311 | + throw err |
| 312 | + }) |
| 313 | +}) |
| 314 | + |
| 315 | +t.test('shrinkwrap --global', t => { |
| 316 | + const shrinkwrap = requireInject('../../lib/shrinkwrap.js', mocks) |
| 317 | + |
| 318 | + npm.flatOptions.global = true |
| 319 | + |
| 320 | + shrinkwrap([], err => { |
| 321 | + t.match( |
| 322 | + err, |
| 323 | + /does not work for global packages/, |
| 324 | + 'should throw no global support msg' |
| 325 | + ) |
| 326 | + t.equal(err.code, 'ESHRINKWRAPGLOBAL', 'should throw expected error code') |
| 327 | + t.end() |
| 328 | + }) |
| 329 | +}) |
0 commit comments