| 1 |
const t = require('tap') |
| 2 |
const { resolve } = require('node:path') |
| 3 |
|
| 4 |
// start from clean slate |
| 5 |
delete process.env.ARBORIST_DEBUG |
| 6 |
delete process.env.NODE_DEBUG |
| 7 |
delete process.env.npm_lifecycle_event |
| 8 |
delete process.env.npm_package_name |
| 9 |
|
| 10 |
let isTTY = false |
| 11 |
Object.defineProperty(process.stderr, 'isTTY', { |
| 12 |
get: () => isTTY, |
| 13 |
configurable: true, |
| 14 |
enumerable: true, |
| 15 |
}) |
| 16 |
|
| 17 |
const LOGS = [] |
| 18 |
console.error = (...msg) => LOGS.push(msg) |
| 19 |
|
| 20 |
t.test('debug does nothing when ARBORIST_DEBUG explicitly 0', t => { |
| 21 |
process.env.ARBORIST_DEBUG = '0' |
| 22 |
const debug = t.mock('../lib/debug.js') |
| 23 |
debug(() => { |
| 24 |
throw new Error('this should not happen') |
| 25 |
}) |
| 26 |
debug.log('hello') |
| 27 |
t.strictSame(LOGS, []) |
| 28 |
delete process.env.ARBORIST_DEBUG |
| 29 |
t.end() |
| 30 |
}) |
| 31 |
|
| 32 |
t.test('debug does nothing outside arborist folder', t => { |
| 33 |
const dir = resolve(__dirname) |
| 34 |
const cwd = process.cwd() |
| 35 |
t.teardown(() => process.chdir(cwd)) |
| 36 |
process.chdir(dir) |
| 37 |
const debug = t.mock('../lib/debug.js') |
| 38 |
debug.log('hello') |
| 39 |
t.strictSame(LOGS, []) |
| 40 |
debug(() => { |
| 41 |
throw new Error('this should not happen') |
| 42 |
}) |
| 43 |
t.end() |
| 44 |
}) |
| 45 |
|
| 46 |
t.test('debug runs when cwd is arborist folder, no TTY coloring', t => { |
| 47 |
const dir = resolve(__dirname, '..') |
| 48 |
const cwd = process.cwd() |
| 49 |
t.teardown(() => process.chdir(cwd)) |
| 50 |
process.chdir(dir) |
| 51 |
const debug = t.mock('../lib/debug.js') |
| 52 |
isTTY = false |
| 53 |
t.plan(2) |
| 54 |
debug(() => { |
| 55 |
t.pass('called debug function') |
| 56 |
}) |
| 57 |
debug.log('hello') |
| 58 |
t.strictSame(LOGS, [[process.pid + ' hello']]) |
| 59 |
LOGS.length = 0 |
| 60 |
}) |
| 61 |
|
| 62 |
t.test('debug runs when cwd is arborist folder, no TTY coloring', t => { |
| 63 |
const dir = resolve(__dirname, '..') |
| 64 |
const cwd = process.cwd() |
| 65 |
t.teardown(() => process.chdir(cwd)) |
| 66 |
process.chdir(dir) |
| 67 |
isTTY = true |
| 68 |
const debug = t.mock('../lib/debug.js') |
| 69 |
t.plan(2) |
| 70 |
debug(() => { |
| 71 |
t.pass('called debug function') |
| 72 |
}) |
| 73 |
debug.log('hello') |
| 74 |
t.strictSame(LOGS, [[process.pid + ' \u001b[31mhello\u001b[39m']]) |
| 75 |
LOGS.length = 0 |
| 76 |
}) |
| 77 |
|
| 78 |
t.test('debug runs when NODE_DEBUG contains "arborist"', t => { |
| 79 |
t.teardown(() => { |
| 80 |
delete process.env.NODE_DEBUG |
| 81 |
}) |
| 82 |
process.env.NODE_DEBUG = 'fooo, bar, arborist, etc' |
| 83 |
const debug = t.mock('../lib/debug.js') |
| 84 |
t.plan(1) |
| 85 |
debug(() => { |
| 86 |
t.pass('called debug function') |
| 87 |
}) |
| 88 |
}) |
| 89 |
|
| 90 |
t.test('debug runs when ARBORIST_DEBUG=1', t => { |
| 91 |
t.teardown(() => { |
| 92 |
delete process.env.ARBORIST_DEBUG |
| 93 |
}) |
| 94 |
process.env.ARBORIST_DEBUG = '1' |
| 95 |
const debug = t.mock('../lib/debug.js') |
| 96 |
t.plan(1) |
| 97 |
debug(() => { |
| 98 |
t.pass('called debug function') |
| 99 |
}) |
| 100 |
}) |
| 101 |
|
| 102 |
t.test('debug runs when testing arborist', t => { |
| 103 |
t.teardown(() => { |
| 104 |
delete process.env.npm_lifecycle_event |
| 105 |
delete process.env.npm_package_name |
| 106 |
}) |
| 107 |
process.env.npm_lifecycle_event = 'test' |
| 108 |
process.env.npm_package_name = '@npmcli/arborist' |
| 109 |
const debug = t.mock('../lib/debug.js') |
| 110 |
t.plan(1) |
| 111 |
debug(() => { |
| 112 |
t.pass('called debug function') |
| 113 |
}) |
| 114 |
}) |
| 115 |
|
| 116 |
t.test('debug runs when generating snapshots', t => { |
| 117 |
t.teardown(() => { |
| 118 |
delete process.env.npm_lifecycle_event |
| 119 |
delete process.env.npm_package_name |
| 120 |
}) |
| 121 |
process.env.npm_lifecycle_event = 'snap' |
| 122 |
process.env.npm_package_name = '@npmcli/arborist' |
| 123 |
const debug = t.mock('../lib/debug.js') |
| 124 |
t.plan(1) |
| 125 |
debug(() => { |
| 126 |
t.pass('called debug function') |
| 127 |
}) |
| 128 |
}) |