2020-09-16 02:43:07 +08:00
|
|
|
import { Level, logger } from "@coder/logger"
|
2021-03-16 05:15:24 +08:00
|
|
|
import { promises as fs } from "fs"
|
2020-02-07 08:26:07 +08:00
|
|
|
import * as path from "path"
|
2021-09-21 05:53:09 +08:00
|
|
|
import {
|
2021-11-10 13:28:31 +08:00
|
|
|
UserProvidedArgs,
|
2021-09-22 01:41:33 +08:00
|
|
|
bindAddrFromArgs,
|
2021-09-29 06:45:44 +08:00
|
|
|
defaultConfigFile,
|
2021-09-21 05:53:09 +08:00
|
|
|
parse,
|
|
|
|
setDefaults,
|
|
|
|
shouldOpenInExistingInstance,
|
2022-03-23 04:07:14 +08:00
|
|
|
toCodeArgs,
|
2022-03-12 04:27:19 +08:00
|
|
|
optionDescriptions,
|
|
|
|
options,
|
|
|
|
Options,
|
|
|
|
AuthType,
|
|
|
|
OptionalString,
|
2021-09-21 05:53:09 +08:00
|
|
|
} from "../../../src/node/cli"
|
2021-10-29 04:27:17 +08:00
|
|
|
import { shouldSpawnCliProcess } from "../../../src/node/main"
|
2021-09-29 06:51:00 +08:00
|
|
|
import { generatePassword, paths } from "../../../src/node/util"
|
2023-06-15 05:32:07 +08:00
|
|
|
import {
|
|
|
|
EditorSessionManager,
|
|
|
|
EditorSessionManagerClient,
|
|
|
|
makeEditorSessionManagerServer,
|
|
|
|
} from "../../../src/node/vscodeSocket"
|
|
|
|
import { clean, useEnv, tmpdir, listenOn } from "../../utils/helpers"
|
2021-12-11 02:01:35 +08:00
|
|
|
|
|
|
|
// 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,
|
2023-06-22 14:47:01 +08:00
|
|
|
"session-socket": path.join(paths.data, "code-server-ipc.sock"),
|
2021-12-11 02:01:35 +08:00
|
|
|
_: [],
|
|
|
|
}
|
2020-02-07 08:26:07 +08:00
|
|
|
|
2020-09-16 05:51:43 +08:00
|
|
|
describe("parser", () => {
|
2020-02-19 06:51:55 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
delete process.env.LOG_LEVEL
|
2020-10-16 05:17:04 +08:00
|
|
|
delete process.env.PASSWORD
|
2022-04-14 00:39:05 +08:00
|
|
|
delete process.env.CS_DISABLE_FILE_DOWNLOADS
|
2022-10-29 00:59:24 +08:00
|
|
|
delete process.env.CS_DISABLE_GETTING_STARTED_OVERRIDE
|
2023-03-31 04:01:49 +08:00
|
|
|
delete process.env.VSCODE_PROXY_URI
|
2023-07-22 07:23:21 +08:00
|
|
|
delete process.env.CS_DISABLE_PROXY
|
2021-01-26 07:34:43 +08:00
|
|
|
console.log = jest.fn()
|
2020-02-19 06:51:55 +08:00
|
|
|
})
|
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
it("should parse nothing", async () => {
|
|
|
|
expect(parse([])).toStrictEqual({})
|
2021-01-13 02:53:12 +08:00
|
|
|
})
|
2020-02-07 08:26:07 +08:00
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
it("should parse all available options", async () => {
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(
|
2021-11-10 13:28:31 +08:00
|
|
|
parse(
|
|
|
|
[
|
|
|
|
["--enable", "feature1"],
|
|
|
|
["--enable", "feature2"],
|
|
|
|
|
|
|
|
"--bind-addr=192.169.0.1:8080",
|
|
|
|
|
|
|
|
["--auth", "none"],
|
|
|
|
|
|
|
|
["--extensions-dir", "path/to/ext/dir"],
|
|
|
|
|
|
|
|
["--builtin-extensions-dir", "path/to/builtin/ext/dir"],
|
|
|
|
|
|
|
|
"1",
|
|
|
|
"--verbose",
|
2022-10-14 06:32:20 +08:00
|
|
|
["--app-name", "custom instance name"],
|
|
|
|
["--welcome-text", "welcome to code"],
|
2021-11-10 13:28:31 +08:00
|
|
|
"2",
|
|
|
|
|
2022-01-05 02:37:11 +08:00
|
|
|
["--locale", "ja"],
|
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
["--log", "error"],
|
|
|
|
|
|
|
|
"--help",
|
|
|
|
|
|
|
|
"--open",
|
|
|
|
|
|
|
|
"--socket=mumble",
|
|
|
|
|
2022-03-04 00:54:35 +08:00
|
|
|
"--socket-mode=777",
|
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
"3",
|
|
|
|
|
|
|
|
["--user-data-dir", "path/to/user/dir"],
|
|
|
|
|
|
|
|
["--cert=path/to/cert", "--cert-key", "path/to/cert/key"],
|
|
|
|
|
|
|
|
"--version",
|
|
|
|
|
|
|
|
"--json",
|
|
|
|
|
|
|
|
"--port=8081",
|
|
|
|
|
2022-04-14 00:39:05 +08:00
|
|
|
"--disable-file-downloads",
|
|
|
|
|
2022-10-29 00:59:24 +08:00
|
|
|
"--disable-getting-started-override",
|
|
|
|
|
2023-07-22 07:23:21 +08:00
|
|
|
"--disable-proxy",
|
|
|
|
|
2024-08-27 04:19:55 +08:00
|
|
|
["--abs-proxy-base-path", "/codeserver/app1"],
|
|
|
|
|
2023-06-22 14:47:01 +08:00
|
|
|
["--session-socket", "/tmp/override-code-server-ipc-socket"],
|
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
["--host", "0.0.0.0"],
|
|
|
|
"4",
|
|
|
|
"--",
|
|
|
|
"--5",
|
|
|
|
].flat(),
|
|
|
|
),
|
2021-01-13 02:53:12 +08:00
|
|
|
).toEqual({
|
2021-11-10 13:28:31 +08:00
|
|
|
_: ["1", "2", "3", "4", "--5"],
|
2021-01-13 02:53:12 +08:00
|
|
|
auth: "none",
|
2021-11-10 13:28:31 +08:00
|
|
|
"builtin-extensions-dir": path.resolve("path/to/builtin/ext/dir"),
|
|
|
|
"extensions-dir": path.resolve("path/to/ext/dir"),
|
|
|
|
"user-data-dir": path.resolve("path/to/user/dir"),
|
|
|
|
"cert-key": path.resolve("path/to/cert/key"),
|
2021-01-13 02:53:12 +08:00
|
|
|
cert: {
|
2021-11-10 13:28:31 +08:00
|
|
|
value: path.resolve("path/to/cert"),
|
2020-02-15 08:46:00 +08:00
|
|
|
},
|
2022-04-14 00:39:05 +08:00
|
|
|
"disable-file-downloads": true,
|
2022-10-29 00:59:24 +08:00
|
|
|
"disable-getting-started-override": true,
|
2023-07-22 07:23:21 +08:00
|
|
|
"disable-proxy": true,
|
2021-05-05 05:46:08 +08:00
|
|
|
enable: ["feature1", "feature2"],
|
2021-01-13 02:53:12 +08:00
|
|
|
help: true,
|
|
|
|
host: "0.0.0.0",
|
|
|
|
json: true,
|
2022-01-05 02:37:11 +08:00
|
|
|
locale: "ja",
|
2021-01-13 02:53:12 +08:00
|
|
|
log: "error",
|
|
|
|
open: true,
|
|
|
|
port: 8081,
|
|
|
|
socket: path.resolve("mumble"),
|
2022-03-04 00:54:35 +08:00
|
|
|
"socket-mode": "777",
|
2021-01-13 02:53:12 +08:00
|
|
|
verbose: true,
|
2022-10-14 06:32:20 +08:00
|
|
|
"app-name": "custom instance name",
|
|
|
|
"welcome-text": "welcome to code",
|
2021-01-13 02:53:12 +08:00
|
|
|
version: true,
|
|
|
|
"bind-addr": "192.169.0.1:8080",
|
2023-06-22 14:47:01 +08:00
|
|
|
"session-socket": "/tmp/override-code-server-ipc-socket",
|
2024-08-27 04:19:55 +08:00
|
|
|
"abs-proxy-base-path": "/codeserver/app1",
|
2021-01-13 02:53:12 +08:00
|
|
|
})
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
it("should work with short options", async () => {
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(parse(["-vvv", "-v"])).toEqual({
|
2020-02-07 08:26:07 +08:00
|
|
|
verbose: true,
|
|
|
|
version: true,
|
|
|
|
})
|
2020-02-19 06:51:55 +08:00
|
|
|
})
|
|
|
|
|
2020-09-16 02:43:07 +08:00
|
|
|
it("should use log level env var", async () => {
|
|
|
|
const args = parse([])
|
2021-11-10 13:28:31 +08:00
|
|
|
expect(args).toEqual({})
|
2020-09-16 02:43:07 +08:00
|
|
|
|
2020-02-19 06:51:55 +08:00
|
|
|
process.env.LOG_LEVEL = "debug"
|
2021-01-09 04:55:47 +08:00
|
|
|
const defaults = await setDefaults(args)
|
2021-01-12 02:37:27 +08:00
|
|
|
expect(defaults).toStrictEqual({
|
2020-04-29 05:39:01 +08:00
|
|
|
...defaults,
|
2020-02-19 06:51:55 +08:00
|
|
|
log: "debug",
|
2020-05-19 12:39:57 +08:00
|
|
|
verbose: false,
|
2020-02-19 06:51:55 +08:00
|
|
|
})
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(process.env.LOG_LEVEL).toEqual("debug")
|
|
|
|
expect(logger.level).toEqual(Level.Debug)
|
2020-04-29 05:39:01 +08:00
|
|
|
|
|
|
|
process.env.LOG_LEVEL = "trace"
|
2021-01-09 04:55:47 +08:00
|
|
|
const updated = await setDefaults(args)
|
2021-01-13 02:53:12 +08:00
|
|
|
expect(updated).toStrictEqual({
|
2021-01-09 04:55:47 +08:00
|
|
|
...updated,
|
2020-04-29 05:39:01 +08:00
|
|
|
log: "trace",
|
|
|
|
verbose: true,
|
|
|
|
})
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(process.env.LOG_LEVEL).toEqual("trace")
|
|
|
|
expect(logger.level).toEqual(Level.Trace)
|
2020-02-19 06:51:55 +08:00
|
|
|
})
|
|
|
|
|
2020-05-14 18:08:37 +08:00
|
|
|
it("should prefer --log to env var and --verbose to --log", async () => {
|
2020-09-16 02:43:07 +08:00
|
|
|
let args = parse(["--log", "info"])
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(args).toEqual({
|
2020-09-16 02:43:07 +08:00
|
|
|
log: "info",
|
|
|
|
})
|
|
|
|
|
2020-02-19 06:51:55 +08:00
|
|
|
process.env.LOG_LEVEL = "debug"
|
2021-01-13 02:53:12 +08:00
|
|
|
const defaults = await setDefaults(args)
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(defaults).toEqual({
|
2020-04-29 05:39:01 +08:00
|
|
|
...defaults,
|
2020-02-19 06:51:55 +08:00
|
|
|
log: "info",
|
2020-05-19 12:39:57 +08:00
|
|
|
verbose: false,
|
2020-02-19 06:51:55 +08:00
|
|
|
})
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(process.env.LOG_LEVEL).toEqual("info")
|
|
|
|
expect(logger.level).toEqual(Level.Info)
|
2020-04-29 05:39:01 +08:00
|
|
|
|
|
|
|
process.env.LOG_LEVEL = "trace"
|
2021-01-09 04:55:47 +08:00
|
|
|
const updated = await setDefaults(args)
|
2021-01-13 02:53:12 +08:00
|
|
|
expect(updated).toEqual({
|
2020-04-29 05:39:01 +08:00
|
|
|
...defaults,
|
|
|
|
log: "info",
|
2020-05-19 12:39:57 +08:00
|
|
|
verbose: false,
|
2020-04-29 05:39:01 +08:00
|
|
|
})
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(process.env.LOG_LEVEL).toEqual("info")
|
|
|
|
expect(logger.level).toEqual(Level.Info)
|
2020-04-29 05:39:01 +08:00
|
|
|
|
2020-09-16 02:43:07 +08:00
|
|
|
args = parse(["--log", "info", "--verbose"])
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(args).toEqual({
|
2020-09-16 02:43:07 +08:00
|
|
|
log: "info",
|
|
|
|
verbose: true,
|
|
|
|
})
|
|
|
|
|
2020-04-29 05:39:01 +08:00
|
|
|
process.env.LOG_LEVEL = "warn"
|
2021-01-09 04:55:47 +08:00
|
|
|
const updatedAgain = await setDefaults(args)
|
2021-01-13 02:53:12 +08:00
|
|
|
expect(updatedAgain).toEqual({
|
2020-04-29 05:39:01 +08:00
|
|
|
...defaults,
|
|
|
|
log: "trace",
|
|
|
|
verbose: true,
|
|
|
|
})
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(process.env.LOG_LEVEL).toEqual("trace")
|
|
|
|
expect(logger.level).toEqual(Level.Trace)
|
2020-04-29 05:39:01 +08:00
|
|
|
})
|
|
|
|
|
2022-03-08 07:08:07 +08:00
|
|
|
it("should set valid log level env var", async () => {
|
|
|
|
process.env.LOG_LEVEL = "error"
|
|
|
|
const defaults = await setDefaults(parse([]))
|
|
|
|
expect(defaults).toEqual({
|
|
|
|
...defaults,
|
|
|
|
log: "error",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-09-16 02:43:07 +08:00
|
|
|
it("should ignore invalid log level env var", async () => {
|
2020-04-29 05:39:01 +08:00
|
|
|
process.env.LOG_LEVEL = "bogus"
|
2021-01-09 04:55:47 +08:00
|
|
|
const defaults = await setDefaults(parse([]))
|
|
|
|
expect(defaults).toEqual({
|
2020-09-16 02:43:07 +08:00
|
|
|
...defaults,
|
|
|
|
})
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
|
|
|
|
2020-05-19 12:39:57 +08:00
|
|
|
it("should error if value isn't provided", () => {
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(() => parse(["--auth"])).toThrowError(/--auth requires a value/)
|
|
|
|
expect(() => parse(["--auth=", "--log=debug"])).toThrowError(/--auth requires a value/)
|
|
|
|
expect(() => parse(["--auth", "--log"])).toThrowError(/--auth requires a value/)
|
|
|
|
expect(() => parse(["--auth", "--invalid"])).toThrowError(/--auth requires a value/)
|
|
|
|
expect(() => parse(["--bind-addr"])).toThrowError(/--bind-addr requires a value/)
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
|
|
|
|
2020-05-19 12:39:57 +08:00
|
|
|
it("should error if value is invalid", () => {
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(() => parse(["--port", "foo"])).toThrowError(/--port must be a number/)
|
|
|
|
expect(() => parse(["--auth", "invalid"])).toThrowError(/--auth valid values: \[password, none\]/)
|
|
|
|
expect(() => parse(["--log", "invalid"])).toThrowError(/--log valid values: \[trace, debug, info, warn, error\]/)
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
|
|
|
|
2020-05-19 12:39:57 +08:00
|
|
|
it("should error if the option doesn't exist", () => {
|
2021-01-13 02:53:12 +08:00
|
|
|
expect(() => parse(["--foo"])).toThrowError(/Unknown option --foo/)
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
it("should not error if the value is optional", async () => {
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(parse(["--cert"])).toEqual({
|
2020-02-07 08:26:07 +08:00
|
|
|
cert: {
|
|
|
|
value: undefined,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-19 12:39:57 +08:00
|
|
|
it("should not allow option-like values", () => {
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(() => parse(["--socket", "--socket-path-value"])).toThrowError(/--socket requires a value/)
|
2020-02-07 08:26:07 +08:00
|
|
|
// If you actually had a path like this you would do this instead:
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(parse(["--socket", "./--socket-path-value"])).toEqual({
|
2020-02-07 08:26:07 +08:00
|
|
|
socket: path.resolve("--socket-path-value"),
|
|
|
|
})
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(() => parse(["--cert", "--socket-path-value"])).toThrowError(/Unknown option --socket-path-value/)
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
2020-02-21 08:44:03 +08:00
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
it("should allow positional arguments before options", async () => {
|
|
|
|
expect(parse(["test", "--auth", "none"])).toEqual({
|
|
|
|
_: ["test"],
|
2020-02-21 08:44:03 +08:00
|
|
|
auth: "none",
|
|
|
|
})
|
|
|
|
})
|
2020-03-24 01:08:50 +08:00
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
it("should support repeatable flags", async () => {
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(parse(["--proxy-domain", "*.coder.com"])).toEqual({
|
2020-03-24 01:08:50 +08:00
|
|
|
"proxy-domain": ["*.coder.com"],
|
|
|
|
})
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"])).toEqual({
|
2020-03-24 01:08:50 +08:00
|
|
|
"proxy-domain": ["*.coder.com", "test.com"],
|
|
|
|
})
|
|
|
|
})
|
2020-10-16 05:17:04 +08:00
|
|
|
|
2021-01-13 02:53:12 +08:00
|
|
|
it("should enforce cert-key with cert value or otherwise generate one", async () => {
|
|
|
|
const args = parse(["--cert"])
|
|
|
|
expect(args).toEqual({
|
|
|
|
cert: {
|
|
|
|
value: undefined,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(() => parse(["--cert", "test"])).toThrowError(/--cert-key is missing/)
|
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
|
|
|
...defaults,
|
|
|
|
cert: {
|
|
|
|
value: path.join(paths.data, "localhost.crt"),
|
|
|
|
},
|
|
|
|
"cert-key": path.join(paths.data, "localhost.key"),
|
|
|
|
})
|
|
|
|
})
|
2020-10-16 05:17:04 +08:00
|
|
|
|
|
|
|
it("should use env var password", async () => {
|
|
|
|
process.env.PASSWORD = "test"
|
|
|
|
const args = parse([])
|
2021-11-10 13:28:31 +08:00
|
|
|
expect(args).toEqual({})
|
2020-10-16 05:17:04 +08:00
|
|
|
|
2021-01-09 04:55:47 +08:00
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
2020-10-16 05:17:04 +08:00
|
|
|
...defaults,
|
|
|
|
password: "test",
|
|
|
|
usingEnvPassword: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-04 02:27:59 +08:00
|
|
|
it("should use env var hashed password", async () => {
|
2021-06-03 05:18:54 +08:00
|
|
|
process.env.HASHED_PASSWORD =
|
|
|
|
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY" // test
|
2020-12-09 04:54:17 +08:00
|
|
|
const args = parse([])
|
2021-11-10 13:28:31 +08:00
|
|
|
expect(args).toEqual({})
|
2020-12-09 04:54:17 +08:00
|
|
|
|
2021-01-13 02:53:12 +08:00
|
|
|
const defaultArgs = await setDefaults(args)
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(defaultArgs).toEqual({
|
2020-12-09 04:54:17 +08:00
|
|
|
...defaults,
|
2021-06-03 05:18:54 +08:00
|
|
|
"hashed-password":
|
|
|
|
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
|
2020-12-09 04:54:17 +08:00
|
|
|
usingEnvHashedPassword: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-03-03 04:02:51 +08:00
|
|
|
it("should use env var github token", async () => {
|
|
|
|
process.env.GITHUB_TOKEN = "ga-foo"
|
|
|
|
const args = parse([])
|
|
|
|
expect(args).toEqual({})
|
|
|
|
|
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
|
|
|
...defaults,
|
|
|
|
"github-auth": "ga-foo",
|
|
|
|
})
|
|
|
|
expect(process.env.GITHUB_TOKEN).toBe(undefined)
|
|
|
|
})
|
|
|
|
|
2022-04-14 00:39:05 +08:00
|
|
|
it("should use env var CS_DISABLE_FILE_DOWNLOADS", async () => {
|
|
|
|
process.env.CS_DISABLE_FILE_DOWNLOADS = "1"
|
|
|
|
const args = parse([])
|
2022-04-27 04:09:53 +08:00
|
|
|
expect(args).toEqual({})
|
|
|
|
|
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
|
|
|
...defaults,
|
|
|
|
"disable-file-downloads": true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use env var CS_DISABLE_FILE_DOWNLOADS set to true", async () => {
|
|
|
|
process.env.CS_DISABLE_FILE_DOWNLOADS = "true"
|
|
|
|
const args = parse([])
|
2022-04-14 00:39:05 +08:00
|
|
|
expect(args).toEqual({})
|
|
|
|
|
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
|
|
|
...defaults,
|
|
|
|
"disable-file-downloads": true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-10-29 00:59:24 +08:00
|
|
|
it("should use env var CS_DISABLE_GETTING_STARTED_OVERRIDE", async () => {
|
|
|
|
process.env.CS_DISABLE_GETTING_STARTED_OVERRIDE = "1"
|
|
|
|
const args = parse([])
|
|
|
|
expect(args).toEqual({})
|
|
|
|
|
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
|
|
|
...defaults,
|
|
|
|
"disable-getting-started-override": true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use env var CS_DISABLE_GETTING_STARTED_OVERRIDE set to true", async () => {
|
|
|
|
process.env.CS_DISABLE_GETTING_STARTED_OVERRIDE = "true"
|
|
|
|
const args = parse([])
|
|
|
|
expect(args).toEqual({})
|
|
|
|
|
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
|
|
|
...defaults,
|
|
|
|
"disable-getting-started-override": true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-07-22 07:23:21 +08:00
|
|
|
it("should use env var CS_DISABLE_PROXY", async () => {
|
|
|
|
process.env.CS_DISABLE_PROXY = "1"
|
|
|
|
const args = parse([])
|
|
|
|
expect(args).toEqual({})
|
|
|
|
|
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
|
|
|
...defaults,
|
|
|
|
"disable-proxy": true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use env var CS_DISABLE_PROXY set to true", async () => {
|
|
|
|
process.env.CS_DISABLE_PROXY = "true"
|
|
|
|
const args = parse([])
|
|
|
|
expect(args).toEqual({})
|
|
|
|
|
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
|
|
|
...defaults,
|
|
|
|
"disable-proxy": true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-05 06:04:07 +08:00
|
|
|
it("should error if password passed in", () => {
|
|
|
|
expect(() => parse(["--password", "supersecret123"])).toThrowError(
|
|
|
|
"--password can only be set in the config file or passed in via $PASSWORD",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should error if hashed-password passed in", () => {
|
|
|
|
expect(() => parse(["--hashed-password", "fdas423fs8a"])).toThrowError(
|
|
|
|
"--hashed-password can only be set in the config file or passed in via $HASHED_PASSWORD",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-03-03 04:02:51 +08:00
|
|
|
it("should error if github-auth passed in", () => {
|
|
|
|
expect(() => parse(["--github-auth", "fdas423fs8a"])).toThrowError(
|
|
|
|
"--github-auth can only be set in the config file or passed in via $GITHUB_TOKEN",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-10-16 05:17:04 +08:00
|
|
|
it("should filter proxy domains", async () => {
|
|
|
|
const args = parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "coder.com", "--proxy-domain", "coder.org"])
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(args).toEqual({
|
2020-10-16 05:17:04 +08:00
|
|
|
"proxy-domain": ["*.coder.com", "coder.com", "coder.org"],
|
|
|
|
})
|
|
|
|
|
2021-01-09 04:55:47 +08:00
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
2020-10-16 05:17:04 +08:00
|
|
|
...defaults,
|
2023-06-01 05:31:30 +08:00
|
|
|
"proxy-domain": ["{{port}}.coder.com", "{{port}}.coder.org"],
|
2020-10-16 05:17:04 +08:00
|
|
|
})
|
|
|
|
})
|
2021-06-04 07:30:33 +08:00
|
|
|
it("should allow '=,$/' in strings", async () => {
|
|
|
|
const args = parse([
|
2021-11-10 13:28:31 +08:00
|
|
|
"--disable-update-check",
|
2021-06-04 07:30:33 +08:00
|
|
|
"$argon2i$v=19$m=4096,t=3,p=1$0qr/o+0t00hsbjfqcksfdq$ofcm4rl6o+b7oxpua4qlxubypbbpsf+8l531u7p9hyy",
|
|
|
|
])
|
|
|
|
expect(args).toEqual({
|
2021-11-10 13:28:31 +08:00
|
|
|
"disable-update-check": true,
|
|
|
|
_: ["$argon2i$v=19$m=4096,t=3,p=1$0qr/o+0t00hsbjfqcksfdq$ofcm4rl6o+b7oxpua4qlxubypbbpsf+8l531u7p9hyy"],
|
2021-06-04 07:30:33 +08:00
|
|
|
})
|
|
|
|
})
|
2021-06-04 07:37:46 +08:00
|
|
|
it("should parse options with double-dash and multiple equal signs ", async () => {
|
|
|
|
const args = parse(
|
|
|
|
[
|
|
|
|
"--hashed-password=$argon2i$v=19$m=4096,t=3,p=1$0qr/o+0t00hsbjfqcksfdq$ofcm4rl6o+b7oxpua4qlxubypbbpsf+8l531u7p9hyy",
|
|
|
|
],
|
|
|
|
{
|
|
|
|
configFile: "/pathtoconfig",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
expect(args).toEqual({
|
|
|
|
"hashed-password":
|
|
|
|
"$argon2i$v=19$m=4096,t=3,p=1$0qr/o+0t00hsbjfqcksfdq$ofcm4rl6o+b7oxpua4qlxubypbbpsf+8l531u7p9hyy",
|
|
|
|
})
|
|
|
|
})
|
2022-02-16 07:19:22 +08:00
|
|
|
it("should throw an error for invalid config values", async () => {
|
|
|
|
const fakePath = "/fake-config-path"
|
|
|
|
const expectedErrMsg = `error reading ${fakePath}: `
|
|
|
|
|
|
|
|
expect(() =>
|
|
|
|
parse(["--foo"], {
|
|
|
|
configFile: fakePath,
|
|
|
|
}),
|
|
|
|
).toThrowError(expectedErrMsg)
|
|
|
|
})
|
2022-02-25 02:07:42 +08:00
|
|
|
it("should ignore optional strings set to false", async () => {
|
|
|
|
expect(parse(["--cert=false"])).toEqual({})
|
|
|
|
})
|
2022-03-03 04:02:51 +08:00
|
|
|
it("should use last flag", async () => {
|
|
|
|
expect(parse(["--port", "8081", "--port", "8082"])).toEqual({
|
|
|
|
port: 8082,
|
|
|
|
})
|
|
|
|
})
|
2023-03-31 04:01:49 +08:00
|
|
|
|
|
|
|
it("should not set proxy uri", async () => {
|
|
|
|
await setDefaults(parse([]))
|
|
|
|
expect(process.env.VSCODE_PROXY_URI).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should set proxy uri", async () => {
|
|
|
|
await setDefaults(parse(["--proxy-domain", "coder.org"]))
|
2023-06-01 05:31:30 +08:00
|
|
|
expect(process.env.VSCODE_PROXY_URI).toEqual("//{{port}}.coder.org")
|
2023-03-31 04:01:49 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should set proxy uri to first domain", async () => {
|
|
|
|
await setDefaults(
|
|
|
|
parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "coder.com", "--proxy-domain", "coder.org"]),
|
|
|
|
)
|
2023-06-01 05:31:30 +08:00
|
|
|
expect(process.env.VSCODE_PROXY_URI).toEqual("//{{port}}.coder.com")
|
2023-03-31 04:01:49 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should not override existing proxy uri", async () => {
|
|
|
|
process.env.VSCODE_PROXY_URI = "foo"
|
|
|
|
await setDefaults(
|
|
|
|
parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "coder.com", "--proxy-domain", "coder.org"]),
|
|
|
|
)
|
|
|
|
expect(process.env.VSCODE_PROXY_URI).toEqual("foo")
|
|
|
|
})
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
2020-09-16 05:51:43 +08:00
|
|
|
|
|
|
|
describe("cli", () => {
|
2021-12-18 03:06:52 +08:00
|
|
|
const testName = "cli"
|
2023-06-15 05:32:07 +08:00
|
|
|
let tmpDirPath: string
|
2020-09-16 05:51:43 +08:00
|
|
|
|
2021-01-09 04:55:47 +08:00
|
|
|
beforeAll(async () => {
|
2021-12-18 03:06:52 +08:00
|
|
|
await clean(testName)
|
2020-09-16 05:51:43 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
delete process.env.VSCODE_IPC_HOOK_CLI
|
2023-06-15 05:32:07 +08:00
|
|
|
tmpDirPath = await tmpdir(testName)
|
2020-09-16 05:51:43 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should use existing if inside code-server", async () => {
|
2021-01-13 02:53:12 +08:00
|
|
|
process.env.VSCODE_IPC_HOOK_CLI = "test"
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {}
|
2023-06-22 14:47:01 +08:00
|
|
|
expect(await shouldOpenInExistingInstance(args, "")).toStrictEqual("test")
|
2020-09-16 05:51:43 +08:00
|
|
|
|
|
|
|
args.port = 8081
|
2021-11-10 13:28:31 +08:00
|
|
|
args._ = ["./file"]
|
2023-06-22 14:47:01 +08:00
|
|
|
expect(await shouldOpenInExistingInstance(args, "")).toStrictEqual("test")
|
2020-09-16 05:51:43 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should use existing if --reuse-window is set", async () => {
|
2023-06-22 14:47:01 +08:00
|
|
|
const sessionSocket = path.join(tmpDirPath, "session-socket")
|
|
|
|
const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager())
|
2023-06-15 05:32:07 +08:00
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {}
|
2020-09-16 05:51:43 +08:00
|
|
|
args["reuse-window"] = true
|
2023-06-22 14:47:01 +08:00
|
|
|
await expect(shouldOpenInExistingInstance(args, sessionSocket)).rejects.toThrow()
|
2020-09-16 05:51:43 +08:00
|
|
|
|
2023-06-15 05:32:07 +08:00
|
|
|
const socketPath = path.join(tmpDirPath, "socket")
|
2023-06-22 14:47:01 +08:00
|
|
|
const client = new EditorSessionManagerClient(sessionSocket)
|
2023-06-15 05:32:07 +08:00
|
|
|
await client.addSession({
|
|
|
|
entry: {
|
|
|
|
workspace: {
|
|
|
|
id: "aaa",
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
uri: {
|
|
|
|
path: "/aaa",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
socketPath,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const vscodeSockets = listenOn(socketPath)
|
|
|
|
|
2023-06-22 14:47:01 +08:00
|
|
|
await expect(shouldOpenInExistingInstance(args, sessionSocket)).resolves.toStrictEqual(socketPath)
|
2020-09-16 05:51:43 +08:00
|
|
|
|
|
|
|
args.port = 8081
|
2023-06-22 14:47:01 +08:00
|
|
|
await expect(shouldOpenInExistingInstance(args, sessionSocket)).resolves.toStrictEqual(socketPath)
|
2023-06-15 05:32:07 +08:00
|
|
|
|
|
|
|
server.close()
|
|
|
|
vscodeSockets.close()
|
2020-09-16 05:51:43 +08:00
|
|
|
})
|
2021-01-13 02:53:12 +08:00
|
|
|
|
2020-09-16 05:51:43 +08:00
|
|
|
it("should use existing if --new-window is set", async () => {
|
2023-06-22 14:47:01 +08:00
|
|
|
const sessionSocket = path.join(tmpDirPath, "session-socket")
|
|
|
|
const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager())
|
2023-06-15 05:32:07 +08:00
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {}
|
2020-09-16 05:51:43 +08:00
|
|
|
args["new-window"] = true
|
2023-06-22 14:47:01 +08:00
|
|
|
await expect(shouldOpenInExistingInstance(args, sessionSocket)).rejects.toThrow()
|
2020-09-16 05:51:43 +08:00
|
|
|
|
2023-06-15 05:32:07 +08:00
|
|
|
const socketPath = path.join(tmpDirPath, "socket")
|
2023-06-22 14:47:01 +08:00
|
|
|
const client = new EditorSessionManagerClient(sessionSocket)
|
2023-06-15 05:32:07 +08:00
|
|
|
await client.addSession({
|
|
|
|
entry: {
|
|
|
|
workspace: {
|
|
|
|
id: "aaa",
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
uri: {
|
|
|
|
path: "/aaa",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
socketPath,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const vscodeSockets = listenOn(socketPath)
|
|
|
|
|
2023-06-22 14:47:01 +08:00
|
|
|
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(socketPath)
|
2020-09-16 05:51:43 +08:00
|
|
|
|
|
|
|
args.port = 8081
|
2023-06-22 14:47:01 +08:00
|
|
|
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(socketPath)
|
2023-06-15 05:32:07 +08:00
|
|
|
|
|
|
|
server.close()
|
|
|
|
vscodeSockets.close()
|
2020-09-16 05:51:43 +08:00
|
|
|
})
|
|
|
|
|
2021-01-13 02:53:12 +08:00
|
|
|
it("should use existing if no unrelated flags are set, has positional, and socket is active", async () => {
|
2023-06-22 14:47:01 +08:00
|
|
|
const sessionSocket = path.join(tmpDirPath, "session-socket")
|
|
|
|
const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager())
|
2023-06-15 05:32:07 +08:00
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {}
|
2023-06-22 14:47:01 +08:00
|
|
|
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(undefined)
|
2021-01-13 02:53:12 +08:00
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
args._ = ["./file"]
|
2023-06-22 14:47:01 +08:00
|
|
|
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(undefined)
|
2021-01-13 02:53:12 +08:00
|
|
|
|
2023-06-22 14:47:01 +08:00
|
|
|
const client = new EditorSessionManagerClient(sessionSocket)
|
2023-06-15 05:32:07 +08:00
|
|
|
const socketPath = path.join(tmpDirPath, "socket")
|
|
|
|
await client.addSession({
|
|
|
|
entry: {
|
|
|
|
workspace: {
|
|
|
|
id: "aaa",
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
uri: {
|
|
|
|
path: "/aaa",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
socketPath,
|
|
|
|
},
|
2021-01-13 02:53:12 +08:00
|
|
|
})
|
2023-06-15 05:32:07 +08:00
|
|
|
const vscodeSockets = listenOn(socketPath)
|
2020-09-16 05:51:43 +08:00
|
|
|
|
2023-06-22 14:47:01 +08:00
|
|
|
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(socketPath)
|
2020-09-16 05:51:43 +08:00
|
|
|
|
2021-01-13 02:53:12 +08:00
|
|
|
args.port = 8081
|
2023-06-22 14:47:01 +08:00
|
|
|
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(undefined)
|
2023-06-15 05:32:07 +08:00
|
|
|
|
|
|
|
server.close()
|
|
|
|
vscodeSockets.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should prefer matching sessions for only the first path", async () => {
|
2023-06-22 14:47:01 +08:00
|
|
|
const sessionSocket = path.join(tmpDirPath, "session-socket")
|
|
|
|
const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager())
|
|
|
|
const client = new EditorSessionManagerClient(sessionSocket)
|
2023-06-15 05:32:07 +08:00
|
|
|
await client.addSession({
|
|
|
|
entry: {
|
|
|
|
workspace: {
|
|
|
|
id: "aaa",
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
uri: {
|
|
|
|
path: "/aaa",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
socketPath: `${tmpDirPath}/vscode-ipc-aaa.sock`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await client.addSession({
|
|
|
|
entry: {
|
|
|
|
workspace: {
|
|
|
|
id: "bbb",
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
uri: {
|
|
|
|
path: "/bbb",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
socketPath: `${tmpDirPath}/vscode-ipc-bbb.sock`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
listenOn(`${tmpDirPath}/vscode-ipc-aaa.sock`, `${tmpDirPath}/vscode-ipc-bbb.sock`)
|
|
|
|
|
|
|
|
const args: UserProvidedArgs = {}
|
|
|
|
args._ = ["/aaa/file", "/bbb/file"]
|
2023-06-22 14:47:01 +08:00
|
|
|
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(`${tmpDirPath}/vscode-ipc-aaa.sock`)
|
2023-06-15 05:32:07 +08:00
|
|
|
|
|
|
|
server.close()
|
2021-01-13 02:53:12 +08:00
|
|
|
})
|
2020-09-16 05:51:43 +08:00
|
|
|
})
|
2021-06-04 07:30:33 +08:00
|
|
|
|
2021-10-29 04:27:17 +08:00
|
|
|
describe("shouldSpawnCliProcess", () => {
|
|
|
|
it("should return false if no 'extension' related args passed in", async () => {
|
2021-11-10 13:28:31 +08:00
|
|
|
const args = {}
|
2021-10-29 04:27:17 +08:00
|
|
|
const actual = await shouldSpawnCliProcess(args)
|
2021-09-21 05:53:09 +08:00
|
|
|
const expected = false
|
|
|
|
|
|
|
|
expect(actual).toBe(expected)
|
|
|
|
})
|
|
|
|
|
2021-10-29 04:27:17 +08:00
|
|
|
it("should return true if 'list-extensions' passed in", async () => {
|
2021-09-21 05:53:09 +08:00
|
|
|
const args = {
|
|
|
|
["list-extensions"]: true,
|
|
|
|
}
|
2021-10-29 04:27:17 +08:00
|
|
|
const actual = await shouldSpawnCliProcess(args)
|
2021-09-21 05:53:09 +08:00
|
|
|
const expected = true
|
|
|
|
|
|
|
|
expect(actual).toBe(expected)
|
|
|
|
})
|
|
|
|
|
2021-10-29 04:27:17 +08:00
|
|
|
it("should return true if 'install-extension' passed in", async () => {
|
2021-09-21 05:53:09 +08:00
|
|
|
const args = {
|
|
|
|
["install-extension"]: ["hello.world"],
|
|
|
|
}
|
2021-10-29 04:27:17 +08:00
|
|
|
const actual = await shouldSpawnCliProcess(args)
|
2021-09-21 05:53:09 +08:00
|
|
|
const expected = true
|
|
|
|
|
|
|
|
expect(actual).toBe(expected)
|
|
|
|
})
|
|
|
|
|
2021-10-29 04:27:17 +08:00
|
|
|
it("should return true if 'uninstall-extension' passed in", async () => {
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {
|
2021-09-21 05:53:09 +08:00
|
|
|
["uninstall-extension"]: ["hello.world"],
|
|
|
|
}
|
2021-10-29 04:27:17 +08:00
|
|
|
const actual = await shouldSpawnCliProcess(args)
|
2021-09-21 05:53:09 +08:00
|
|
|
const expected = true
|
|
|
|
|
|
|
|
expect(actual).toBe(expected)
|
|
|
|
})
|
|
|
|
})
|
2021-09-22 01:41:33 +08:00
|
|
|
|
|
|
|
describe("bindAddrFromArgs", () => {
|
|
|
|
it("should return the bind address", () => {
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {}
|
2021-09-22 01:41:33 +08:00
|
|
|
|
|
|
|
const addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
const actual = bindAddrFromArgs(addr, args)
|
|
|
|
const expected = addr
|
|
|
|
|
|
|
|
expect(actual).toStrictEqual(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use the bind-address if set in args", () => {
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {
|
2021-09-22 01:41:33 +08:00
|
|
|
["bind-addr"]: "localhost:3000",
|
|
|
|
}
|
|
|
|
|
|
|
|
const addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
const actual = bindAddrFromArgs(addr, args)
|
|
|
|
const expected = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 3000,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(actual).toStrictEqual(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use the host if set in args", () => {
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {
|
2021-09-22 01:41:33 +08:00
|
|
|
["host"]: "coder",
|
|
|
|
}
|
|
|
|
|
|
|
|
const addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
const actual = bindAddrFromArgs(addr, args)
|
|
|
|
const expected = {
|
|
|
|
host: "coder",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(actual).toStrictEqual(expected)
|
|
|
|
})
|
|
|
|
|
2023-09-09 06:50:11 +08:00
|
|
|
it("should use process.env.CODE_SERVER_HOST if set", () => {
|
|
|
|
const [setValue, resetValue] = useEnv("CODE_SERVER_HOST")
|
|
|
|
setValue("coder")
|
|
|
|
|
|
|
|
const args: UserProvidedArgs = {}
|
|
|
|
|
|
|
|
const addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
const actual = bindAddrFromArgs(addr, args)
|
|
|
|
const expected = {
|
|
|
|
host: "coder",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(actual).toStrictEqual(expected)
|
|
|
|
resetValue()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use the args.host over process.env.CODE_SERVER_HOST if both set", () => {
|
|
|
|
const [setValue, resetValue] = useEnv("CODE_SERVER_HOST")
|
|
|
|
setValue("coder")
|
|
|
|
|
|
|
|
const args: UserProvidedArgs = {
|
|
|
|
host: "123.123.123.123",
|
|
|
|
}
|
|
|
|
|
|
|
|
const addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
const actual = bindAddrFromArgs(addr, args)
|
|
|
|
const expected = {
|
|
|
|
host: "123.123.123.123",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(actual).toStrictEqual(expected)
|
|
|
|
resetValue()
|
|
|
|
})
|
|
|
|
|
2021-09-22 01:41:33 +08:00
|
|
|
it("should use process.env.PORT if set", () => {
|
|
|
|
const [setValue, resetValue] = useEnv("PORT")
|
|
|
|
setValue("8000")
|
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {}
|
2021-09-22 01:41:33 +08:00
|
|
|
|
|
|
|
const addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
const actual = bindAddrFromArgs(addr, args)
|
|
|
|
const expected = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8000,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(actual).toStrictEqual(expected)
|
|
|
|
resetValue()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should set port if in args", () => {
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {
|
2021-09-22 01:41:33 +08:00
|
|
|
port: 3000,
|
|
|
|
}
|
|
|
|
|
|
|
|
const addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
const actual = bindAddrFromArgs(addr, args)
|
|
|
|
const expected = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 3000,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(actual).toStrictEqual(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use the args.port over process.env.PORT if both set", () => {
|
|
|
|
const [setValue, resetValue] = useEnv("PORT")
|
|
|
|
setValue("8000")
|
|
|
|
|
2021-11-10 13:28:31 +08:00
|
|
|
const args: UserProvidedArgs = {
|
2021-09-22 01:41:33 +08:00
|
|
|
port: 3000,
|
|
|
|
}
|
|
|
|
|
|
|
|
const addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
const actual = bindAddrFromArgs(addr, args)
|
|
|
|
const expected = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 3000,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(actual).toStrictEqual(expected)
|
|
|
|
resetValue()
|
|
|
|
})
|
|
|
|
})
|
2021-09-29 06:45:44 +08:00
|
|
|
|
|
|
|
describe("defaultConfigFile", () => {
|
2021-09-29 06:51:00 +08:00
|
|
|
it("should return the default config file as a string", async () => {
|
|
|
|
const password = await generatePassword()
|
|
|
|
const actual = defaultConfigFile(password)
|
|
|
|
|
|
|
|
expect(actual).toMatch(`bind-addr: 127.0.0.1:8080
|
|
|
|
auth: password
|
|
|
|
password: ${password}
|
|
|
|
cert: false`)
|
2021-09-29 06:45:44 +08:00
|
|
|
})
|
|
|
|
})
|
2021-10-30 07:03:57 +08:00
|
|
|
|
2022-03-23 04:07:14 +08:00
|
|
|
describe("toCodeArgs", () => {
|
2021-12-11 02:01:35 +08:00
|
|
|
const vscodeDefaults = {
|
|
|
|
...defaults,
|
|
|
|
help: false,
|
|
|
|
port: "8080",
|
|
|
|
version: false,
|
2022-11-10 06:10:03 +08:00
|
|
|
log: undefined,
|
2021-12-11 02:01:35 +08:00
|
|
|
}
|
|
|
|
|
2021-12-18 03:06:52 +08:00
|
|
|
const testName = "vscode-args"
|
2021-12-11 02:01:35 +08:00
|
|
|
beforeAll(async () => {
|
|
|
|
// Clean up temporary directories from the previous run.
|
2021-12-18 03:06:52 +08:00
|
|
|
await clean(testName)
|
2021-12-11 02:01:35 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should convert empty args", async () => {
|
2022-03-23 04:07:14 +08:00
|
|
|
expect(await toCodeArgs(await setDefaults(parse([])))).toStrictEqual({
|
2021-12-11 02:01:35 +08:00
|
|
|
...vscodeDefaults,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should ignore regular file", async () => {
|
2021-12-18 03:06:52 +08:00
|
|
|
const file = path.join(await tmpdir(testName), "file")
|
2021-12-11 02:01:35 +08:00
|
|
|
await fs.writeFile(file, "foobar")
|
2022-03-23 04:07:14 +08:00
|
|
|
expect(await toCodeArgs(await setDefaults(parse([file])))).toStrictEqual({
|
2021-12-11 02:01:35 +08:00
|
|
|
...vscodeDefaults,
|
|
|
|
_: [file],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2022-03-12 04:27:19 +08:00
|
|
|
|
|
|
|
describe("optionDescriptions", () => {
|
|
|
|
it("should return the descriptions of all the available options", () => {
|
|
|
|
const expectedOptionDescriptions = Object.entries(options)
|
|
|
|
.flat()
|
|
|
|
.filter((item: any) => {
|
|
|
|
if (item.description) {
|
|
|
|
return item.description
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map((item: any) => item.description)
|
|
|
|
const actualOptionDescriptions = optionDescriptions()
|
|
|
|
// We need both the expected and the actual
|
|
|
|
// Both of these are string[]
|
|
|
|
// We then loop through the expectedOptionDescriptions
|
|
|
|
// and check that this expectedDescription exists in the
|
|
|
|
// actualOptionDescriptions
|
|
|
|
|
|
|
|
// To do that we need to loop through actualOptionDescriptions
|
|
|
|
// and make sure we have a substring match
|
|
|
|
expectedOptionDescriptions.forEach((expectedDescription) => {
|
|
|
|
const exists = actualOptionDescriptions.find((desc) => {
|
|
|
|
if (
|
|
|
|
desc.replace(/\n/g, " ").replace(/ /g, "").includes(expectedDescription.replace(/\n/g, " ").replace(/ /g, ""))
|
|
|
|
) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
expect(exists).toBeTruthy()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it("should visually align multiple options", () => {
|
|
|
|
const opts: Partial<Options<Required<UserProvidedArgs>>> = {
|
|
|
|
"cert-key": { type: "string", path: true, description: "Path to certificate key when using non-generated cert." },
|
|
|
|
"cert-host": {
|
|
|
|
type: "string",
|
|
|
|
description: "Hostname to use when generating a self signed certificate.",
|
|
|
|
},
|
|
|
|
"disable-update-check": {
|
|
|
|
type: "boolean",
|
|
|
|
description:
|
|
|
|
"Disable update check. Without this flag, code-server checks every 6 hours against the latest github release and \n" +
|
|
|
|
"then notifies you once every week that a new release is available.",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect(optionDescriptions(opts)).toStrictEqual([
|
|
|
|
" --cert-key Path to certificate key when using non-generated cert.",
|
|
|
|
" --cert-host Hostname to use when generating a self signed certificate.",
|
|
|
|
` --disable-update-check Disable update check. Without this flag, code-server checks every 6 hours against the latest github release and
|
|
|
|
then notifies you once every week that a new release is available.`,
|
|
|
|
])
|
|
|
|
})
|
|
|
|
it("should add all valid options for enumerated types", () => {
|
|
|
|
const opts: Partial<Options<Required<UserProvidedArgs>>> = {
|
|
|
|
auth: { type: AuthType, description: "The type of authentication to use." },
|
|
|
|
}
|
|
|
|
expect(optionDescriptions(opts)).toStrictEqual([" --auth The type of authentication to use. [password, none]"])
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should show if an option is deprecated", () => {
|
|
|
|
const opts: Partial<Options<Required<UserProvidedArgs>>> = {
|
2023-02-14 06:52:48 +08:00
|
|
|
cert: {
|
2022-03-12 04:27:19 +08:00
|
|
|
type: OptionalString,
|
2023-02-14 06:52:48 +08:00
|
|
|
description: "foo",
|
2022-03-12 04:27:19 +08:00
|
|
|
deprecated: true,
|
|
|
|
},
|
|
|
|
}
|
2023-02-14 06:52:48 +08:00
|
|
|
expect(optionDescriptions(opts)).toStrictEqual([" --cert (deprecated) foo"])
|
2022-03-12 04:27:19 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should show newlines in description", () => {
|
|
|
|
const opts: Partial<Options<Required<UserProvidedArgs>>> = {
|
|
|
|
"install-extension": {
|
|
|
|
type: "string[]",
|
|
|
|
description:
|
|
|
|
"Install or update a VS Code extension by id or vsix. The identifier of an extension is `${publisher}.${name}`.\n" +
|
|
|
|
"To install a specific version provide `@${version}`. For example: 'vscode.csharp@1.2.3'.",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect(optionDescriptions(opts)).toStrictEqual([
|
|
|
|
` --install-extension Install or update a VS Code extension by id or vsix. The identifier of an extension is \`\${publisher}.\${name}\`.
|
|
|
|
To install a specific version provide \`@\${version}\`. For example: 'vscode.csharp@1.2.3'.`,
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|