Skip to content

Commit eb77c7d

Browse files
committed
feat: add tests for readSocketPath
1 parent d3cbb5e commit eb77c7d

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
yarn-build-
4444
4545
- name: Install dependencies
46-
if: steps.cache-yarn.outputs.cache-hit != 'true'
46+
# if: steps.cache-yarn.outputs.cache-hit != 'true'
4747
run: yarn --frozen-lockfile
4848

4949
- name: Run yarn fmt

src/node/cli.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,9 @@ export const shouldRunVsCodeCli = (args: Args): boolean => {
670670
}
671671

672672
/**
673-
* Reads the socketPath which defaults to a temporary directory
674-
* with another directory called vscode-ipc.
673+
* Reads the socketPath based on path passed in.
674+
*
675+
* The one usually pased in is the DEFAULT_SOCKET_PATH.
675676
*
676677
* If it can't read the path, it throws an error and returns undefined.
677678
*/

test/unit/node/cli.test.ts

+41-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {
1212
shouldOpenInExistingInstance,
1313
shouldRunVsCodeCli,
1414
splitOnFirstEquals,
15+
readSocketPath,
1516
} from "../../../src/node/cli"
16-
import { tmpdir } from "../../../src/node/constants"
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]
@@ -378,10 +378,11 @@ describe("parser", () => {
378378

379379
describe("cli", () => {
380380
let args: Mutable<Args> = { _: [] }
381-
const testDir = path.join(tmpdir, "tests/cli")
381+
let testDir: string
382382
const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc")
383383

384384
beforeAll(async () => {
385+
testDir = await tmpdir("cli")
385386
await fs.rmdir(testDir, { recursive: true })
386387
await fs.mkdir(testDir, { recursive: true })
387388
})
@@ -655,3 +656,40 @@ password: ${password}
655656
cert: false`)
656657
})
657658
})
659+
660+
describe("readSocketPath", () => {
661+
const fileContents = "readSocketPath file contents"
662+
let tmpDirPath: string
663+
let tmpFilePath: string
664+
665+
beforeEach(async () => {
666+
tmpDirPath = await tmpdir("readSocketPath")
667+
tmpFilePath = path.join(tmpDirPath, "readSocketPath.txt")
668+
await fs.writeFile(tmpFilePath, fileContents)
669+
})
670+
671+
afterEach(async () => {
672+
await fs.rmdir(tmpDirPath, { recursive: true })
673+
})
674+
675+
it("should throw an error if it can't read the file", async () => {
676+
// TODO@jsjoeio - implement
677+
// Test it on a directory.... ESDIR
678+
// TODO@jsjoeio - implement
679+
expect(() => readSocketPath(tmpDirPath)).rejects.toThrow("EISDIR")
680+
})
681+
it("should return undefined if it can't read the file", async () => {
682+
// TODO@jsjoeio - implement
683+
const socketPath = await readSocketPath(path.join(tmpDirPath, "not-a-file"))
684+
expect(socketPath).toBeUndefined()
685+
})
686+
it("should return the file contents", async () => {
687+
const contents = await readSocketPath(tmpFilePath)
688+
expect(contents).toBe(fileContents)
689+
})
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+
})
695+
})

0 commit comments

Comments
 (0)