Skip to content

Commit 87370ed

Browse files
committed
refactor: clean up
1 parent e107a15 commit 87370ed

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

src/node/cli.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import * as path from "path"
66
import { Args as VsArgs } from "../../typings/ipc"
77
import { canConnect, generateCertificate, generatePassword, humanPath, isNodeJSErrnoException, paths } from "./util"
88

9+
const DEFAULT_SOCKET_PATH = path.join(os.tmpdir(), "vscode-ipc")
10+
911
export enum Feature {
1012
/** Web socket compression. */
1113
PermessageDeflate = "permessage-deflate",
@@ -662,18 +664,20 @@ export const shouldRunVsCodeCli = (args: Args): boolean => {
662664
*
663665
* If it can't read the path, it throws an error and returns undefined.
664666
*/
665-
export async function readSocketPath(): Promise<string | undefined> {
666-
// TODO@jsjoeio - we should make this function pure and pass in the path
667-
// to the file it reads
667+
export async function readSocketPath(path: string): Promise<string | undefined> {
668668
try {
669-
return await fs.readFile(path.join(os.tmpdir(), "vscode-ipc"), "utf8")
669+
return await fs.readFile(path, "utf8")
670670
} catch (error) {
671+
// If it doesn't exist, we don't care.
672+
// But if it fails for some reason, we should throw.
673+
// We want to surface that to the user.
671674
if (!isNodeJSErrnoException(error) || error.code !== "ENOENT") {
672675
throw error
673676
}
674677
}
675678
return undefined
676679
}
680+
677681
/**
678682
* Determine if it looks like the user is trying to open a file or folder in an
679683
* existing instance. The arguments here should be the arguments the user
@@ -691,15 +695,15 @@ export const shouldOpenInExistingInstance = async (args: Args): Promise<string |
691695
return args[cur as keyof Args] ? prev + 1 : prev
692696
}, 0)
693697
if (openInFlagCount > 0) {
694-
return readSocketPath()
698+
return readSocketPath(DEFAULT_SOCKET_PATH)
695699
}
696700

697701
// It's possible the user is trying to spawn another instance of code-server.
698702
// Check if any unrelated flags are set (check against one because `_` always
699703
// exists), that a file or directory was passed, and that the socket is
700704
// active.
701705
if (Object.keys(args).length === 1 && args._.length > 0) {
702-
const socketPath = await readSocketPath()
706+
const socketPath = await readSocketPath(DEFAULT_SOCKET_PATH)
703707
if (socketPath && (await canConnect(socketPath))) {
704708
return socketPath
705709
}

src/node/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,5 +531,5 @@ export function escapeHtml(unsafe: string): string {
531531
* it has a .code property.
532532
*/
533533
export function isNodeJSErrnoException(error: unknown): error is NodeJS.ErrnoException {
534-
return error instanceof Error && (error as NodeJS.ErrnoException).code !== undefined
534+
return error !== undefined && (error as NodeJS.ErrnoException).code !== undefined
535535
}

test/unit/node/cli.test.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import {
1414
shouldRunVsCodeCli,
1515
splitOnFirstEquals,
1616
} from "../../../src/node/cli"
17-
import { tmpdir } from "../../../src/node/constants"
1817
import { generatePassword, paths } from "../../../src/node/util"
19-
import { useEnv } from "../../utils/helpers"
18+
import { tmpdir, useEnv } from "../../utils/helpers"
2019

2120
type Mutable<T> = {
2221
-readonly [P in keyof T]: T[P]
@@ -379,10 +378,11 @@ describe("parser", () => {
379378

380379
describe("cli", () => {
381380
let args: Mutable<Args> = { _: [] }
382-
const testDir = path.join(tmpdir, "tests/cli")
383381
const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc")
382+
let testDir: string
384383

385384
beforeAll(async () => {
385+
testDir = await tmpdir("cli")
386386
await fs.rmdir(testDir, { recursive: true })
387387
await fs.mkdir(testDir, { recursive: true })
388388
})
@@ -657,32 +657,39 @@ cert: false`)
657657
})
658658
})
659659

660-
describe.only("readSocketPath", () => {
661-
const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc")
660+
describe("readSocketPath", () => {
662661
const fileContents = "readSocketPath file contents"
662+
let tmpDirPath: string
663+
let tmpFilePath: string
663664

664665
beforeEach(async () => {
665-
await fs.writeFile(vscodeIpcPath, fileContents)
666+
tmpDirPath = await tmpdir("readSocketPath")
667+
tmpFilePath = path.join(tmpDirPath, "readSocketPath.txt")
668+
await fs.writeFile(tmpFilePath, fileContents)
666669
})
667670

668671
afterEach(async () => {
669-
await fs.rmdir(vscodeIpcPath, { recursive: true })
672+
await fs.rmdir(tmpDirPath, { recursive: true })
670673
})
671674

672-
it.skip("should throw an error if it can't read the file", async () => {
675+
it("should throw an error if it can't read the file", async () => {
673676
// TODO@jsjoeio - implement
677+
// Test it on a directory.... ESDIR
674678
// TODO@jsjoeio - implement
675-
const p = await readSocketPath()
676-
console.log(p)
677-
expect(p).toThrowError("oops")
679+
expect(() => readSocketPath(tmpDirPath)).rejects.toThrow("EISDIR")
678680
})
679-
it.skip("should return undefined if it can't read the file", async () => {
681+
it("should return undefined if it can't read the file", async () => {
680682
// TODO@jsjoeio - implement
681-
const socketPath = await readSocketPath()
683+
const socketPath = await readSocketPath(path.join(tmpDirPath, "not-a-file"))
682684
expect(socketPath).toBeUndefined()
683685
})
684686
it("should return the file contents", async () => {
685-
const contents = await readSocketPath()
687+
const contents = await readSocketPath(tmpFilePath)
686688
expect(contents).toBe(fileContents)
687689
})
690+
it("should return the same file contents for two different calls", async () => {
691+
const contents1 = await readSocketPath(tmpFilePath)
692+
const contents2 = await readSocketPath(tmpFilePath)
693+
expect(contents2).toBe(contents1)
694+
})
688695
})

0 commit comments

Comments
 (0)