2020-09-16 02:43:07 +08:00
|
|
|
import { Level, logger } from "@coder/logger"
|
2020-02-07 08:26:07 +08:00
|
|
|
import * as assert from "assert"
|
2020-09-16 05:51:43 +08:00
|
|
|
import * as fs from "fs-extra"
|
|
|
|
import * as net from "net"
|
|
|
|
import * as os from "os"
|
2020-02-07 08:26:07 +08:00
|
|
|
import * as path from "path"
|
2020-09-16 05:51:43 +08:00
|
|
|
import { Args, parse, setDefaults, shouldOpenInExistingInstance } from "../src/node/cli"
|
|
|
|
import { paths, tmpdir } from "../src/node/util"
|
2020-02-07 08:26:07 +08:00
|
|
|
|
2020-09-16 05:51:43 +08:00
|
|
|
type Mutable<T> = {
|
|
|
|
-readonly [P in keyof T]: T[P]
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-02-19 06:51:55 +08:00
|
|
|
})
|
|
|
|
|
2020-09-16 02:43:07 +08:00
|
|
|
// The parser should not set any defaults so the caller can determine what
|
2020-09-16 05:51:43 +08:00
|
|
|
// values the user actually set. These are only set after explicitly calling
|
|
|
|
// `setDefaults`.
|
2020-04-29 05:39:01 +08:00
|
|
|
const defaults = {
|
2020-10-16 05:17:04 +08:00
|
|
|
auth: "password",
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
"proxy-domain": [],
|
|
|
|
usingEnvPassword: false,
|
2020-12-09 04:54:17 +08:00
|
|
|
usingEnvHashedPassword: false,
|
2020-09-16 02:43:07 +08:00
|
|
|
"extensions-dir": path.join(paths.data, "extensions"),
|
|
|
|
"user-data-dir": paths.data,
|
2020-04-29 05:39:01 +08:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:17:04 +08:00
|
|
|
it("should parse nothing", () => {
|
2020-09-16 02:43:07 +08:00
|
|
|
assert.deepEqual(parse([]), { _: [] })
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
|
|
|
|
2020-05-19 12:39:57 +08:00
|
|
|
it("should parse all available options", () => {
|
2020-02-07 08:26:07 +08:00
|
|
|
assert.deepEqual(
|
2020-05-19 12:39:57 +08:00
|
|
|
parse([
|
2020-04-27 21:22:52 +08:00
|
|
|
"--bind-addr=192.169.0.1:8080",
|
2020-02-07 08:26:07 +08:00
|
|
|
"--auth",
|
|
|
|
"none",
|
|
|
|
"--extensions-dir",
|
|
|
|
"foo",
|
|
|
|
"--builtin-extensions-dir",
|
|
|
|
"foobar",
|
|
|
|
"--extra-extensions-dir",
|
|
|
|
"nozzle",
|
|
|
|
"1",
|
|
|
|
"--extra-builtin-extensions-dir",
|
|
|
|
"bazzle",
|
|
|
|
"--verbose",
|
|
|
|
"2",
|
|
|
|
"--log",
|
|
|
|
"error",
|
|
|
|
"--help",
|
2020-12-11 01:24:37 +08:00
|
|
|
"--home=http://localhost:8080/",
|
2020-02-07 08:26:07 +08:00
|
|
|
"--open",
|
2020-02-20 00:54:23 +08:00
|
|
|
"--socket=mumble",
|
2020-02-07 08:26:07 +08:00
|
|
|
"3",
|
|
|
|
"--user-data-dir",
|
|
|
|
"bar",
|
2020-02-20 00:54:23 +08:00
|
|
|
"--cert=baz",
|
2020-02-07 08:26:07 +08:00
|
|
|
"--cert-key",
|
|
|
|
"qux",
|
|
|
|
"--version",
|
|
|
|
"--json",
|
2020-02-20 00:54:23 +08:00
|
|
|
"--port=8081",
|
2020-02-07 08:26:07 +08:00
|
|
|
"--host",
|
|
|
|
"0.0.0.0",
|
|
|
|
"4",
|
|
|
|
"--",
|
|
|
|
"-5",
|
|
|
|
"--6",
|
|
|
|
]),
|
|
|
|
{
|
|
|
|
_: ["1", "2", "3", "4", "-5", "--6"],
|
|
|
|
auth: "none",
|
|
|
|
"builtin-extensions-dir": path.resolve("foobar"),
|
|
|
|
"cert-key": path.resolve("qux"),
|
|
|
|
cert: {
|
|
|
|
value: path.resolve("baz"),
|
|
|
|
},
|
|
|
|
"extensions-dir": path.resolve("foo"),
|
|
|
|
"extra-builtin-extensions-dir": [path.resolve("bazzle")],
|
|
|
|
"extra-extensions-dir": [path.resolve("nozzle")],
|
|
|
|
help: true,
|
2020-12-11 01:24:37 +08:00
|
|
|
home: "http://localhost:8080/",
|
2020-02-07 08:26:07 +08:00
|
|
|
host: "0.0.0.0",
|
|
|
|
json: true,
|
2020-09-16 02:43:07 +08:00
|
|
|
log: "error",
|
2020-02-07 08:26:07 +08:00
|
|
|
open: true,
|
|
|
|
port: 8081,
|
|
|
|
socket: path.resolve("mumble"),
|
|
|
|
"user-data-dir": path.resolve("bar"),
|
|
|
|
verbose: true,
|
|
|
|
version: true,
|
2020-04-27 21:22:52 +08:00
|
|
|
"bind-addr": "192.169.0.1:8080",
|
2020-02-15 08:46:00 +08:00
|
|
|
},
|
2020-02-07 08:26:07 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-19 12:39:57 +08:00
|
|
|
it("should work with short options", () => {
|
|
|
|
assert.deepEqual(parse(["-vvv", "-v"]), {
|
2020-09-16 02:43:07 +08:00
|
|
|
_: [],
|
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([])
|
|
|
|
assert.deepEqual(args, { _: [] })
|
|
|
|
|
2020-02-19 06:51:55 +08:00
|
|
|
process.env.LOG_LEVEL = "debug"
|
2020-09-16 02:43:07 +08:00
|
|
|
assert.deepEqual(await setDefaults(args), {
|
2020-04-29 05:39:01 +08:00
|
|
|
...defaults,
|
2020-09-16 02:43:07 +08:00
|
|
|
_: [],
|
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
|
|
|
})
|
|
|
|
assert.equal(process.env.LOG_LEVEL, "debug")
|
2020-04-29 05:39:01 +08:00
|
|
|
assert.equal(logger.level, Level.Debug)
|
|
|
|
|
|
|
|
process.env.LOG_LEVEL = "trace"
|
2020-09-16 02:43:07 +08:00
|
|
|
assert.deepEqual(await setDefaults(args), {
|
2020-04-29 05:39:01 +08:00
|
|
|
...defaults,
|
2020-09-16 02:43:07 +08:00
|
|
|
_: [],
|
2020-04-29 05:39:01 +08:00
|
|
|
log: "trace",
|
|
|
|
verbose: true,
|
|
|
|
})
|
|
|
|
assert.equal(process.env.LOG_LEVEL, "trace")
|
|
|
|
assert.equal(logger.level, 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"])
|
|
|
|
assert.deepEqual(args, {
|
|
|
|
_: [],
|
|
|
|
log: "info",
|
|
|
|
})
|
|
|
|
|
2020-02-19 06:51:55 +08:00
|
|
|
process.env.LOG_LEVEL = "debug"
|
2020-09-16 02:43:07 +08:00
|
|
|
assert.deepEqual(await setDefaults(args), {
|
2020-04-29 05:39:01 +08:00
|
|
|
...defaults,
|
2020-09-16 02:43:07 +08:00
|
|
|
_: [],
|
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
|
|
|
})
|
|
|
|
assert.equal(process.env.LOG_LEVEL, "info")
|
2020-04-29 05:39:01 +08:00
|
|
|
assert.equal(logger.level, Level.Info)
|
|
|
|
|
|
|
|
process.env.LOG_LEVEL = "trace"
|
2020-09-16 02:43:07 +08:00
|
|
|
assert.deepEqual(await setDefaults(args), {
|
2020-04-29 05:39:01 +08:00
|
|
|
...defaults,
|
2020-09-16 02:43:07 +08:00
|
|
|
_: [],
|
2020-04-29 05:39:01 +08:00
|
|
|
log: "info",
|
2020-05-19 12:39:57 +08:00
|
|
|
verbose: false,
|
2020-04-29 05:39:01 +08:00
|
|
|
})
|
|
|
|
assert.equal(process.env.LOG_LEVEL, "info")
|
|
|
|
assert.equal(logger.level, Level.Info)
|
|
|
|
|
2020-09-16 02:43:07 +08:00
|
|
|
args = parse(["--log", "info", "--verbose"])
|
|
|
|
assert.deepEqual(args, {
|
|
|
|
_: [],
|
|
|
|
log: "info",
|
|
|
|
verbose: true,
|
|
|
|
})
|
|
|
|
|
2020-04-29 05:39:01 +08:00
|
|
|
process.env.LOG_LEVEL = "warn"
|
2020-09-16 02:43:07 +08:00
|
|
|
assert.deepEqual(await setDefaults(args), {
|
2020-04-29 05:39:01 +08:00
|
|
|
...defaults,
|
2020-09-16 02:43:07 +08:00
|
|
|
_: [],
|
2020-04-29 05:39:01 +08:00
|
|
|
log: "trace",
|
|
|
|
verbose: true,
|
|
|
|
})
|
|
|
|
assert.equal(process.env.LOG_LEVEL, "trace")
|
|
|
|
assert.equal(logger.level, Level.Trace)
|
|
|
|
})
|
|
|
|
|
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"
|
2020-09-16 02:43:07 +08:00
|
|
|
assert.deepEqual(await setDefaults(parse([])), {
|
|
|
|
_: [],
|
|
|
|
...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", () => {
|
|
|
|
assert.throws(() => parse(["--auth"]), /--auth requires a value/)
|
|
|
|
assert.throws(() => parse(["--auth=", "--log=debug"]), /--auth requires a value/)
|
|
|
|
assert.throws(() => parse(["--auth", "--log"]), /--auth requires a value/)
|
|
|
|
assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/)
|
|
|
|
assert.throws(() => parse(["--bind-addr"]), /--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", () => {
|
|
|
|
assert.throws(() => parse(["--port", "foo"]), /--port must be a number/)
|
|
|
|
assert.throws(() => parse(["--auth", "invalid"]), /--auth valid values: \[password, none\]/)
|
|
|
|
assert.throws(() => parse(["--log", "invalid"]), /--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", () => {
|
|
|
|
assert.throws(() => parse(["--foo"]), /Unknown option --foo/)
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
|
|
|
|
2020-05-19 12:39:57 +08:00
|
|
|
it("should not error if the value is optional", () => {
|
|
|
|
assert.deepEqual(parse(["--cert"]), {
|
2020-09-16 02:43:07 +08:00
|
|
|
_: [],
|
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", () => {
|
|
|
|
assert.throws(() => parse(["--socket", "--socket-path-value"]), /--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:
|
2020-05-19 12:39:57 +08:00
|
|
|
assert.deepEqual(parse(["--socket", "./--socket-path-value"]), {
|
2020-09-16 02:43:07 +08:00
|
|
|
_: [],
|
2020-02-07 08:26:07 +08:00
|
|
|
socket: path.resolve("--socket-path-value"),
|
|
|
|
})
|
2020-05-19 12:39:57 +08:00
|
|
|
assert.throws(() => parse(["--cert", "--socket-path-value"]), /Unknown option --socket-path-value/)
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
2020-02-21 08:44:03 +08:00
|
|
|
|
2020-05-19 12:39:57 +08:00
|
|
|
it("should allow positional arguments before options", () => {
|
|
|
|
assert.deepEqual(parse(["foo", "test", "--auth", "none"]), {
|
2020-02-21 08:44:03 +08:00
|
|
|
_: ["foo", "test"],
|
|
|
|
auth: "none",
|
|
|
|
})
|
|
|
|
})
|
2020-03-24 01:08:50 +08:00
|
|
|
|
2020-05-19 12:39:57 +08:00
|
|
|
it("should support repeatable flags", () => {
|
|
|
|
assert.deepEqual(parse(["--proxy-domain", "*.coder.com"]), {
|
2020-09-16 02:43:07 +08:00
|
|
|
_: [],
|
2020-03-24 01:08:50 +08:00
|
|
|
"proxy-domain": ["*.coder.com"],
|
|
|
|
})
|
2020-05-19 12:39:57 +08:00
|
|
|
assert.deepEqual(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), {
|
2020-09-16 02:43:07 +08:00
|
|
|
_: [],
|
2020-03-24 01:08:50 +08:00
|
|
|
"proxy-domain": ["*.coder.com", "test.com"],
|
|
|
|
})
|
|
|
|
})
|
2020-10-16 05:17:04 +08:00
|
|
|
|
|
|
|
it("should enforce cert-key with cert value or otherwise generate one", async () => {
|
|
|
|
const args = parse(["--cert"])
|
|
|
|
assert.deepEqual(args, {
|
|
|
|
_: [],
|
|
|
|
cert: {
|
|
|
|
value: undefined,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.throws(() => parse(["--cert", "test"]), /--cert-key is missing/)
|
|
|
|
assert.deepEqual(await setDefaults(args), {
|
|
|
|
_: [],
|
|
|
|
...defaults,
|
|
|
|
cert: {
|
2020-11-13 02:29:41 +08:00
|
|
|
value: path.join(paths.data, "localhost.crt"),
|
2020-10-16 05:17:04 +08:00
|
|
|
},
|
2020-11-13 02:29:41 +08:00
|
|
|
"cert-key": path.join(paths.data, "localhost.key"),
|
2020-10-16 05:17:04 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should override with --link", async () => {
|
|
|
|
const args = parse("--cert test --cert-key test --socket test --host 0.0.0.0 --port 8888 --link test".split(" "))
|
|
|
|
assert.deepEqual(await setDefaults(args), {
|
|
|
|
_: [],
|
|
|
|
...defaults,
|
|
|
|
auth: "none",
|
|
|
|
host: "localhost",
|
|
|
|
link: {
|
|
|
|
value: "test",
|
|
|
|
},
|
|
|
|
port: 0,
|
|
|
|
cert: undefined,
|
|
|
|
"cert-key": path.resolve("test"),
|
|
|
|
socket: undefined,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use env var password", async () => {
|
|
|
|
process.env.PASSWORD = "test"
|
|
|
|
const args = parse([])
|
|
|
|
assert.deepEqual(args, {
|
|
|
|
_: [],
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.deepEqual(await setDefaults(args), {
|
|
|
|
...defaults,
|
|
|
|
_: [],
|
|
|
|
password: "test",
|
|
|
|
usingEnvPassword: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-12-09 04:54:17 +08:00
|
|
|
it("should use env var hashed password", async () => {
|
|
|
|
process.env.HASHED_PASSWORD = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" // test
|
|
|
|
const args = parse([])
|
|
|
|
assert.deepEqual(args, {
|
|
|
|
_: [],
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.deepEqual(await setDefaults(args), {
|
|
|
|
...defaults,
|
|
|
|
_: [],
|
2020-12-19 01:20:38 +08:00
|
|
|
"hashed-password": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
|
2020-12-09 04:54:17 +08:00
|
|
|
usingEnvHashedPassword: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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"])
|
|
|
|
assert.deepEqual(args, {
|
|
|
|
_: [],
|
|
|
|
"proxy-domain": ["*.coder.com", "coder.com", "coder.org"],
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.deepEqual(await setDefaults(args), {
|
|
|
|
...defaults,
|
|
|
|
_: [],
|
|
|
|
"proxy-domain": ["coder.com", "coder.org"],
|
|
|
|
})
|
|
|
|
})
|
2020-02-07 08:26:07 +08:00
|
|
|
})
|
2020-09-16 05:51:43 +08:00
|
|
|
|
|
|
|
describe("cli", () => {
|
|
|
|
let args: Mutable<Args> = { _: [] }
|
|
|
|
const testDir = path.join(tmpdir, "tests/cli")
|
|
|
|
const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc")
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
await fs.remove(testDir)
|
|
|
|
await fs.mkdirp(testDir)
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
delete process.env.VSCODE_IPC_HOOK_CLI
|
|
|
|
args = { _: [] }
|
|
|
|
await fs.remove(vscodeIpcPath)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use existing if inside code-server", async () => {
|
|
|
|
process.env.VSCODE_IPC_HOOK_CLI = "test"
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
|
|
|
|
|
|
|
args.port = 8081
|
|
|
|
args._.push("./file")
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use existing if --reuse-window is set", async () => {
|
|
|
|
args["reuse-window"] = true
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
|
|
|
|
|
|
|
await fs.writeFile(vscodeIpcPath, "test")
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
|
|
|
|
|
|
|
args.port = 8081
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use existing if --new-window is set", async () => {
|
|
|
|
args["new-window"] = true
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
|
|
|
|
|
|
|
await fs.writeFile(vscodeIpcPath, "test")
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
|
|
|
|
|
|
|
args.port = 8081
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use existing if no unrelated flags are set, has positional, and socket is active", async () => {
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
|
|
|
|
|
|
|
args._.push("./file")
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
|
|
|
|
|
|
|
const socketPath = path.join(testDir, "socket")
|
|
|
|
await fs.writeFile(vscodeIpcPath, socketPath)
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
|
|
|
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
const server = net.createServer(() => {
|
|
|
|
// Close after getting the first connection.
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
server.once("listening", () => resolve(server))
|
|
|
|
server.listen(socketPath)
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), socketPath)
|
|
|
|
|
|
|
|
args.port = 8081
|
|
|
|
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
|
|
|
})
|
|
|
|
})
|