|
1 | 1 | import { Level, logger } from "@coder/logger"
|
2 | 2 | import * as assert from "assert"
|
| 3 | +import * as fs from "fs-extra" |
| 4 | +import * as net from "net" |
| 5 | +import * as os from "os" |
3 | 6 | import * as path from "path"
|
4 |
| -import { parse, setDefaults } from "../src/node/cli" |
5 |
| -import { paths } from "../src/node/util" |
| 7 | +import { Args, parse, setDefaults, shouldOpenInExistingInstance } from "../src/node/cli" |
| 8 | +import { paths, tmpdir } from "../src/node/util" |
6 | 9 |
|
7 |
| -describe("cli", () => { |
| 10 | +type Mutable<T> = { |
| 11 | + -readonly [P in keyof T]: T[P] |
| 12 | +} |
| 13 | + |
| 14 | +describe("parser", () => { |
8 | 15 | beforeEach(() => {
|
9 | 16 | delete process.env.LOG_LEVEL
|
10 | 17 | })
|
11 | 18 |
|
12 | 19 | // The parser should not set any defaults so the caller can determine what
|
13 |
| - // values the user actually set. These are set after calling `setDefaults`. |
| 20 | + // values the user actually set. These are only set after explicitly calling |
| 21 | + // `setDefaults`. |
14 | 22 | const defaults = {
|
15 | 23 | "extensions-dir": path.join(paths.data, "extensions"),
|
16 | 24 | "user-data-dir": paths.data,
|
@@ -225,3 +233,76 @@ describe("cli", () => {
|
225 | 233 | })
|
226 | 234 | })
|
227 | 235 | })
|
| 236 | + |
| 237 | +describe("cli", () => { |
| 238 | + let args: Mutable<Args> = { _: [] } |
| 239 | + const testDir = path.join(tmpdir, "tests/cli") |
| 240 | + const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc") |
| 241 | + |
| 242 | + before(async () => { |
| 243 | + await fs.remove(testDir) |
| 244 | + await fs.mkdirp(testDir) |
| 245 | + }) |
| 246 | + |
| 247 | + beforeEach(async () => { |
| 248 | + delete process.env.VSCODE_IPC_HOOK_CLI |
| 249 | + args = { _: [] } |
| 250 | + await fs.remove(vscodeIpcPath) |
| 251 | + }) |
| 252 | + |
| 253 | + it("should use existing if inside code-server", async () => { |
| 254 | + process.env.VSCODE_IPC_HOOK_CLI = "test" |
| 255 | + assert.strictEqual(await shouldOpenInExistingInstance(args), "test") |
| 256 | + |
| 257 | + args.port = 8081 |
| 258 | + args._.push("./file") |
| 259 | + assert.strictEqual(await shouldOpenInExistingInstance(args), "test") |
| 260 | + }) |
| 261 | + |
| 262 | + it("should use existing if --reuse-window is set", async () => { |
| 263 | + args["reuse-window"] = true |
| 264 | + assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) |
| 265 | + |
| 266 | + await fs.writeFile(vscodeIpcPath, "test") |
| 267 | + assert.strictEqual(await shouldOpenInExistingInstance(args), "test") |
| 268 | + |
| 269 | + args.port = 8081 |
| 270 | + assert.strictEqual(await shouldOpenInExistingInstance(args), "test") |
| 271 | + }) |
| 272 | + |
| 273 | + it("should use existing if --new-window is set", async () => { |
| 274 | + args["new-window"] = true |
| 275 | + assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) |
| 276 | + |
| 277 | + await fs.writeFile(vscodeIpcPath, "test") |
| 278 | + assert.strictEqual(await shouldOpenInExistingInstance(args), "test") |
| 279 | + |
| 280 | + args.port = 8081 |
| 281 | + assert.strictEqual(await shouldOpenInExistingInstance(args), "test") |
| 282 | + }) |
| 283 | + |
| 284 | + it("should use existing if no unrelated flags are set, has positional, and socket is active", async () => { |
| 285 | + assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) |
| 286 | + |
| 287 | + args._.push("./file") |
| 288 | + assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) |
| 289 | + |
| 290 | + const socketPath = path.join(testDir, "socket") |
| 291 | + await fs.writeFile(vscodeIpcPath, socketPath) |
| 292 | + assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) |
| 293 | + |
| 294 | + await new Promise((resolve) => { |
| 295 | + const server = net.createServer(() => { |
| 296 | + // Close after getting the first connection. |
| 297 | + server.close() |
| 298 | + }) |
| 299 | + server.once("listening", () => resolve(server)) |
| 300 | + server.listen(socketPath) |
| 301 | + }) |
| 302 | + |
| 303 | + assert.strictEqual(await shouldOpenInExistingInstance(args), socketPath) |
| 304 | + |
| 305 | + args.port = 8081 |
| 306 | + assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) |
| 307 | + }) |
| 308 | +}) |
0 commit comments