Skip to content

Commit ecc06ea

Browse files
committed
feat: add tests for readSocketPath
1 parent 9c8ab46 commit ecc06ea

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/node/cli.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,9 @@ function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
660660
}
661661

662662
/**
663-
* Reads the socketPath which defaults to a temporary directory
664-
* with another directory called vscode-ipc.
663+
* Reads the socketPath based on path passed in.
664+
*
665+
* The one usually pased in is the DEFAULT_SOCKET_PATH.
665666
*
666667
* If it can't read the path, it throws an error and returns undefined.
667668
*/

test/unit/node/cli.test.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import {
1111
setDefaults,
1212
shouldOpenInExistingInstance,
1313
splitOnFirstEquals,
14+
readSocketPath,
1415
} from "../../../src/node/cli"
15-
import { tmpdir } from "../../../src/node/constants"
1616
import { shouldSpawnCliProcess } from "../../../src/node/main"
1717
import { generatePassword, paths } from "../../../src/node/util"
18-
import { useEnv } from "../../utils/helpers"
18+
import { useEnv, tmpdir } from "../../utils/helpers"
1919

2020
type Mutable<T> = {
2121
-readonly [P in keyof T]: T[P]
@@ -390,10 +390,11 @@ describe("parser", () => {
390390

391391
describe("cli", () => {
392392
let args: Mutable<Args> = { _: [] }
393-
const testDir = path.join(tmpdir, "tests/cli")
393+
let testDir: string
394394
const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc")
395395

396396
beforeAll(async () => {
397+
testDir = await tmpdir("cli")
397398
await fs.rmdir(testDir, { recursive: true })
398399
await fs.mkdir(testDir, { recursive: true })
399400
})
@@ -667,3 +668,40 @@ password: ${password}
667668
cert: false`)
668669
})
669670
})
671+
672+
describe("readSocketPath", () => {
673+
const fileContents = "readSocketPath file contents"
674+
let tmpDirPath: string
675+
let tmpFilePath: string
676+
677+
beforeEach(async () => {
678+
tmpDirPath = await tmpdir("readSocketPath")
679+
tmpFilePath = path.join(tmpDirPath, "readSocketPath.txt")
680+
await fs.writeFile(tmpFilePath, fileContents)
681+
})
682+
683+
afterEach(async () => {
684+
await fs.rmdir(tmpDirPath, { recursive: true })
685+
})
686+
687+
it("should throw an error if it can't read the file", async () => {
688+
// TODO@jsjoeio - implement
689+
// Test it on a directory.... ESDIR
690+
// TODO@jsjoeio - implement
691+
expect(() => readSocketPath(tmpDirPath)).rejects.toThrow("EISDIR")
692+
})
693+
it("should return undefined if it can't read the file", async () => {
694+
// TODO@jsjoeio - implement
695+
const socketPath = await readSocketPath(path.join(tmpDirPath, "not-a-file"))
696+
expect(socketPath).toBeUndefined()
697+
})
698+
it("should return the file contents", async () => {
699+
const contents = await readSocketPath(tmpFilePath)
700+
expect(contents).toBe(fileContents)
701+
})
702+
it("should return the same file contents for two different calls", async () => {
703+
const contents1 = await readSocketPath(tmpFilePath)
704+
const contents2 = await readSocketPath(tmpFilePath)
705+
expect(contents2).toBe(contents1)
706+
})
707+
})

0 commit comments

Comments
 (0)