2020-09-15 20:43:07 +02:00
|
|
|
import { Level, logger } from "@coder/logger"
|
2021-03-15 22:15:24 +01:00
|
|
|
import { promises as fs } from "fs"
|
2020-09-15 23:51:43 +02:00
|
|
|
import * as net from "net"
|
|
|
|
import * as os from "os"
|
2020-02-07 01:26:07 +01:00
|
|
|
import * as path from "path"
|
2021-09-20 23:53:09 +02:00
|
|
|
import {
|
|
|
|
Args,
|
2021-09-21 19:41:33 +02:00
|
|
|
bindAddrFromArgs,
|
2021-09-20 23:53:09 +02:00
|
|
|
parse,
|
|
|
|
setDefaults,
|
|
|
|
shouldOpenInExistingInstance,
|
|
|
|
shouldRunVsCodeCli,
|
|
|
|
splitOnFirstEquals,
|
|
|
|
} from "../../../src/node/cli"
|
2021-07-28 01:52:57 +02:00
|
|
|
import { tmpdir } from "../../../src/node/constants"
|
|
|
|
import { paths } from "../../../src/node/util"
|
2021-09-21 19:41:33 +02:00
|
|
|
import { useEnv } from "../../utils/helpers"
|
2020-02-07 01:26:07 +01:00
|
|
|
|
2020-09-15 23:51:43 +02:00
|
|
|
type Mutable<T> = {
|
|
|
|
-readonly [P in keyof T]: T[P]
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("parser", () => {
|
2020-02-18 23:51:55 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
delete process.env.LOG_LEVEL
|
2020-10-15 23:17:04 +02:00
|
|
|
delete process.env.PASSWORD
|
2021-01-26 00:34:43 +01:00
|
|
|
console.log = jest.fn()
|
2020-02-18 23:51:55 +01:00
|
|
|
})
|
|
|
|
|
2020-09-15 20:43:07 +02:00
|
|
|
// The parser should not set any defaults so the caller can determine what
|
2020-09-15 23:51:43 +02:00
|
|
|
// values the user actually set. These are only set after explicitly calling
|
|
|
|
// `setDefaults`.
|
2020-04-28 23:39:01 +02:00
|
|
|
const defaults = {
|
2020-10-15 23:17:04 +02:00
|
|
|
auth: "password",
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
"proxy-domain": [],
|
|
|
|
usingEnvPassword: false,
|
2020-12-08 21:54:17 +01:00
|
|
|
usingEnvHashedPassword: false,
|
2020-09-15 20:43:07 +02:00
|
|
|
"extensions-dir": path.join(paths.data, "extensions"),
|
|
|
|
"user-data-dir": paths.data,
|
2020-04-28 23:39:01 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 23:17:04 +02:00
|
|
|
it("should parse nothing", () => {
|
2021-01-12 19:53:12 +01:00
|
|
|
expect(parse([])).toStrictEqual({ _: [] })
|
|
|
|
})
|
2020-02-07 01:26:07 +01:00
|
|
|
|
2020-05-19 06:39:57 +02:00
|
|
|
it("should parse all available options", () => {
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(
|
2020-05-19 06:39:57 +02:00
|
|
|
parse([
|
2021-05-04 23:46:08 +02:00
|
|
|
"--enable",
|
|
|
|
"feature1",
|
|
|
|
"--enable",
|
|
|
|
"feature2",
|
2020-04-27 15:22:52 +02:00
|
|
|
"--bind-addr=192.169.0.1:8080",
|
2020-02-07 01:26:07 +01: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 17:54:23 +01:00
|
|
|
"--socket=mumble",
|
2020-02-07 01:26:07 +01:00
|
|
|
"3",
|
|
|
|
"--user-data-dir",
|
|
|
|
"bar",
|
2020-02-19 17:54:23 +01:00
|
|
|
"--cert=baz",
|
2020-02-07 01:26:07 +01:00
|
|
|
"--cert-key",
|
|
|
|
"qux",
|
|
|
|
"--version",
|
|
|
|
"--json",
|
2020-02-19 17:54:23 +01:00
|
|
|
"--port=8081",
|
2020-02-07 01:26:07 +01:00
|
|
|
"--host",
|
|
|
|
"0.0.0.0",
|
|
|
|
"4",
|
|
|
|
"--",
|
|
|
|
"-5",
|
|
|
|
"--6",
|
2021-01-12 19:53:12 +01:00
|
|
|
]),
|
|
|
|
).toEqual({
|
|
|
|
_: ["1", "2", "3", "4", "-5", "--6"],
|
|
|
|
auth: "none",
|
|
|
|
"builtin-extensions-dir": path.resolve("foobar"),
|
|
|
|
"cert-key": path.resolve("qux"),
|
|
|
|
cert: {
|
|
|
|
value: path.resolve("baz"),
|
2020-02-15 01:46:00 +01:00
|
|
|
},
|
2021-05-04 23:46:08 +02:00
|
|
|
enable: ["feature1", "feature2"],
|
2021-01-12 19:53:12 +01:00
|
|
|
"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: "error",
|
|
|
|
open: true,
|
|
|
|
port: 8081,
|
|
|
|
socket: path.resolve("mumble"),
|
|
|
|
"user-data-dir": path.resolve("bar"),
|
|
|
|
verbose: true,
|
|
|
|
version: true,
|
|
|
|
"bind-addr": "192.169.0.1:8080",
|
|
|
|
})
|
2020-02-07 01:26:07 +01:00
|
|
|
})
|
|
|
|
|
2020-05-19 06:39:57 +02:00
|
|
|
it("should work with short options", () => {
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(parse(["-vvv", "-v"])).toEqual({
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
2020-02-07 01:26:07 +01:00
|
|
|
verbose: true,
|
|
|
|
version: true,
|
|
|
|
})
|
2020-02-18 23:51:55 +01:00
|
|
|
})
|
|
|
|
|
2020-09-15 20:43:07 +02:00
|
|
|
it("should use log level env var", async () => {
|
|
|
|
const args = parse([])
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(args).toEqual({ _: [] })
|
2020-09-15 20:43:07 +02:00
|
|
|
|
2020-02-18 23:51:55 +01:00
|
|
|
process.env.LOG_LEVEL = "debug"
|
2021-01-08 21:55:47 +01:00
|
|
|
const defaults = await setDefaults(args)
|
2021-01-11 19:37:27 +01:00
|
|
|
expect(defaults).toStrictEqual({
|
2020-04-28 23:39:01 +02:00
|
|
|
...defaults,
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
2020-02-18 23:51:55 +01:00
|
|
|
log: "debug",
|
2020-05-19 06:39:57 +02:00
|
|
|
verbose: false,
|
2020-02-18 23:51:55 +01:00
|
|
|
})
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(process.env.LOG_LEVEL).toEqual("debug")
|
|
|
|
expect(logger.level).toEqual(Level.Debug)
|
2020-04-28 23:39:01 +02:00
|
|
|
|
|
|
|
process.env.LOG_LEVEL = "trace"
|
2021-01-08 21:55:47 +01:00
|
|
|
const updated = await setDefaults(args)
|
2021-01-12 19:53:12 +01:00
|
|
|
expect(updated).toStrictEqual({
|
2021-01-08 21:55:47 +01:00
|
|
|
...updated,
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
2020-04-28 23:39:01 +02:00
|
|
|
log: "trace",
|
|
|
|
verbose: true,
|
|
|
|
})
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(process.env.LOG_LEVEL).toEqual("trace")
|
|
|
|
expect(logger.level).toEqual(Level.Trace)
|
2020-02-18 23:51:55 +01:00
|
|
|
})
|
|
|
|
|
2020-05-14 12:08:37 +02:00
|
|
|
it("should prefer --log to env var and --verbose to --log", async () => {
|
2020-09-15 20:43:07 +02:00
|
|
|
let args = parse(["--log", "info"])
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(args).toEqual({
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
|
|
|
log: "info",
|
|
|
|
})
|
|
|
|
|
2020-02-18 23:51:55 +01:00
|
|
|
process.env.LOG_LEVEL = "debug"
|
2021-01-12 19:53:12 +01:00
|
|
|
const defaults = await setDefaults(args)
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(defaults).toEqual({
|
2020-04-28 23:39:01 +02:00
|
|
|
...defaults,
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
2020-02-18 23:51:55 +01:00
|
|
|
log: "info",
|
2020-05-19 06:39:57 +02:00
|
|
|
verbose: false,
|
2020-02-18 23:51:55 +01:00
|
|
|
})
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(process.env.LOG_LEVEL).toEqual("info")
|
|
|
|
expect(logger.level).toEqual(Level.Info)
|
2020-04-28 23:39:01 +02:00
|
|
|
|
|
|
|
process.env.LOG_LEVEL = "trace"
|
2021-01-08 21:55:47 +01:00
|
|
|
const updated = await setDefaults(args)
|
2021-01-12 19:53:12 +01:00
|
|
|
expect(updated).toEqual({
|
2020-04-28 23:39:01 +02:00
|
|
|
...defaults,
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
2020-04-28 23:39:01 +02:00
|
|
|
log: "info",
|
2020-05-19 06:39:57 +02:00
|
|
|
verbose: false,
|
2020-04-28 23:39:01 +02:00
|
|
|
})
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(process.env.LOG_LEVEL).toEqual("info")
|
|
|
|
expect(logger.level).toEqual(Level.Info)
|
2020-04-28 23:39:01 +02:00
|
|
|
|
2020-09-15 20:43:07 +02:00
|
|
|
args = parse(["--log", "info", "--verbose"])
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(args).toEqual({
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
|
|
|
log: "info",
|
|
|
|
verbose: true,
|
|
|
|
})
|
|
|
|
|
2020-04-28 23:39:01 +02:00
|
|
|
process.env.LOG_LEVEL = "warn"
|
2021-01-08 21:55:47 +01:00
|
|
|
const updatedAgain = await setDefaults(args)
|
2021-01-12 19:53:12 +01:00
|
|
|
expect(updatedAgain).toEqual({
|
2020-04-28 23:39:01 +02:00
|
|
|
...defaults,
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
2020-04-28 23:39:01 +02:00
|
|
|
log: "trace",
|
|
|
|
verbose: true,
|
|
|
|
})
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(process.env.LOG_LEVEL).toEqual("trace")
|
|
|
|
expect(logger.level).toEqual(Level.Trace)
|
2020-04-28 23:39:01 +02:00
|
|
|
})
|
|
|
|
|
2020-09-15 20:43:07 +02:00
|
|
|
it("should ignore invalid log level env var", async () => {
|
2020-04-28 23:39:01 +02:00
|
|
|
process.env.LOG_LEVEL = "bogus"
|
2021-01-08 21:55:47 +01:00
|
|
|
const defaults = await setDefaults(parse([]))
|
|
|
|
expect(defaults).toEqual({
|
2020-09-15 20:43:07 +02:00
|
|
|
...defaults,
|
2021-01-08 21:55:47 +01:00
|
|
|
_: [],
|
2020-09-15 20:43:07 +02:00
|
|
|
})
|
2020-02-07 01:26:07 +01:00
|
|
|
})
|
|
|
|
|
2020-05-19 06:39:57 +02:00
|
|
|
it("should error if value isn't provided", () => {
|
2021-01-08 21:55:47 +01: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 01:26:07 +01:00
|
|
|
})
|
|
|
|
|
2020-05-19 06:39:57 +02:00
|
|
|
it("should error if value is invalid", () => {
|
2021-01-08 21:55:47 +01: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 01:26:07 +01:00
|
|
|
})
|
|
|
|
|
2020-05-19 06:39:57 +02:00
|
|
|
it("should error if the option doesn't exist", () => {
|
2021-01-12 19:53:12 +01:00
|
|
|
expect(() => parse(["--foo"])).toThrowError(/Unknown option --foo/)
|
2020-02-07 01:26:07 +01:00
|
|
|
})
|
|
|
|
|
2020-05-19 06:39:57 +02:00
|
|
|
it("should not error if the value is optional", () => {
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(parse(["--cert"])).toEqual({
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
2020-02-07 01:26:07 +01:00
|
|
|
cert: {
|
|
|
|
value: undefined,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-19 06:39:57 +02:00
|
|
|
it("should not allow option-like values", () => {
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(() => parse(["--socket", "--socket-path-value"])).toThrowError(/--socket requires a value/)
|
2020-02-07 01:26:07 +01:00
|
|
|
// If you actually had a path like this you would do this instead:
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(parse(["--socket", "./--socket-path-value"])).toEqual({
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
2020-02-07 01:26:07 +01:00
|
|
|
socket: path.resolve("--socket-path-value"),
|
|
|
|
})
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(() => parse(["--cert", "--socket-path-value"])).toThrowError(/Unknown option --socket-path-value/)
|
2020-02-07 01:26:07 +01:00
|
|
|
})
|
2020-02-21 01:44:03 +01:00
|
|
|
|
2020-05-19 06:39:57 +02:00
|
|
|
it("should allow positional arguments before options", () => {
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(parse(["foo", "test", "--auth", "none"])).toEqual({
|
2020-02-21 01:44:03 +01:00
|
|
|
_: ["foo", "test"],
|
|
|
|
auth: "none",
|
|
|
|
})
|
|
|
|
})
|
2020-03-23 18:08:50 +01:00
|
|
|
|
2020-05-19 06:39:57 +02:00
|
|
|
it("should support repeatable flags", () => {
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(parse(["--proxy-domain", "*.coder.com"])).toEqual({
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
2020-03-23 18:08:50 +01:00
|
|
|
"proxy-domain": ["*.coder.com"],
|
|
|
|
})
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"])).toEqual({
|
2020-09-15 20:43:07 +02:00
|
|
|
_: [],
|
2020-03-23 18:08:50 +01:00
|
|
|
"proxy-domain": ["*.coder.com", "test.com"],
|
|
|
|
})
|
|
|
|
})
|
2020-10-15 23:17:04 +02:00
|
|
|
|
2021-01-12 19:53:12 +01: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-15 23:17:04 +02: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(" "))
|
2021-01-08 21:55:47 +01:00
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
2020-10-15 23:17:04 +02:00
|
|
|
_: [],
|
|
|
|
...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([])
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(args).toEqual({
|
2020-10-15 23:17:04 +02:00
|
|
|
_: [],
|
|
|
|
})
|
|
|
|
|
2021-01-08 21:55:47 +01:00
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
2020-10-15 23:17:04 +02:00
|
|
|
...defaults,
|
|
|
|
_: [],
|
|
|
|
password: "test",
|
|
|
|
usingEnvPassword: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-03 20:27:59 +02:00
|
|
|
it("should use env var hashed password", async () => {
|
2021-06-02 23:18:54 +02:00
|
|
|
process.env.HASHED_PASSWORD =
|
|
|
|
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY" // test
|
2020-12-08 21:54:17 +01:00
|
|
|
const args = parse([])
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(args).toEqual({
|
2020-12-08 21:54:17 +01:00
|
|
|
_: [],
|
|
|
|
})
|
|
|
|
|
2021-01-12 19:53:12 +01:00
|
|
|
const defaultArgs = await setDefaults(args)
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(defaultArgs).toEqual({
|
2020-12-08 21:54:17 +01:00
|
|
|
...defaults,
|
|
|
|
_: [],
|
2021-06-02 23:18:54 +02:00
|
|
|
"hashed-password":
|
|
|
|
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
|
2020-12-08 21:54:17 +01:00
|
|
|
usingEnvHashedPassword: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-10-15 23:17:04 +02:00
|
|
|
it("should filter proxy domains", async () => {
|
|
|
|
const args = parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "coder.com", "--proxy-domain", "coder.org"])
|
2021-01-08 21:55:47 +01:00
|
|
|
expect(args).toEqual({
|
2020-10-15 23:17:04 +02:00
|
|
|
_: [],
|
|
|
|
"proxy-domain": ["*.coder.com", "coder.com", "coder.org"],
|
|
|
|
})
|
|
|
|
|
2021-01-08 21:55:47 +01:00
|
|
|
const defaultArgs = await setDefaults(args)
|
|
|
|
expect(defaultArgs).toEqual({
|
2020-10-15 23:17:04 +02:00
|
|
|
...defaults,
|
|
|
|
_: [],
|
|
|
|
"proxy-domain": ["coder.com", "coder.org"],
|
|
|
|
})
|
|
|
|
})
|
2021-06-04 01:30:33 +02:00
|
|
|
it("should allow '=,$/' in strings", async () => {
|
|
|
|
const args = parse([
|
|
|
|
"--enable-proposed-api",
|
|
|
|
"$argon2i$v=19$m=4096,t=3,p=1$0qr/o+0t00hsbjfqcksfdq$ofcm4rl6o+b7oxpua4qlxubypbbpsf+8l531u7p9hyy",
|
|
|
|
])
|
|
|
|
expect(args).toEqual({
|
|
|
|
_: [],
|
|
|
|
"enable-proposed-api": [
|
|
|
|
"$argon2i$v=19$m=4096,t=3,p=1$0qr/o+0t00hsbjfqcksfdq$ofcm4rl6o+b7oxpua4qlxubypbbpsf+8l531u7p9hyy",
|
|
|
|
],
|
|
|
|
})
|
|
|
|
})
|
2021-06-04 01:37:46 +02: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",
|
|
|
|
})
|
|
|
|
})
|
2020-02-07 01:26:07 +01:00
|
|
|
})
|
2020-09-15 23:51:43 +02:00
|
|
|
|
|
|
|
describe("cli", () => {
|
|
|
|
let args: Mutable<Args> = { _: [] }
|
|
|
|
const testDir = path.join(tmpdir, "tests/cli")
|
|
|
|
const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc")
|
|
|
|
|
2021-01-08 21:55:47 +01:00
|
|
|
beforeAll(async () => {
|
2021-03-15 22:15:24 +01:00
|
|
|
await fs.rmdir(testDir, { recursive: true })
|
|
|
|
await fs.mkdir(testDir, { recursive: true })
|
2020-09-15 23:51:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
delete process.env.VSCODE_IPC_HOOK_CLI
|
|
|
|
args = { _: [] }
|
2021-03-15 22:15:24 +01:00
|
|
|
await fs.rmdir(vscodeIpcPath, { recursive: true })
|
2020-09-15 23:51:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should use existing if inside code-server", async () => {
|
2021-01-12 19:53:12 +01:00
|
|
|
process.env.VSCODE_IPC_HOOK_CLI = "test"
|
2021-01-11 19:37:27 +01:00
|
|
|
expect(await shouldOpenInExistingInstance(args)).toStrictEqual("test")
|
2020-09-15 23:51:43 +02:00
|
|
|
|
|
|
|
args.port = 8081
|
2021-01-12 19:53:12 +01:00
|
|
|
args._.push("./file")
|
2021-01-11 19:37:27 +01:00
|
|
|
expect(await shouldOpenInExistingInstance(args)).toStrictEqual("test")
|
2020-09-15 23:51:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should use existing if --reuse-window is set", async () => {
|
|
|
|
args["reuse-window"] = true
|
2021-03-15 21:31:19 +01:00
|
|
|
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual(undefined)
|
2020-09-15 23:51:43 +02:00
|
|
|
|
|
|
|
await fs.writeFile(vscodeIpcPath, "test")
|
2021-01-08 21:55:47 +01:00
|
|
|
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual("test")
|
2020-09-15 23:51:43 +02:00
|
|
|
|
|
|
|
args.port = 8081
|
2021-01-08 21:55:47 +01:00
|
|
|
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual("test")
|
2020-09-15 23:51:43 +02:00
|
|
|
})
|
2021-01-12 19:53:12 +01:00
|
|
|
|
2020-09-15 23:51:43 +02:00
|
|
|
it("should use existing if --new-window is set", async () => {
|
|
|
|
args["new-window"] = true
|
2021-01-11 19:37:27 +01:00
|
|
|
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(undefined)
|
2020-09-15 23:51:43 +02:00
|
|
|
|
|
|
|
await fs.writeFile(vscodeIpcPath, "test")
|
2021-01-11 19:37:27 +01:00
|
|
|
expect(await shouldOpenInExistingInstance(args)).toStrictEqual("test")
|
2020-09-15 23:51:43 +02:00
|
|
|
|
|
|
|
args.port = 8081
|
2021-01-11 19:37:27 +01:00
|
|
|
expect(await shouldOpenInExistingInstance(args)).toStrictEqual("test")
|
2020-09-15 23:51:43 +02:00
|
|
|
})
|
|
|
|
|
2021-01-12 19:53:12 +01:00
|
|
|
it("should use existing if no unrelated flags are set, has positional, and socket is active", async () => {
|
|
|
|
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(undefined)
|
|
|
|
|
|
|
|
args._.push("./file")
|
|
|
|
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(undefined)
|
|
|
|
|
|
|
|
const socketPath = path.join(testDir, "socket")
|
|
|
|
await fs.writeFile(vscodeIpcPath, socketPath)
|
|
|
|
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(undefined)
|
|
|
|
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
const server = net.createServer(() => {
|
|
|
|
// Close after getting the first connection.
|
|
|
|
server.close()
|
2020-09-15 23:51:43 +02:00
|
|
|
})
|
2021-01-12 19:53:12 +01:00
|
|
|
server.once("listening", () => resolve(server))
|
|
|
|
server.listen(socketPath)
|
|
|
|
})
|
2020-09-15 23:51:43 +02:00
|
|
|
|
2021-01-12 19:53:12 +01:00
|
|
|
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(socketPath)
|
2020-09-15 23:51:43 +02:00
|
|
|
|
2021-01-12 19:53:12 +01:00
|
|
|
args.port = 8081
|
|
|
|
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(undefined)
|
|
|
|
})
|
2020-09-15 23:51:43 +02:00
|
|
|
})
|
2021-06-04 01:30:33 +02:00
|
|
|
|
|
|
|
describe("splitOnFirstEquals", () => {
|
|
|
|
it("should split on the first equals", () => {
|
2021-06-04 01:37:46 +02:00
|
|
|
const testStr = "enabled-proposed-api=test=value"
|
2021-06-04 01:30:33 +02:00
|
|
|
const actual = splitOnFirstEquals(testStr)
|
2021-06-04 01:37:46 +02:00
|
|
|
const expected = ["enabled-proposed-api", "test=value"]
|
2021-06-04 01:30:33 +02:00
|
|
|
expect(actual).toEqual(expect.arrayContaining(expected))
|
|
|
|
})
|
|
|
|
it("should split on first equals regardless of multiple equals signs", () => {
|
|
|
|
const testStr =
|
2021-06-04 01:37:46 +02:00
|
|
|
"hashed-password=$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY"
|
2021-06-04 01:30:33 +02:00
|
|
|
const actual = splitOnFirstEquals(testStr)
|
|
|
|
const expected = [
|
2021-06-04 01:37:46 +02:00
|
|
|
"hashed-password",
|
2021-06-04 01:30:33 +02:00
|
|
|
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
|
|
|
|
]
|
|
|
|
expect(actual).toEqual(expect.arrayContaining(expected))
|
|
|
|
})
|
2021-06-04 01:37:46 +02:00
|
|
|
it("should always return the first element before an equals", () => {
|
|
|
|
const testStr = "auth="
|
2021-06-04 01:30:33 +02:00
|
|
|
const actual = splitOnFirstEquals(testStr)
|
2021-06-04 01:37:46 +02:00
|
|
|
const expected = ["auth"]
|
2021-06-04 01:30:33 +02:00
|
|
|
expect(actual).toEqual(expect.arrayContaining(expected))
|
|
|
|
})
|
|
|
|
})
|
2021-09-20 23:53:09 +02:00
|
|
|
|
|
|
|
describe("shouldRunVsCodeCli", () => {
|
|
|
|
it("should return false if no 'extension' related args passed in", () => {
|
|
|
|
const args = {
|
|
|
|
_: [],
|
|
|
|
}
|
|
|
|
const actual = shouldRunVsCodeCli(args)
|
|
|
|
const expected = false
|
|
|
|
|
|
|
|
expect(actual).toBe(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should return true if 'list-extensions' passed in", () => {
|
|
|
|
const args = {
|
|
|
|
_: [],
|
|
|
|
["list-extensions"]: true,
|
|
|
|
}
|
|
|
|
const actual = shouldRunVsCodeCli(args)
|
|
|
|
const expected = true
|
|
|
|
|
|
|
|
expect(actual).toBe(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should return true if 'install-extension' passed in", () => {
|
|
|
|
const args = {
|
|
|
|
_: [],
|
|
|
|
["install-extension"]: ["hello.world"],
|
|
|
|
}
|
|
|
|
const actual = shouldRunVsCodeCli(args)
|
|
|
|
const expected = true
|
|
|
|
|
|
|
|
expect(actual).toBe(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should return true if 'uninstall-extension' passed in", () => {
|
|
|
|
const args = {
|
|
|
|
_: [],
|
|
|
|
["uninstall-extension"]: ["hello.world"],
|
|
|
|
}
|
|
|
|
const actual = shouldRunVsCodeCli(args)
|
|
|
|
const expected = true
|
|
|
|
|
|
|
|
expect(actual).toBe(expected)
|
|
|
|
})
|
|
|
|
})
|
2021-09-21 19:41:33 +02:00
|
|
|
|
|
|
|
describe("bindAddrFromArgs", () => {
|
|
|
|
it("should return the bind address", () => {
|
|
|
|
const args = {
|
|
|
|
_: [],
|
|
|
|
}
|
|
|
|
|
|
|
|
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", () => {
|
|
|
|
const args = {
|
|
|
|
_: [],
|
|
|
|
["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", () => {
|
|
|
|
const args = {
|
|
|
|
_: [],
|
|
|
|
["host"]: "coder",
|
|
|
|
}
|
|
|
|
|
|
|
|
const addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
const actual = bindAddrFromArgs(addr, args)
|
|
|
|
const expected = {
|
|
|
|
host: "coder",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(actual).toStrictEqual(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should use process.env.PORT if set", () => {
|
|
|
|
const [setValue, resetValue] = useEnv("PORT")
|
|
|
|
setValue("8000")
|
|
|
|
|
|
|
|
const args = {
|
|
|
|
_: [],
|
|
|
|
}
|
|
|
|
|
|
|
|
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", () => {
|
|
|
|
const args = {
|
|
|
|
_: [],
|
|
|
|
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")
|
|
|
|
|
|
|
|
const args = {
|
|
|
|
_: [],
|
|
|
|
port: 3000,
|
|
|
|
}
|
|
|
|
|
|
|
|
const addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
const actual = bindAddrFromArgs(addr, args)
|
|
|
|
const expected = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 3000,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(actual).toStrictEqual(expected)
|
|
|
|
resetValue()
|
|
|
|
})
|
|
|
|
})
|