-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathopts.js
183 lines (154 loc) · 3.73 KB
/
opts.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
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
173
174
175
176
177
178
179
180
181
182
183
const t = require('tap')
const ini = require('ini')
let [GIT_ASKPASS, GIT_SSH_COMMAND] = ['', '']
const mockFs = {
existsSync: () => false,
readFileSync: () => '',
}
let gitOpts
t.beforeEach(() => {
backupEnv()
gitOpts = t.mock('../lib/opts.js', {
'node:fs': mockFs,
})
})
t.afterEach(() => {
restoreEnv()
})
t.test('defaults', t => {
t.match(gitOpts(), {
env: {
GIT_ASKPASS: 'echo',
GIT_SSH_COMMAND: 'ssh -oStrictHostKeyChecking=accept-new',
},
shell: false,
}, 'got the git defaults we want')
t.end()
})
t.test('handle case when fs.existsSync throws an error', t => {
const gitOptsWithMockFs = t.mock('../lib/opts.js', {
'node:fs': {
...mockFs,
existsSync: () => {
throw new Error('Mocked error')
},
},
})
t.match(gitOptsWithMockFs(), {
env: {
GIT_ASKPASS: 'echo',
GIT_SSH_COMMAND: 'ssh -oStrictHostKeyChecking=accept-new',
},
shell: false,
}, 'should apply defaults when fs.existsSync throws an error')
t.end()
})
t.test('handle case when git config does not exist', t => {
const gitOptsWithMockFs = t.mock('../lib/opts.js', {
'node:fs': {
...mockFs,
existsSync: () => false,
},
})
t.match(gitOptsWithMockFs(), {
env: {
GIT_ASKPASS: 'echo',
GIT_SSH_COMMAND: 'ssh -oStrictHostKeyChecking=accept-new',
},
shell: false,
}, 'should apply defaults when git config does not exist')
t.end()
})
t.test('does not override when sshCommand is set in env', t => {
process.env.GIT_ASKPASS = 'test_askpass'
process.env.GIT_SSH_COMMAND = 'test_ssh_command'
t.match(gitOpts(), {
env: {
GIT_ASKPASS: 'test_askpass',
GIT_SSH_COMMAND: 'test_ssh_command',
},
shell: false,
}, 'values already in process.env remain')
t.end()
})
t.test('as non-root', t => {
process.getuid = () => 999
t.match(gitOpts({
foo: 'bar',
env: { override: 'for some reason' },
}, {
uid: 420,
gid: 69,
abc: 'def',
}), {
foo: 'bar',
env: { override: 'for some reason' },
uid: undefined,
gid: undefined,
abc: undefined,
}, 'do not set uid/gid as non-root')
t.end()
})
t.test('does not override when sshCommand is set in git config', t => {
const gitConfigContent = `[core]
askpass = echo
sshCommand = custom_ssh_command
`
const gitOptsWithMockFs = t.mock('../lib/opts.js', {
'node:fs': {
...mockFs,
existsSync: () => true,
readFileSync: () => gitConfigContent,
},
})
t.match(gitOptsWithMockFs(), {
env: {
GIT_ASKPASS: null,
GIT_SSH_COMMAND: null,
},
shell: false,
}, 'sshCommand in git config remains')
t.end()
})
t.test('does not override when sshCommand is set in git config', t => {
const gitConfigContent = `[core]
askpass = echo
sshCommand = custom_ssh_command
`
const { loadGitConfig } = t.mock('../lib/opts.js', {
'node:fs': {
...mockFs,
existsSync: () => true,
readFileSync: () => gitConfigContent,
},
})
t.match(loadGitConfig(),
ini.parse(gitConfigContent),
'cachedConfig should be populated with git config'
)
const gitOptsWithMockFs = t.mock('../lib/opts.js', {
'node:fs': {
...mockFs,
existsSync: () => true,
readFileSync: () => gitConfigContent,
},
})
t.match(gitOptsWithMockFs(), {
env: {
GIT_ASKPASS: null,
GIT_SSH_COMMAND: null,
},
shell: false,
}, 'sshCommand in git config remains')
t.end()
})
function backupEnv () {
GIT_ASKPASS = process.env.GIT_ASKPASS
GIT_SSH_COMMAND = process.env.GIT_SSH_COMMAND
delete process.env.GIT_ASKPASS
delete process.env.GIT_SSH_COMMAND
}
function restoreEnv () {
process.env.GIT_ASKPASS = GIT_ASKPASS
process.env.GIT_SSH_COMMAND = GIT_SSH_COMMAND
}