| 1 |
const { resolve } = require('node:path') |
| 2 |
const t = require('tap') |
| 3 |
const calcDepFlags = require('../lib/calc-dep-flags.js') |
| 4 |
const Node = require('../lib/node.js') |
| 5 |
const Link = require('../lib/link.js') |
| 6 |
|
| 7 |
const { |
| 8 |
normalizePath, |
| 9 |
printTree, |
| 10 |
} = require('./fixtures/utils.js') |
| 11 |
|
| 12 |
const cwd = normalizePath(process.cwd()) |
| 13 |
t.cleanSnapshot = s => s.split(cwd).join('{CWD}') |
| 14 |
|
| 15 |
t.test('flag stuff', t => { |
| 16 |
const root = new Node({ |
| 17 |
path: '/x', |
| 18 |
realpath: '/x', |
| 19 |
pkg: { |
| 20 |
dependencies: { prod: '' }, |
| 21 |
devDependencies: { dev: '' }, |
| 22 |
optionalDependencies: { optional: '' }, |
| 23 |
peerDependencies: { peer: '', peeroptional: '' }, |
| 24 |
peerDependenciesMeta: { peeroptional: { optional: true } }, |
| 25 |
}, |
| 26 |
}) |
| 27 |
|
| 28 |
const optional = new Node({ |
| 29 |
pkg: { |
| 30 |
name: 'optional', |
| 31 |
version: '1.2.3', |
| 32 |
dependencies: { devoptional: '', missing: '' }, |
| 33 |
}, |
| 34 |
parent: root, |
| 35 |
}) |
| 36 |
|
| 37 |
const devoptional = new Node({ |
| 38 |
pkg: { |
| 39 |
name: 'devoptional', |
| 40 |
version: '1.2.3', |
| 41 |
}, |
| 42 |
parent: root, |
| 43 |
}) |
| 44 |
|
| 45 |
const extraneous = new Node({ |
| 46 |
pkg: { |
| 47 |
name: 'extraneous', |
| 48 |
}, |
| 49 |
parent: root, |
| 50 |
}) |
| 51 |
|
| 52 |
const peer = new Node({ |
| 53 |
pkg: { |
| 54 |
name: 'peer', |
| 55 |
version: '1.2.3', |
| 56 |
dependencies: { peerdep: '' }, |
| 57 |
}, |
| 58 |
parent: root, |
| 59 |
}) |
| 60 |
|
| 61 |
const peerdep = new Node({ |
| 62 |
pkg: { |
| 63 |
name: 'peerdep', |
| 64 |
version: '1.2.3', |
| 65 |
}, |
| 66 |
parent: root, |
| 67 |
}) |
| 68 |
|
| 69 |
const prod = new Node({ |
| 70 |
pkg: { |
| 71 |
name: 'prod', |
| 72 |
version: '1.2.3', |
| 73 |
dependencies: { proddep: '' }, |
| 74 |
peerDependencies: { metapeer: '' }, |
| 75 |
}, |
| 76 |
parent: root, |
| 77 |
}) |
| 78 |
|
| 79 |
const metapeer = new Node({ |
| 80 |
pkg: { |
| 81 |
name: 'metapeer', |
| 82 |
version: '1.2.3', |
| 83 |
dependencies: { metapeerdep: '' }, |
| 84 |
}, |
| 85 |
parent: root, |
| 86 |
}) |
| 87 |
|
| 88 |
const metapeerdep = new Node({ |
| 89 |
pkg: { |
| 90 |
name: 'metapeerdep', |
| 91 |
version: '1.2.3', |
| 92 |
}, |
| 93 |
parent: root, |
| 94 |
}) |
| 95 |
|
| 96 |
const proddep = new Node({ |
| 97 |
pkg: { |
| 98 |
name: 'proddep', |
| 99 |
version: '1.2.3', |
| 100 |
dependencies: { proddep: '' }, |
| 101 |
}, |
| 102 |
parent: root, |
| 103 |
}) |
| 104 |
|
| 105 |
const dev = new Node({ |
| 106 |
pkg: { |
| 107 |
name: 'dev', |
| 108 |
version: '1.2.3', |
| 109 |
dependencies: { devdep: '' }, |
| 110 |
}, |
| 111 |
parent: root, |
| 112 |
}) |
| 113 |
|
| 114 |
const devdep = new Node({ |
| 115 |
pkg: { |
| 116 |
name: 'devdep', |
| 117 |
version: '1.2.3', |
| 118 |
dependencies: { proddep: '', linky: '', devoptional: '' }, |
| 119 |
optionalDependencies: { devandoptional: '' }, |
| 120 |
}, |
| 121 |
parent: root, |
| 122 |
}) |
| 123 |
|
| 124 |
const devandoptional = new Node({ |
| 125 |
pkg: { |
| 126 |
name: 'devandoptional', |
| 127 |
version: '1.2.3', |
| 128 |
}, |
| 129 |
parent: root, |
| 130 |
}) |
| 131 |
|
| 132 |
const linky = new Link({ |
| 133 |
pkg: { |
| 134 |
name: 'linky', |
| 135 |
version: '1.2.3', |
| 136 |
dependencies: { linklink: '' }, |
| 137 |
}, |
| 138 |
realpath: '/x/y/z', |
| 139 |
parent: devdep, |
| 140 |
}) |
| 141 |
|
| 142 |
// a link dep depended upon by the target of a linked dep |
| 143 |
const linkylinky = new Link({ |
| 144 |
pkg: { |
| 145 |
name: 'linklink', |
| 146 |
version: '1.2.3', |
| 147 |
}, |
| 148 |
realpath: '/l/i/n/k/link', |
| 149 |
parent: linky.target, |
| 150 |
}) |
| 151 |
|
| 152 |
const peeroptional = new Node({ |
| 153 |
pkg: { |
| 154 |
name: 'peeroptional', |
| 155 |
version: '1.2.3', |
| 156 |
dependencies: { optional: '' }, |
| 157 |
}, |
| 158 |
parent: root, |
| 159 |
}) |
| 160 |
|
| 161 |
calcDepFlags(root) |
| 162 |
|
| 163 |
t.match(optional, { |
| 164 |
extraneous: false, |
| 165 |
dev: false, |
| 166 |
optional: true, |
| 167 |
devOptional: false, |
| 168 |
peer: false, |
| 169 |
}) |
| 170 |
t.match(devoptional, { |
| 171 |
extraneous: false, |
| 172 |
dev: false, |
| 173 |
optional: false, |
| 174 |
devOptional: true, |
| 175 |
peer: false, |
| 176 |
}) |
| 177 |
t.match(extraneous, { |
| 178 |
extraneous: true, |
| 179 |
}) |
| 180 |
t.match(peer, { |
| 181 |
extraneous: false, |
| 182 |
dev: false, |
| 183 |
optional: false, |
| 184 |
devOptional: false, |
| 185 |
peer: true, |
| 186 |
}) |
| 187 |
t.match(peerdep, { |
| 188 |
extraneous: false, |
| 189 |
dev: false, |
| 190 |
optional: false, |
| 191 |
devOptional: false, |
| 192 |
peer: true, |
| 193 |
}) |
| 194 |
t.match(prod, { |
| 195 |
extraneous: false, |
| 196 |
dev: false, |
| 197 |
optional: false, |
| 198 |
devOptional: false, |
| 199 |
peer: false, |
| 200 |
}) |
| 201 |
t.match(metapeer, { |
| 202 |
extraneous: false, |
| 203 |
dev: false, |
| 204 |
optional: false, |
| 205 |
devOptional: false, |
| 206 |
peer: true, |
| 207 |
}) |
| 208 |
t.match(metapeerdep, { |
| 209 |
extraneous: false, |
| 210 |
dev: false, |
| 211 |
optional: false, |
| 212 |
devOptional: false, |
| 213 |
peer: true, |
| 214 |
}) |
| 215 |
t.match(proddep, { |
| 216 |
extraneous: false, |
| 217 |
dev: false, |
| 218 |
optional: false, |
| 219 |
devOptional: false, |
| 220 |
peer: false, |
| 221 |
}) |
| 222 |
t.match(dev, { |
| 223 |
extraneous: false, |
| 224 |
dev: true, |
| 225 |
optional: false, |
| 226 |
devOptional: false, |
| 227 |
peer: false, |
| 228 |
}) |
| 229 |
t.match(devdep, { |
| 230 |
extraneous: false, |
| 231 |
dev: true, |
| 232 |
optional: false, |
| 233 |
devOptional: false, |
| 234 |
peer: false, |
| 235 |
}) |
| 236 |
t.match(devandoptional, { |
| 237 |
extraneous: false, |
| 238 |
dev: true, |
| 239 |
optional: true, |
| 240 |
devOptional: false, |
| 241 |
peer: false, |
| 242 |
}) |
| 243 |
t.match(linky, { |
| 244 |
extraneous: false, |
| 245 |
dev: true, |
| 246 |
optional: false, |
| 247 |
devOptional: false, |
| 248 |
peer: false, |
| 249 |
}) |
| 250 |
t.match(linkylinky, { |
| 251 |
extraneous: false, |
| 252 |
dev: true, |
| 253 |
optional: false, |
| 254 |
devOptional: false, |
| 255 |
peer: false, |
| 256 |
}) |
| 257 |
t.match(peeroptional, { |
| 258 |
extraneous: true, |
| 259 |
dev: false, |
| 260 |
optional: true, |
| 261 |
devOptional: false, |
| 262 |
peer: true, |
| 263 |
}) |
| 264 |
|
| 265 |
t.matchSnapshot(printTree(root), 'after') |
| 266 |
t.end() |
| 267 |
}) |
| 268 |
|
| 269 |
t.test('no reset', async t => { |
| 270 |
const root = new Node({ |
| 271 |
path: '/some/path', |
| 272 |
realpath: '/some/path', |
| 273 |
pkg: { |
| 274 |
dependencies: { foo: '' }, |
| 275 |
}, |
| 276 |
}) |
| 277 |
const foo = new Node({ parent: root, pkg: { name: 'foo', version: '1.2.3' } }) |
| 278 |
|
| 279 |
root.optional = false |
| 280 |
root.dev = true |
| 281 |
root.extraneous = false |
| 282 |
|
| 283 |
calcDepFlags(root, false) |
| 284 |
t.matchSnapshot(printTree(root), 'after') |
| 285 |
t.equal(root.dev, true, 'root.dev') |
| 286 |
t.equal(foo.dev, true, 'foo.dev') |
| 287 |
t.equal(root.optional, false, 'root.optional') |
| 288 |
t.equal(foo.optional, false, 'foo.optional') |
| 289 |
t.equal(root.extraneous, false, 'root.extraneous') |
| 290 |
t.equal(foo.extraneous, false, 'foo.extraneous') |
| 291 |
}) |
| 292 |
|
| 293 |
t.test('set parents to not extraneous when visiting', t => { |
| 294 |
const root = new Node({ |
| 295 |
path: '/some/path', |
| 296 |
realpath: '/some/path', |
| 297 |
pkg: { |
| 298 |
dependencies: { |
| 299 |
baz: 'file:node_modules/asdf/node_modules/baz', |
| 300 |
foo: 'file:bar/foo', |
| 301 |
}, |
| 302 |
}, |
| 303 |
}) |
| 304 |
const bar = new Node({ |
| 305 |
root, |
| 306 |
path: resolve(root.path, 'bar'), |
| 307 |
}) |
| 308 |
const foo = new Node({ |
| 309 |
root, |
| 310 |
path: resolve(bar.path, 'foo'), |
| 311 |
pkg: { name: 'foo', version: '1.2.3' }, |
| 312 |
}) |
| 313 |
const asdf = new Node({ |
| 314 |
parent: root, |
| 315 |
pkg: { name: 'asdf', version: '1.2.3' }, |
| 316 |
}) |
| 317 |
const baz = new Node({ |
| 318 |
parent: asdf, |
| 319 |
pkg: { name: 'baz', version: '1.2.3' }, |
| 320 |
}) |
| 321 |
const fooLink = new Link({ |
| 322 |
name: 'foo', |
| 323 |
target: foo, |
| 324 |
parent: root, |
| 325 |
realpath: foo.path, |
| 326 |
}) |
| 327 |
const bazLink = new Link({ |
| 328 |
name: 'baz', |
| 329 |
target: baz, |
| 330 |
parent: root, |
| 331 |
realpath: baz.path, |
| 332 |
}) |
| 333 |
|
| 334 |
calcDepFlags(root, true) |
| 335 |
|
| 336 |
t.equal(root.extraneous, false, 'root is not extraneous') |
| 337 |
t.equal(asdf.extraneous, false, 'asdf is not extraneous') |
| 338 |
t.equal(bar.extraneous, false, 'bar is not extraneous') |
| 339 |
t.equal(baz.extraneous, false, 'baz is not extraneous') |
| 340 |
t.equal(foo.extraneous, false, 'foo is not extraneous') |
| 341 |
t.equal(fooLink.extraneous, false, 'fooLink is not extraneous') |
| 342 |
t.equal(bazLink.extraneous, false, 'bazLink is not extraneous') |
| 343 |
t.end() |
| 344 |
}) |
| 345 |
|
| 346 |
t.test('check null target in link', async t => { |
| 347 |
const root = new Link({ |
| 348 |
path: '/some/path', |
| 349 |
realpath: '/some/path', |
| 350 |
pkg: { |
| 351 |
dependencies: { foo: '' }, |
| 352 |
}, |
| 353 |
}) |
| 354 |
t.doesNotThrow(() => calcDepFlags(root)) |
| 355 |
t.doesNotThrow(() => calcDepFlags(root, false)) |
| 356 |
t.end() |
| 357 |
}) |