@@ -5,10 +5,11 @@ import { SSHConfig } from "./sshConfig"
5
5
const sshFilePath = "~/.config/ssh"
6
6
7
7
const mockFileSystem = {
8
- readFile : vi . fn ( ) ,
9
8
mkdir : vi . fn ( ) ,
10
- writeFile : vi . fn ( ) ,
9
+ readFile : vi . fn ( ) ,
11
10
rename : vi . fn ( ) ,
11
+ stat : vi . fn ( ) ,
12
+ writeFile : vi . fn ( ) ,
12
13
}
13
14
14
15
afterEach ( ( ) => {
@@ -17,6 +18,7 @@ afterEach(() => {
17
18
18
19
it ( "creates a new file and adds config with empty label" , async ( ) => {
19
20
mockFileSystem . readFile . mockRejectedValueOnce ( "No file found" )
21
+ mockFileSystem . stat . mockRejectedValueOnce ( { code : "ENOENT" } )
20
22
21
23
const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
22
24
await sshConfig . load ( )
@@ -49,6 +51,7 @@ Host coder-vscode--*
49
51
50
52
it ( "creates a new file and adds the config" , async ( ) => {
51
53
mockFileSystem . readFile . mockRejectedValueOnce ( "No file found" )
54
+ mockFileSystem . stat . mockRejectedValueOnce ( { code : "ENOENT" } )
52
55
53
56
const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
54
57
await sshConfig . load ( )
@@ -88,6 +91,7 @@ it("adds a new coder config in an existent SSH configuration", async () => {
88
91
StrictHostKeyChecking=no
89
92
UserKnownHostsFile=/dev/null`
90
93
mockFileSystem . readFile . mockResolvedValueOnce ( existentSSHConfig )
94
+ mockFileSystem . stat . mockResolvedValueOnce ( { mode : 0o644 } )
91
95
92
96
const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
93
97
await sshConfig . load ( )
@@ -113,7 +117,7 @@ Host coder-vscode.dev.coder.com--*
113
117
114
118
expect ( mockFileSystem . writeFile ) . toBeCalledWith ( expect . stringContaining ( sshFilePath ) , expectedOutput , {
115
119
encoding : "utf-8" ,
116
- mode : 384 ,
120
+ mode : 0o644 ,
117
121
} )
118
122
expect ( mockFileSystem . rename ) . toBeCalledWith ( expect . stringContaining ( sshFilePath + "." ) , sshFilePath )
119
123
} )
@@ -150,6 +154,7 @@ Host coder-vscode.dev.coder.com--*
150
154
Host *
151
155
SetEnv TEST=1`
152
156
mockFileSystem . readFile . mockResolvedValueOnce ( existentSSHConfig )
157
+ mockFileSystem . stat . mockResolvedValueOnce ( { mode : 0o644 } )
153
158
154
159
const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
155
160
await sshConfig . load ( )
@@ -178,7 +183,7 @@ Host *
178
183
179
184
expect ( mockFileSystem . writeFile ) . toBeCalledWith ( expect . stringContaining ( sshFilePath ) , expectedOutput , {
180
185
encoding : "utf-8" ,
181
- mode : 384 ,
186
+ mode : 0o644 ,
182
187
} )
183
188
expect ( mockFileSystem . rename ) . toBeCalledWith ( expect . stringContaining ( sshFilePath + "." ) , sshFilePath )
184
189
} )
@@ -199,6 +204,7 @@ Host coder-vscode--*
199
204
UserKnownHostsFile=/dev/null
200
205
# --- END CODER VSCODE ---`
201
206
mockFileSystem . readFile . mockResolvedValueOnce ( existentSSHConfig )
207
+ mockFileSystem . stat . mockResolvedValueOnce ( { mode : 0o644 } )
202
208
203
209
const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
204
210
await sshConfig . load ( )
@@ -224,7 +230,7 @@ Host coder-vscode.dev.coder.com--*
224
230
225
231
expect ( mockFileSystem . writeFile ) . toBeCalledWith ( expect . stringContaining ( sshFilePath ) , expectedOutput , {
226
232
encoding : "utf-8" ,
227
- mode : 384 ,
233
+ mode : 0o644 ,
228
234
} )
229
235
expect ( mockFileSystem . rename ) . toBeCalledWith ( expect . stringContaining ( sshFilePath + "." ) , sshFilePath )
230
236
} )
@@ -233,6 +239,7 @@ it("it does not remove a user-added block that only matches the host of an old c
233
239
const existentSSHConfig = `Host coder-vscode--*
234
240
ForwardAgent=yes`
235
241
mockFileSystem . readFile . mockResolvedValueOnce ( existentSSHConfig )
242
+ mockFileSystem . stat . mockResolvedValueOnce ( { mode : 0o644 } )
236
243
237
244
const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
238
245
await sshConfig . load ( )
@@ -259,7 +266,7 @@ Host coder-vscode.dev.coder.com--*
259
266
260
267
expect ( mockFileSystem . writeFile ) . toBeCalledWith ( expect . stringContaining ( sshFilePath ) , expectedOutput , {
261
268
encoding : "utf-8" ,
262
- mode : 384 ,
269
+ mode : 0o644 ,
263
270
} )
264
271
expect ( mockFileSystem . rename ) . toBeCalledWith ( expect . stringContaining ( sshFilePath + "." ) , sshFilePath )
265
272
} )
@@ -451,6 +458,7 @@ Host afterconfig
451
458
452
459
const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
453
460
mockFileSystem . readFile . mockResolvedValueOnce ( existentSSHConfig )
461
+ mockFileSystem . stat . mockResolvedValueOnce ( { mode : 0o644 } )
454
462
await sshConfig . load ( )
455
463
456
464
const expectedOutput = `Host beforeconfig
@@ -494,13 +502,15 @@ Host afterconfig
494
502
495
503
expect ( mockFileSystem . writeFile ) . toBeCalledWith ( expect . stringContaining ( sshFilePath ) , expectedOutput , {
496
504
encoding : "utf-8" ,
497
- mode : 384 ,
505
+ mode : 0o644 ,
498
506
} )
499
507
expect ( mockFileSystem . rename ) . toBeCalledWith ( expect . stringContaining ( sshFilePath + "." ) , sshFilePath )
500
508
} )
501
509
502
510
it ( "override values" , async ( ) => {
503
511
mockFileSystem . readFile . mockRejectedValueOnce ( "No file found" )
512
+ mockFileSystem . stat . mockRejectedValueOnce ( { code : "ENOENT" } )
513
+
504
514
const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
505
515
await sshConfig . load ( )
506
516
await sshConfig . update (
0 commit comments