2020-04-29 04:39:01 +07:00
|
|
|
import { logger, Level } from "@coder/logger"
|
2020-02-07 07:26:07 +07:00
|
|
|
import * as assert from "assert"
|
|
|
|
import * as path from "path"
|
|
|
|
import { parse } from "../src/node/cli"
|
|
|
|
import { xdgLocalDir } from "../src/node/util"
|
|
|
|
|
|
|
|
describe("cli", () => {
|
2020-02-19 05:51:55 +07:00
|
|
|
beforeEach(() => {
|
|
|
|
delete process.env.LOG_LEVEL
|
|
|
|
})
|
|
|
|
|
2020-04-29 04:39:01 +07:00
|
|
|
// The parser will always fill these out.
|
|
|
|
const defaults = {
|
|
|
|
_: [],
|
|
|
|
"extensions-dir": path.join(xdgLocalDir, "extensions"),
|
|
|
|
"user-data-dir": xdgLocalDir,
|
|
|
|
}
|
|
|
|
|
2020-02-07 07:26:07 +07:00
|
|
|
it("should set defaults", () => {
|
2020-04-29 04:39:01 +07:00
|
|
|
assert.deepEqual(parse([]), defaults)
|
2020-02-07 07:26:07 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should parse all available options", () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
parse([
|
2020-04-27 20:22:52 +07:00
|
|
|
"--bind-addr=192.169.0.1:8080",
|
2020-02-07 07:26:07 +07: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",
|
|
|
|
"--open",
|
2020-02-19 23:54:23 +07:00
|
|
|
"--socket=mumble",
|
2020-02-07 07:26:07 +07:00
|
|
|
"3",
|
|
|
|
"--user-data-dir",
|
|
|
|
"bar",
|
2020-02-19 23:54:23 +07:00
|
|
|
"--cert=baz",
|
2020-02-07 07:26:07 +07:00
|
|
|
"--cert-key",
|
|
|
|
"qux",
|
|
|
|
"--version",
|
|
|
|
"--json",
|
2020-02-19 23:54:23 +07:00
|
|
|
"--port=8081",
|
2020-02-07 07:26:07 +07: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,
|
|
|
|
host: "0.0.0.0",
|
|
|
|
json: true,
|
|
|
|
log: "trace",
|
|
|
|
open: true,
|
|
|
|
port: 8081,
|
|
|
|
socket: path.resolve("mumble"),
|
|
|
|
"user-data-dir": path.resolve("bar"),
|
|
|
|
verbose: true,
|
|
|
|
version: true,
|
2020-04-27 20:22:52 +07:00
|
|
|
"bind-addr": "192.169.0.1:8080",
|
2020-02-15 07:46:00 +07:00
|
|
|
},
|
2020-02-07 07:26:07 +07:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should work with short options", () => {
|
|
|
|
assert.deepEqual(parse(["-vvv", "-v"]), {
|
2020-04-29 04:39:01 +07:00
|
|
|
...defaults,
|
2020-02-07 07:26:07 +07:00
|
|
|
log: "trace",
|
|
|
|
verbose: true,
|
|
|
|
version: true,
|
|
|
|
})
|
2020-02-19 05:51:55 +07:00
|
|
|
assert.equal(process.env.LOG_LEVEL, "trace")
|
2020-04-29 04:39:01 +07:00
|
|
|
assert.equal(logger.level, Level.Trace)
|
2020-02-19 05:51:55 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should use log level env var", () => {
|
|
|
|
process.env.LOG_LEVEL = "debug"
|
|
|
|
assert.deepEqual(parse([]), {
|
2020-04-29 04:39:01 +07:00
|
|
|
...defaults,
|
2020-02-19 05:51:55 +07:00
|
|
|
log: "debug",
|
|
|
|
})
|
|
|
|
assert.equal(process.env.LOG_LEVEL, "debug")
|
2020-04-29 04:39:01 +07:00
|
|
|
assert.equal(logger.level, Level.Debug)
|
|
|
|
|
|
|
|
process.env.LOG_LEVEL = "trace"
|
|
|
|
assert.deepEqual(parse([]), {
|
|
|
|
...defaults,
|
|
|
|
log: "trace",
|
|
|
|
verbose: true,
|
|
|
|
})
|
|
|
|
assert.equal(process.env.LOG_LEVEL, "trace")
|
|
|
|
assert.equal(logger.level, Level.Trace)
|
2020-02-19 05:51:55 +07:00
|
|
|
})
|
|
|
|
|
2020-04-29 04:39:01 +07:00
|
|
|
it("should prefer --log to env var and --verbose to --log", () => {
|
2020-02-19 05:51:55 +07:00
|
|
|
process.env.LOG_LEVEL = "debug"
|
|
|
|
assert.deepEqual(parse(["--log", "info"]), {
|
2020-04-29 04:39:01 +07:00
|
|
|
...defaults,
|
2020-02-19 05:51:55 +07:00
|
|
|
log: "info",
|
|
|
|
})
|
|
|
|
assert.equal(process.env.LOG_LEVEL, "info")
|
2020-04-29 04:39:01 +07:00
|
|
|
assert.equal(logger.level, Level.Info)
|
|
|
|
|
|
|
|
process.env.LOG_LEVEL = "trace"
|
|
|
|
assert.deepEqual(parse(["--log", "info"]), {
|
|
|
|
...defaults,
|
|
|
|
log: "info",
|
|
|
|
})
|
|
|
|
assert.equal(process.env.LOG_LEVEL, "info")
|
|
|
|
assert.equal(logger.level, Level.Info)
|
|
|
|
|
|
|
|
process.env.LOG_LEVEL = "warn"
|
|
|
|
assert.deepEqual(parse(["--log", "info", "--verbose"]), {
|
|
|
|
...defaults,
|
|
|
|
log: "trace",
|
|
|
|
verbose: true,
|
|
|
|
})
|
|
|
|
assert.equal(process.env.LOG_LEVEL, "trace")
|
|
|
|
assert.equal(logger.level, Level.Trace)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should ignore invalid log level env var", () => {
|
|
|
|
process.env.LOG_LEVEL = "bogus"
|
|
|
|
assert.deepEqual(parse([]), defaults)
|
2020-02-07 07:26:07 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should error if value isn't provided", () => {
|
|
|
|
assert.throws(() => parse(["--auth"]), /--auth requires a value/)
|
2020-02-19 23:54:23 +07:00
|
|
|
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/)
|
2020-04-27 20:22:52 +07:00
|
|
|
assert.throws(() => parse(["--bind-addr"]), /--bind-addr requires a value/)
|
2020-02-07 07:26:07 +07:00
|
|
|
})
|
|
|
|
|
2020-02-20 00:06:32 +07:00
|
|
|
it("should error if value is invalid", () => {
|
2020-02-07 07:26:07 +07:00
|
|
|
assert.throws(() => parse(["--port", "foo"]), /--port must be a number/)
|
|
|
|
assert.throws(() => parse(["--auth", "invalid"]), /--auth valid values: \[password, none\]/)
|
2020-02-20 00:06:32 +07:00
|
|
|
assert.throws(() => parse(["--log", "invalid"]), /--log valid values: \[trace, debug, info, warn, error\]/)
|
2020-02-07 07:26:07 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should error if the option doesn't exist", () => {
|
|
|
|
assert.throws(() => parse(["--foo"]), /Unknown option --foo/)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not error if the value is optional", () => {
|
|
|
|
assert.deepEqual(parse(["--cert"]), {
|
2020-04-29 04:39:01 +07:00
|
|
|
...defaults,
|
2020-02-07 07:26:07 +07:00
|
|
|
cert: {
|
|
|
|
value: undefined,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not allow option-like values", () => {
|
|
|
|
assert.throws(() => parse(["--socket", "--socket-path-value"]), /--socket requires a value/)
|
|
|
|
// If you actually had a path like this you would do this instead:
|
|
|
|
assert.deepEqual(parse(["--socket", "./--socket-path-value"]), {
|
2020-04-29 04:39:01 +07:00
|
|
|
...defaults,
|
2020-02-07 07:26:07 +07:00
|
|
|
socket: path.resolve("--socket-path-value"),
|
|
|
|
})
|
|
|
|
assert.throws(() => parse(["--cert", "--socket-path-value"]), /Unknown option --socket-path-value/)
|
|
|
|
})
|
2020-02-21 07:44:03 +07:00
|
|
|
|
|
|
|
it("should allow positional arguments before options", () => {
|
|
|
|
assert.deepEqual(parse(["foo", "test", "--auth", "none"]), {
|
2020-04-29 04:39:01 +07:00
|
|
|
...defaults,
|
2020-02-21 07:44:03 +07:00
|
|
|
_: ["foo", "test"],
|
|
|
|
auth: "none",
|
|
|
|
})
|
|
|
|
})
|
2020-03-24 00:08:50 +07:00
|
|
|
|
|
|
|
it("should support repeatable flags", () => {
|
|
|
|
assert.deepEqual(parse(["--proxy-domain", "*.coder.com"]), {
|
2020-04-29 04:39:01 +07:00
|
|
|
...defaults,
|
2020-03-24 00:08:50 +07:00
|
|
|
"proxy-domain": ["*.coder.com"],
|
|
|
|
})
|
|
|
|
assert.deepEqual(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), {
|
2020-04-29 04:39:01 +07:00
|
|
|
...defaults,
|
2020-03-24 00:08:50 +07:00
|
|
|
"proxy-domain": ["*.coder.com", "test.com"],
|
|
|
|
})
|
|
|
|
})
|
2020-02-07 07:26:07 +07:00
|
|
|
})
|