/
mock-npm.js
57 lines (50 loc) · 1.49 KB
/
mock-npm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Basic npm fixture that you can give a config object that acts like
// npm.config You still need a separate flatOptions but this is the first step
// to eventually just using npm itself
const realConfig = require('../../lib/utils/config')
class MockNpm {
constructor (base = {}) {
this._mockOutputs = []
this.isMockNpm = true
this.base = base
const config = base.config || {}
for (const attr in base) {
if (attr !== 'config') {
this[attr] = base[attr]
}
}
this.flatOptions = base.flatOptions || {}
this.config = {
// for now just set `find` to what config.find should return
// this works cause `find` is not an existing config entry
find: (k) => ({...realConfig.defaults, ...config})[k],
get: (k) => ({...realConfig.defaults, ...config})[k],
set: (k, v) => config[k] = v,
list: [{ ...realConfig.defaults, ...config}]
}
if (!this.log) {
this.log = {
clearProgress: () => {},
disableProgress: () => {},
enableProgress: () => {},
http: () => {},
info: () => {},
levels: [],
notice: () => {},
pause: () => {},
silly: () => {},
verbose: () => {},
warn: () => {},
}
}
}
output(...msg) {
if (this.base.output)
return this.base.output(msg)
this._mockOutputs.push(msg)
}
}
// TODO export MockNpm, and change tests to use new MockNpm()
module.exports = (base = {}) => {
return new MockNpm(base)
}