Archived
1
0

Add separate function for VS Code arguments (#4599)

The problem before was that the pop() caused the open in existing
instance functionality to break because the arguments no longer
contained the file.

We could simply remove the pop() but since `workspace` and `folder` are
not CLI arguments I think it makes sense to handle them in a separate
function which can be called at the point where they are needed.  This
also lets us de-duplicate some logic since we create these arguments in
two spots and lets us skip this logic when we do not need it.

The pop() is still avoided because manipulating a passed-in object
in-place seems like a risky move.  If we really need to do this we
should copy the positional argument array instead.
This commit is contained in:
Asher
2021-12-10 12:01:35 -06:00
committed by GitHub
parent 3b91cffae5
commit 9e583fa562
5 changed files with 112 additions and 58 deletions

View File

@ -12,10 +12,26 @@ import {
setDefaults,
shouldOpenInExistingInstance,
splitOnFirstEquals,
toVsCodeArgs,
} from "../../../src/node/cli"
import { shouldSpawnCliProcess } from "../../../src/node/main"
import { generatePassword, paths } from "../../../src/node/util"
import { useEnv, tmpdir } from "../../utils/helpers"
import { clean, useEnv, tmpdir } from "../../utils/helpers"
// The parser should not set any defaults so the caller can determine what
// values the user actually set. These are only set after explicitly calling
// `setDefaults`.
const defaults = {
auth: "password",
host: "localhost",
port: 8080,
"proxy-domain": [],
usingEnvPassword: false,
usingEnvHashedPassword: false,
"extensions-dir": path.join(paths.data, "extensions"),
"user-data-dir": paths.data,
_: [],
}
describe("parser", () => {
beforeEach(() => {
@ -24,23 +40,6 @@ describe("parser", () => {
console.log = jest.fn()
})
// The parser should not set any defaults so the caller can determine what
// values the user actually set. These are only set after explicitly calling
// `setDefaults`.
const defaults = {
auth: "password",
host: "localhost",
port: 8080,
"proxy-domain": [],
usingEnvPassword: false,
usingEnvHashedPassword: false,
"extensions-dir": path.join(paths.data, "extensions"),
"user-data-dir": paths.data,
_: [],
workspace: "",
folder: "",
}
it("should parse nothing", async () => {
expect(parse([])).toStrictEqual({})
})
@ -667,3 +666,59 @@ describe("readSocketPath", () => {
expect(contents2).toBe(contents1)
})
})
describe("toVsCodeArgs", () => {
const vscodeDefaults = {
...defaults,
"connection-token": "0000",
"accept-server-license-terms": true,
help: false,
port: "8080",
version: false,
}
beforeAll(async () => {
// Clean up temporary directories from the previous run.
await clean("vscode-args")
})
it("should convert empty args", async () => {
expect(await toVsCodeArgs(await setDefaults(parse([])))).toStrictEqual({
...vscodeDefaults,
folder: "",
workspace: "",
})
})
it("should convert with workspace", async () => {
const workspace = path.join(await tmpdir("vscode-args"), "test.code-workspace")
await fs.writeFile(workspace, "foobar")
expect(await toVsCodeArgs(await setDefaults(parse([workspace])))).toStrictEqual({
...vscodeDefaults,
workspace,
folder: "",
_: [workspace],
})
})
it("should convert with folder", async () => {
const folder = await tmpdir("vscode-args")
expect(await toVsCodeArgs(await setDefaults(parse([folder])))).toStrictEqual({
...vscodeDefaults,
folder,
workspace: "",
_: [folder],
})
})
it("should ignore regular file", async () => {
const file = path.join(await tmpdir("vscode-args"), "file")
await fs.writeFile(file, "foobar")
expect(await toVsCodeArgs(await setDefaults(parse([file])))).toStrictEqual({
...vscodeDefaults,
folder: "",
workspace: "",
_: [file],
})
})
})

View File

@ -42,8 +42,6 @@ describe("plugin", () => {
usingEnvHashedPassword: false,
"extensions-dir": "",
"user-data-dir": "",
workspace: "",
folder: "",
}
next()
}