Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4997f50

Browse files
committed
adjust tempfile naming
1 parent1925567 commit4997f50

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

‎src/sshConfig.test.ts

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import{it,afterEach,vi,expect}from"vitest"
33
import{SSHConfig}from"./sshConfig"
44

5-
constsshFilePath="~/.config/ssh"
5+
// This is not the usual path to ~/.ssh/config, but
6+
// setting it to a different path makes it easier to test
7+
// and makes mistakes abundantly clear.
8+
constsshFilePath="/Path/To/UserHomeDir/.sshConfigDir/sshConfigFile"
9+
constsshTempFilePathExpr=`^/Path/To/UserHomeDir/.sshConfigDir/.sshConfigFile.vscode-coder-tmp.[a-z0-9]+$`
610

711
constmockFileSystem={
812
mkdir:vi.fn(),
@@ -42,11 +46,14 @@ Host coder-vscode--*
4246

4347
expect(mockFileSystem.readFile).toBeCalledWith(sshFilePath,expect.anything())
4448
expect(mockFileSystem.writeFile).toBeCalledWith(
45-
expect.stringContaining(sshFilePath),
49+
expect.stringMatching(sshTempFilePathExpr),
4650
expectedOutput,
47-
expect.anything(),
51+
expect.objectContaining({
52+
encoding:"utf-8",
53+
mode:0o600,// Default mode for new files.
54+
}),
4855
)
49-
expect(mockFileSystem.rename).toBeCalledWith(expect.stringContaining(sshFilePath+"."),sshFilePath)
56+
expect(mockFileSystem.rename).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),sshFilePath)
5057
})
5158

5259
it("creates a new file and adds the config",async()=>{
@@ -75,11 +82,14 @@ Host coder-vscode.dev.coder.com--*
7582

7683
expect(mockFileSystem.readFile).toBeCalledWith(sshFilePath,expect.anything())
7784
expect(mockFileSystem.writeFile).toBeCalledWith(
78-
expect.stringContaining(sshFilePath),
85+
expect.stringMatching(sshTempFilePathExpr),
7986
expectedOutput,
80-
expect.anything(),
87+
expect.objectContaining({
88+
encoding:"utf-8",
89+
mode:0o600,// Default mode for new files.
90+
}),
8191
)
82-
expect(mockFileSystem.rename).toBeCalledWith(expect.stringContaining(sshFilePath+"."),sshFilePath)
92+
expect(mockFileSystem.rename).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),sshFilePath)
8393
})
8494

8595
it("adds a new coder config in an existent SSH configuration",async()=>{
@@ -115,11 +125,11 @@ Host coder-vscode.dev.coder.com--*
115125
UserKnownHostsFile /dev/null
116126
# --- END CODER VSCODE dev.coder.com ---`
117127

118-
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringContaining(sshFilePath),expectedOutput,{
128+
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),expectedOutput,{
119129
encoding:"utf-8",
120130
mode:0o644,
121131
})
122-
expect(mockFileSystem.rename).toBeCalledWith(expect.stringContaining(sshFilePath+"."),sshFilePath)
132+
expect(mockFileSystem.rename).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),sshFilePath)
123133
})
124134

125135
it("updates an existent coder config",async()=>{
@@ -181,11 +191,11 @@ Host coder-vscode.dev-updated.coder.com--*
181191
Host *
182192
SetEnv TEST=1`
183193

184-
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringContaining(sshFilePath),expectedOutput,{
194+
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),expectedOutput,{
185195
encoding:"utf-8",
186196
mode:0o644,
187197
})
188-
expect(mockFileSystem.rename).toBeCalledWith(expect.stringContaining(sshFilePath+"."),sshFilePath)
198+
expect(mockFileSystem.rename).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),sshFilePath)
189199
})
190200

191201
it("does not remove deployment-unaware SSH config and adds the new one",async()=>{
@@ -228,11 +238,11 @@ Host coder-vscode.dev.coder.com--*
228238
UserKnownHostsFile /dev/null
229239
# --- END CODER VSCODE dev.coder.com ---`
230240

231-
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringContaining(sshFilePath),expectedOutput,{
241+
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),expectedOutput,{
232242
encoding:"utf-8",
233243
mode:0o644,
234244
})
235-
expect(mockFileSystem.rename).toBeCalledWith(expect.stringContaining(sshFilePath+"."),sshFilePath)
245+
expect(mockFileSystem.rename).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),sshFilePath)
236246
})
237247

238248
it("it does not remove a user-added block that only matches the host of an old coder SSH config",async()=>{
@@ -264,11 +274,11 @@ Host coder-vscode.dev.coder.com--*
264274
UserKnownHostsFile /dev/null
265275
# --- END CODER VSCODE dev.coder.com ---`
266276

267-
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringContaining(sshFilePath),expectedOutput,{
277+
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),expectedOutput,{
268278
encoding:"utf-8",
269279
mode:0o644,
270280
})
271-
expect(mockFileSystem.rename).toBeCalledWith(expect.stringContaining(sshFilePath+"."),sshFilePath)
281+
expect(mockFileSystem.rename).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),sshFilePath)
272282
})
273283

274284
it("throws an error if there is a missing end block",async()=>{
@@ -540,11 +550,11 @@ Host afterconfig
540550
LogLevel:"ERROR",
541551
})
542552

543-
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringContaining(sshFilePath),expectedOutput,{
553+
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),expectedOutput,{
544554
encoding:"utf-8",
545555
mode:0o644,
546556
})
547-
expect(mockFileSystem.rename).toBeCalledWith(expect.stringContaining(sshFilePath+"."),sshFilePath)
557+
expect(mockFileSystem.rename).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),sshFilePath)
548558
})
549559

550560
it("override values",async()=>{
@@ -588,9 +598,12 @@ Host coder-vscode.dev.coder.com--*
588598

589599
expect(mockFileSystem.readFile).toBeCalledWith(sshFilePath,expect.anything())
590600
expect(mockFileSystem.writeFile).toBeCalledWith(
591-
expect.stringContaining(sshFilePath),
601+
expect.stringMatching(sshTempFilePathExpr),
592602
expectedOutput,
593-
expect.anything(),
603+
expect.objectContaining({
604+
encoding:"utf-8",
605+
mode:0o600,// Default mode for new files.
606+
}),
594607
)
595-
expect(mockFileSystem.rename).toBeCalledWith(expect.stringContaining(sshFilePath+"."),sshFilePath)
608+
expect(mockFileSystem.rename).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr),sshFilePath)
596609
})

‎src/sshConfig.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ export class SSHConfig {
239239
recursive:true,
240240
})
241241
constrandSuffix=Math.random().toString(36).substring(8)
242-
consttempFilePath=`${this.filePath}.${randSuffix}`
242+
constfileName=path.basename(this.filePath)
243+
constdirName=path.dirname(this.filePath)
244+
consttempFilePath=`${dirName}/.${fileName}.vscode-coder-tmp.${randSuffix}`
243245
awaitthis.fileSystem.writeFile(tempFilePath,this.getRaw(),{
244246
mode:existingMode,
245247
encoding:"utf-8",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp