diff options
author | Luke Karrys <luke@lukekarrys.com> | 2022-02-22 07:58:12 +0300 |
---|---|---|
committer | Luke Karrys <luke@lukekarrys.com> | 2022-02-24 02:58:59 +0300 |
commit | ce1c2bf4bade53df66efff03415d29a828a3d47a (patch) | |
tree | 35253ef97d11949e4fe1f2ddc9badf6582e9e993 /test/fixtures | |
parent | 174dd88cba31b25461619fe796fe1d3ac34eae70 (diff) |
chore(cli): remove log option from being passed anywhere
Diffstat (limited to 'test/fixtures')
-rw-r--r-- | test/fixtures/mock-npm.js | 53 | ||||
-rw-r--r-- | test/fixtures/sandbox.js | 45 |
2 files changed, 51 insertions, 47 deletions
diff --git a/test/fixtures/mock-npm.js b/test/fixtures/mock-npm.js index c3dc47f5d..4846d9a48 100644 --- a/test/fixtures/mock-npm.js +++ b/test/fixtures/mock-npm.js @@ -31,6 +31,18 @@ const RealMockNpm = (t, otherMocks = {}) => { return mock } +const setLoglevel = (t, loglevel, reset = true) => { + if (t && reset) { + const _level = log.level + t.teardown(() => log.level = _level) + } + + if (loglevel) { + // Set log level on the npmlog singleton and shared across everything + log.level = loglevel + } +} + // Resolve some options to a function call with supplied args const result = (fn, ...args) => typeof fn === 'function' ? fn(...args) : fn @@ -62,14 +74,8 @@ const LoadMockNpm = async (t, { throw new Error('cant `load` without `init`') } - const _level = log.level - t.teardown(() => log.level = _level) - - if (config.loglevel) { - // Set log level as early as possible since it is set - // on the npmlog singleton and shared across everything - log.level = config.loglevel - } + // Set log level as early as possible since + setLoglevel(t, config.loglevel) const dir = t.testdir({ root: testdir, cache: {} }) const prefix = path.join(dir, 'root') @@ -93,11 +99,9 @@ const LoadMockNpm = async (t, { for (const [k, v] of Object.entries(result(config, { npm, prefix, cache }))) { npm.config.set(k, v) } - if (config.loglevel) { - // Set global loglevel *again* since it possibly got reset during load - // XXX: remove with npmlog - log.level = config.loglevel - } + // Set global loglevel *again* since it possibly got reset during load + // XXX: remove with npmlog + setLoglevel(t, config.loglevel, false) npm.prefix = prefix npm.cache = cache } @@ -129,7 +133,7 @@ const realConfig = require('../../lib/utils/config') // npm.config You still need a separate flatOptions. Tests should migrate to // using the real npm mock above class MockNpm { - constructor (base = {}) { + constructor (base = {}, t) { this._mockOutputs = [] this.isMockNpm = true this.base = base @@ -150,9 +154,24 @@ class MockNpm { // for now isDefault is going to just return false if a value was defined isDefault: (k) => !Object.prototype.hasOwnProperty.call(config, k), get: (k) => ({ ...realConfig.defaults, ...config })[k], - set: (k, v) => config[k] = v, + set: (k, v) => { + config[k] = v + // mock how real npm derives silent + if (k === 'loglevel') { + this.flatOptions.silent = v === 'silent' + this.silent = v === 'silent' + } + }, list: [{ ...realConfig.defaults, ...config }], } + + if (t && config.loglevel) { + setLoglevel(t, config.loglevel) + } + + if (config.loglevel) { + this.config.set('loglevel', config.loglevel) + } } output (...msg) { @@ -163,8 +182,8 @@ class MockNpm { } } -const FakeMockNpm = (base = {}) => { - return new MockNpm(base) +const FakeMockNpm = (base = {}, t) => { + return new MockNpm(base, t) } module.exports = { diff --git a/test/fixtures/sandbox.js b/test/fixtures/sandbox.js index 701d9cea7..d51281d41 100644 --- a/test/fixtures/sandbox.js +++ b/test/fixtures/sandbox.js @@ -193,6 +193,8 @@ class Sandbox extends EventEmitter { // test.teardown hook teardown () { if (this[_parent]) { + const sandboxProcess = sandboxes.get(this[_parent]) + sandboxProcess.removeAllListeners('log') sandboxes.delete(this[_parent]) } if (this[_npm]) { @@ -211,20 +213,7 @@ class Sandbox extends EventEmitter { return Reflect.get(this, prop, this) } - const actual = Reflect.get(target, prop, receiver) - if (typeof actual === 'function') { - // in node 10.1 there's an interesting bug where if a function on process - // is called without explicitly forcing the 'this' arg to something, we - // get 'Illegal invocation' errors. wrapping function properties in their - // own proxy so that we can make sure the context is right fixes it - return new Proxy(actual, { - apply: (target, context, args) => { - return Reflect.apply(target, _process, args) - }, - }) - } - - return actual + return Reflect.get(target, prop, receiver) } // proxy set handler @@ -278,17 +267,15 @@ class Sandbox extends EventEmitter { ...this[_mocks], ...mockedLogs.logMocks, }) + this.process.on('log', (l, ...args) => { + if (l !== 'pause' && l !== 'resume') { + this[_logs].push([l, ...args]) + } + }) + this[_npm] = new Npm() this[_npm].output = (...args) => this[_output].push(args) await this[_npm].load() - // in some node versions (later 10.x) our executionAsyncId at this point - // will for some reason appear to have been triggered by a different parent - // so immediately after load, if we can see that we lost our ancestry, we - // fix it here with a hammer - if (chain.get(executionAsyncId()) !== this[_parent]) { - chain.set(executionAsyncId(), this[_parent]) - process = this[_proxy] - } const cmd = this[_npm].argv.shift() return this[_npm].exec(cmd, this[_npm].argv) @@ -330,17 +317,15 @@ class Sandbox extends EventEmitter { ...this[_mocks], ...mockedLogs.logMocks, }) + this.process.on('log', (l, ...args) => { + if (l !== 'pause' && l !== 'resume') { + this[_logs].push([l, ...args]) + } + }) + this[_npm] = new Npm() this[_npm].output = (...args) => this[_output].push(args) await this[_npm].load() - // in some node versions (later 10.x) our executionAsyncId at this point - // will for some reason appear to have been triggered by a different parent - // so immediately after load, if we can see that we lost our ancestry, we - // fix it here with a hammer - if (chain.get(executionAsyncId()) !== this[_parent]) { - chain.set(executionAsyncId(), this[_parent]) - process = this[_proxy] - } const impl = await this[_npm].cmd(command) return impl.completion({ |