@@ -5,10 +5,11 @@ import { SSHConfig } from "./sshConfig"
55const sshFilePath = "~/.config/ssh"
66
77const mockFileSystem = {
8- readFile : vi . fn ( ) ,
98 mkdir : vi . fn ( ) ,
10- writeFile : vi . fn ( ) ,
9+ readFile : vi . fn ( ) ,
1110 rename : vi . fn ( ) ,
11+ stat : vi . fn ( ) ,
12+ writeFile : vi . fn ( ) ,
1213}
1314
1415afterEach ( ( ) => {
@@ -17,6 +18,7 @@ afterEach(() => {
1718
1819it ( "creates a new file and adds config with empty label" , async ( ) => {
1920 mockFileSystem . readFile . mockRejectedValueOnce ( "No file found" )
21+ mockFileSystem . stat . mockRejectedValueOnce ( { code : "ENOENT" } )
2022
2123 const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
2224 await sshConfig . load ( )
@@ -49,6 +51,7 @@ Host coder-vscode--*
4951
5052it ( "creates a new file and adds the config" , async ( ) => {
5153 mockFileSystem . readFile . mockRejectedValueOnce ( "No file found" )
54+ mockFileSystem . stat . mockRejectedValueOnce ( { code : "ENOENT" } )
5255
5356 const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
5457 await sshConfig . load ( )
@@ -88,6 +91,7 @@ it("adds a new coder config in an existent SSH configuration", async () => {
8891 StrictHostKeyChecking=no
8992 UserKnownHostsFile=/dev/null`
9093 mockFileSystem . readFile . mockResolvedValueOnce ( existentSSHConfig )
94+ mockFileSystem . stat . mockResolvedValueOnce ( { mode : 0o644 } )
9195
9296 const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
9397 await sshConfig . load ( )
@@ -113,7 +117,7 @@ Host coder-vscode.dev.coder.com--*
113117
114118 expect ( mockFileSystem . writeFile ) . toBeCalledWith ( expect . stringContaining ( sshFilePath ) , expectedOutput , {
115119 encoding : "utf-8" ,
116- mode : 384 ,
120+ mode : 0o644 ,
117121 } )
118122 expect ( mockFileSystem . rename ) . toBeCalledWith ( expect . stringContaining ( sshFilePath + "." ) , sshFilePath )
119123} )
@@ -150,6 +154,7 @@ Host coder-vscode.dev.coder.com--*
150154Host *
151155 SetEnv TEST=1`
152156 mockFileSystem . readFile . mockResolvedValueOnce ( existentSSHConfig )
157+ mockFileSystem . stat . mockResolvedValueOnce ( { mode : 0o644 } )
153158
154159 const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
155160 await sshConfig . load ( )
@@ -178,7 +183,7 @@ Host *
178183
179184 expect ( mockFileSystem . writeFile ) . toBeCalledWith ( expect . stringContaining ( sshFilePath ) , expectedOutput , {
180185 encoding : "utf-8" ,
181- mode : 384 ,
186+ mode : 0o644 ,
182187 } )
183188 expect ( mockFileSystem . rename ) . toBeCalledWith ( expect . stringContaining ( sshFilePath + "." ) , sshFilePath )
184189} )
@@ -199,6 +204,7 @@ Host coder-vscode--*
199204 UserKnownHostsFile=/dev/null
200205# --- END CODER VSCODE ---`
201206 mockFileSystem . readFile . mockResolvedValueOnce ( existentSSHConfig )
207+ mockFileSystem . stat . mockResolvedValueOnce ( { mode : 0o644 } )
202208
203209 const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
204210 await sshConfig . load ( )
@@ -224,7 +230,7 @@ Host coder-vscode.dev.coder.com--*
224230
225231 expect ( mockFileSystem . writeFile ) . toBeCalledWith ( expect . stringContaining ( sshFilePath ) , expectedOutput , {
226232 encoding : "utf-8" ,
227- mode : 384 ,
233+ mode : 0o644 ,
228234 } )
229235 expect ( mockFileSystem . rename ) . toBeCalledWith ( expect . stringContaining ( sshFilePath + "." ) , sshFilePath )
230236} )
@@ -233,6 +239,7 @@ it("it does not remove a user-added block that only matches the host of an old c
233239 const existentSSHConfig = `Host coder-vscode--*
234240 ForwardAgent=yes`
235241 mockFileSystem . readFile . mockResolvedValueOnce ( existentSSHConfig )
242+ mockFileSystem . stat . mockResolvedValueOnce ( { mode : 0o644 } )
236243
237244 const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
238245 await sshConfig . load ( )
@@ -259,7 +266,7 @@ Host coder-vscode.dev.coder.com--*
259266
260267 expect ( mockFileSystem . writeFile ) . toBeCalledWith ( expect . stringContaining ( sshFilePath ) , expectedOutput , {
261268 encoding : "utf-8" ,
262- mode : 384 ,
269+ mode : 0o644 ,
263270 } )
264271 expect ( mockFileSystem . rename ) . toBeCalledWith ( expect . stringContaining ( sshFilePath + "." ) , sshFilePath )
265272} )
@@ -451,6 +458,7 @@ Host afterconfig
451458
452459 const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
453460 mockFileSystem . readFile . mockResolvedValueOnce ( existentSSHConfig )
461+ mockFileSystem . stat . mockResolvedValueOnce ( { mode : 0o644 } )
454462 await sshConfig . load ( )
455463
456464 const expectedOutput = `Host beforeconfig
@@ -494,13 +502,15 @@ Host afterconfig
494502
495503 expect ( mockFileSystem . writeFile ) . toBeCalledWith ( expect . stringContaining ( sshFilePath ) , expectedOutput , {
496504 encoding : "utf-8" ,
497- mode : 384 ,
505+ mode : 0o644 ,
498506 } )
499507 expect ( mockFileSystem . rename ) . toBeCalledWith ( expect . stringContaining ( sshFilePath + "." ) , sshFilePath )
500508} )
501509
502510it ( "override values" , async ( ) => {
503511 mockFileSystem . readFile . mockRejectedValueOnce ( "No file found" )
512+ mockFileSystem . stat . mockRejectedValueOnce ( { code : "ENOENT" } )
513+
504514 const sshConfig = new SSHConfig ( sshFilePath , mockFileSystem )
505515 await sshConfig . load ( )
506516 await sshConfig . update (
0 commit comments