summaryrefslogtreecommitdiff
path: root/deps/npm/test/lib/utils/config.js
blob: 90789a8db0bc2263e1a054c4e0332b82f242a52d (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const t = require('tap')
const requireInject = require('require-inject')
Object.defineProperty(process, 'umask', {
  value: () => 0o26,
  writable: true,
  configurable: true,
  enumerable: true
})

// have to fake the node version, or else it'll only pass on this one
Object.defineProperty(process, 'version', {
  value: 'v14.8.0'
})

t.formatSnapshot = obj => {
  if (typeof obj !== 'object' || !obj || !obj.types) {
    return obj
  }
  return {
    ...obj,
    defaults: {
      ...obj.defaults,
      cache: '{CACHE DIR} ' + path.basename(obj.defaults.cache)
    },
    types: formatTypes(obj.types)
  }
}

const path = require('path')
const url = require('url')
const semver = require('semver')

const formatTypes = (types) => Object.entries(types).map(([key, value]) => {
  return [key, formatTypeValue(value)]
}).reduce((set, [key, value]) => {
  set[key] = value
  return set
}, {})

const formatTypeValue = (value) => {
  if (Array.isArray(value)) {
    return value.map(formatTypeValue)
  } else if (value === url) {
    return '{URL MODULE}'
  } else if (value === path) {
    return '{PATH MODULE}'
  } else if (value === semver) {
    return '{SEMVER MODULE}'
  } else if (typeof value === 'function') {
    return `{${value.name} TYPE}`
  } else {
    return value
  }
}

process.env.ComSpec = 'cmd.exe'
process.env.SHELL = '/usr/local/bin/bash'
process.env.LANG = 'UTF-8'
delete process.env.LC_ALL
delete process.env.LC_CTYPE
process.env.EDITOR = 'vim'
process.env.VISUAL = 'mate'

const networkInterfacesThrow = () => {
  throw new Error('no network interfaces for some reason')
}
const networkInterfaces = () => ({
  'eth420': [{ address: '127.0.0.1' }],
  'eth69': [{ address: 'no place like home' }]
})
const tmpdir = () => '/tmp'
const os = { networkInterfaces, tmpdir }

t.test('working network interfaces, not windows', t => {
  const config = requireInject('../../../lib/utils/config.js', {
    os,
    '@npmcli/ci-detect': () => false,
    '../../../lib/utils/is-windows.js': false
  })
  t.matchSnapshot(config)
  t.end()
})

t.test('no working network interfaces, on windows', t => {
  const config = requireInject('../../../lib/utils/config.js', {
    os: { tmpdir, networkInterfaces: networkInterfacesThrow },
    '@npmcli/ci-detect': () => false,
    '../../../lib/utils/is-windows.js': true
  })
  t.matchSnapshot(config)
  t.end()
})

t.test('no process.umask() method', t => {
  Object.defineProperty(process, 'umask', {
    value: null,
    writable: true,
    configurable: true,
    enumerable: true
  })
  t.teardown(() => {
    Object.defineProperty(process, 'umask', {
      value: () => 0o26,
      writable: true,
      configurable: true,
      enumerable: true
    })
  })
  const config = requireInject('../../../lib/utils/config.js', {
    os: { tmpdir, networkInterfaces: networkInterfacesThrow },
    '@npmcli/ci-detect': () => false,
    '../../../lib/utils/is-windows.js': true
  })
  t.equal(config.defaults.umask, 0o22)
  t.matchSnapshot(config)
  t.end()
})

t.test('no comspec on windows', t => {
  delete process.env.ComSpec
  const config = requireInject('../../../lib/utils/config.js', {
    os: { tmpdir, networkInterfaces: networkInterfacesThrow },
    '@npmcli/ci-detect': () => false,
    '../../../lib/utils/is-windows.js': true
  })
  t.equal(config.defaults.shell, 'cmd')
  t.end()
})

t.test('no shell on posix', t => {
  delete process.env.SHELL
  const config = requireInject('../../../lib/utils/config.js', {
    os,
    '@npmcli/ci-detect': () => false,
    '../../../lib/utils/is-windows.js': false
  })
  t.equal(config.defaults.shell, 'bash')
  t.end()
})

t.test('no EDITOR env, use VISUAL', t => {
  delete process.env.EDITOR
  const config = requireInject('../../../lib/utils/config.js', {
    os,
    '@npmcli/ci-detect': () => false,
    '../../../lib/utils/is-windows.js': false
  })
  t.equal(config.defaults.editor, 'mate')
  t.end()
})

t.test('no VISUAL, use system default, not windows', t => {
  delete process.env.VISUAL
  const config = requireInject('../../../lib/utils/config.js', {
    os,
    '@npmcli/ci-detect': () => false,
    '../../../lib/utils/is-windows.js': false
  })
  t.equal(config.defaults.editor, 'vi')
  t.end()
})

t.test('no VISUAL, use system default, not windows', t => {
  delete process.env.VISUAL
  const config = requireInject('../../../lib/utils/config.js', {
    os,
    '@npmcli/ci-detect': () => false,
    '../../../lib/utils/is-windows.js': true
  })
  t.equal(config.defaults.editor, 'notepad.exe')
  t.end()
})