Skip to content

Commit 341f6a0

Browse files
committed
adjust tempfile naming
1 parent 698b334 commit 341f6a0

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-
const sshFilePath = "~/.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+
const sshFilePath = "/Path/To/UserHomeDir/.sshConfigDir/sshConfigFile"
9+
const sshTempFilePathExpr = `^/Path/To/UserHomeDir/.sshConfigDir/.sshConfigFile.vscode-coder-tmp.[a-z0-9]+$`
610

711
const mockFileSystem = {
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 mismatched start and end block count", async () => {
@@ -500,11 +510,11 @@ Host afterconfig
500510
LogLevel: "ERROR",
501511
})
502512

503-
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringContaining(sshFilePath), expectedOutput, {
513+
expect(mockFileSystem.writeFile).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr), expectedOutput, {
504514
encoding: "utf-8",
505515
mode: 0o644,
506516
})
507-
expect(mockFileSystem.rename).toBeCalledWith(expect.stringContaining(sshFilePath + "."), sshFilePath)
517+
expect(mockFileSystem.rename).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr), sshFilePath)
508518
})
509519

510520
it("override values", async () => {
@@ -548,9 +558,12 @@ Host coder-vscode.dev.coder.com--*
548558

549559
expect(mockFileSystem.readFile).toBeCalledWith(sshFilePath, expect.anything())
550560
expect(mockFileSystem.writeFile).toBeCalledWith(
551-
expect.stringContaining(sshFilePath),
561+
expect.stringMatching(sshTempFilePathExpr),
552562
expectedOutput,
553-
expect.anything(),
563+
expect.objectContaining({
564+
encoding: "utf-8",
565+
mode: 0o600, // Default mode for new files.
566+
}),
554567
)
555-
expect(mockFileSystem.rename).toBeCalledWith(expect.stringContaining(sshFilePath + "."), sshFilePath)
568+
expect(mockFileSystem.rename).toBeCalledWith(expect.stringMatching(sshTempFilePathExpr), sshFilePath)
556569
})

src/sshConfig.ts

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

0 commit comments

Comments
 (0)