Debian Codegraph @

workspaces/arborist/test/get-workspace-nodes.js

raw
1 const t = require('tap')
2 const Arborist = require('../lib/arborist/index.js')
3 const { resolve } = require('node:path')
4 const path = resolve(__dirname, './fixtures/workspaces-shared-deps-virtual')
5 const arb = new Arborist({ path })
6
7 const warningTracker = () => {
8 const list = []
9 const onlog = (...msg) => msg[0] === 'warn' && list.push(msg)
10 process.on('log', onlog)
11 return () => {
12 process.removeListener('log', onlog)
13 return list
14 }
15 }
16
17 let tree
18 t.before(async () => {
19 tree = await arb.loadVirtual()
20 })
21
22 t.test('basic behavior', t => {
23 const getLogs = warningTracker()
24 const wsNodes = arb.workspaceNodes(tree, ['a'])
25 t.equal(wsNodes.length, 1)
26 t.equal(wsNodes[0], tree.children.get('a').target)
27 t.same(getLogs(), [])
28 t.end()
29 })
30
31 t.test('filter set, but no workspaces present', t => {
32 const getLogs = warningTracker()
33 const wsNodes = arb.workspaceNodes(tree.children.get('b').target, ['xyz'])
34 t.same(wsNodes, [])
35 t.same(getLogs(), [
36 ['warn', 'workspaces', 'filter set, but no workspaces present'],
37 ])
38 t.end()
39 })
40
41 t.test('name in filter set, but not in workspaces', t => {
42 const getLogs = warningTracker()
43 const wsNodes = arb.workspaceNodes(tree, ['xyz'])
44 t.same(wsNodes, [])
45 t.same(getLogs(), [
46 ['warn', 'workspaces', 'xyz in filter set, but not in workspaces'],
47 ])
48 t.end()
49 })
50
51 t.test('name in filter set, but no workspace folder present', t => {
52 const getLogs = warningTracker()
53 // damage the tree somehow. Note that the getWorkspaces() function
54 // that returns the map already *should* prevent this from happening,
55 // but if we start moving things around and make a mistake, it's
56 // possible to get there.
57 tree.children.get('c').target.root = null
58 const wsNodes = arb.workspaceNodes(tree, ['c'])
59 t.same(wsNodes, [])
60 t.same(getLogs(), [
61 ['warn', 'workspaces', 'c in filter set, but no workspace folder present'],
62 ])
63 t.end()
64 })