From 30ade712bf20924644ece85bbc4db3a22fa6a168 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Mon, 20 Sep 2021 14:53:09 -0700 Subject: [PATCH 1/3] feat: add tests for shouldRunVsCodeCli --- test/unit/node/cli.test.ts | 54 +++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/test/unit/node/cli.test.ts b/test/unit/node/cli.test.ts index 93a86776..ff713456 100644 --- a/test/unit/node/cli.test.ts +++ b/test/unit/node/cli.test.ts @@ -3,7 +3,14 @@ import { promises as fs } from "fs" import * as net from "net" import * as os from "os" import * as path from "path" -import { Args, parse, setDefaults, shouldOpenInExistingInstance, splitOnFirstEquals } from "../../../src/node/cli" +import { + Args, + parse, + setDefaults, + shouldOpenInExistingInstance, + shouldRunVsCodeCli, + splitOnFirstEquals, +} from "../../../src/node/cli" import { tmpdir } from "../../../src/node/constants" import { paths } from "../../../src/node/util" @@ -463,3 +470,48 @@ describe("splitOnFirstEquals", () => { expect(actual).toEqual(expect.arrayContaining(expected)) }) }) + +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) + }) +}) From a673cf283340c4dcde2e6177d22a68a04d396d6a Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Mon, 20 Sep 2021 15:14:46 -0700 Subject: [PATCH 2/3] refactor: shouldRunVsCodeCli --- src/node/cli.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/node/cli.ts b/src/node/cli.ts index a2fac418..aea8e3d9 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -626,7 +626,20 @@ function bindAddrFromAllSources(...argsConfig: Args[]): Addr { } export const shouldRunVsCodeCli = (args: Args): boolean => { - return !!args["list-extensions"] || !!args["install-extension"] || !!args["uninstall-extension"] + // Create new interface with only these keys + // Pick + // Get the keys of new interface + // keyof ... + // Turn that into an array + // Array<...> + type ExtensionArgs = Array> + const extensionRelatedArgs: ExtensionArgs = ["list-extensions", "install-extension", "uninstall-extension"] + + const argKeys = Object.keys(args) + + // If any of the extensionRelatedArgs are included in args + // then we don't want to run the vscode cli + return extensionRelatedArgs.some((arg) => argKeys.includes(arg)) } /** From f84757507bb2a417c5e1e1efac09ec28e22c106c Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Tue, 21 Sep 2021 10:41:33 -0700 Subject: [PATCH 3/3] feat: add tests for bindAddrFromArgs --- src/node/cli.ts | 14 ++-- test/unit/node/cli.test.ts | 127 +++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 6 deletions(-) diff --git a/src/node/cli.ts b/src/node/cli.ts index aea8e3d9..2433ddf2 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -594,7 +594,11 @@ interface Addr { port: number } -function bindAddrFromArgs(addr: Addr, args: Args): Addr { +/** + * This function creates the bind address + * using the CLI args. + */ +export function bindAddrFromArgs(addr: Addr, args: Args): Addr { addr = { ...addr } if (args["bind-addr"]) { addr = parseBindAddr(args["bind-addr"]) @@ -626,13 +630,11 @@ function bindAddrFromAllSources(...argsConfig: Args[]): Addr { } export const shouldRunVsCodeCli = (args: Args): boolean => { - // Create new interface with only these keys - // Pick - // Get the keys of new interface - // keyof ... + // Create new interface with only Arg keys + // keyof Args // Turn that into an array // Array<...> - type ExtensionArgs = Array> + type ExtensionArgs = Array const extensionRelatedArgs: ExtensionArgs = ["list-extensions", "install-extension", "uninstall-extension"] const argKeys = Object.keys(args) diff --git a/test/unit/node/cli.test.ts b/test/unit/node/cli.test.ts index ff713456..e92cb975 100644 --- a/test/unit/node/cli.test.ts +++ b/test/unit/node/cli.test.ts @@ -5,6 +5,7 @@ import * as os from "os" import * as path from "path" import { Args, + bindAddrFromArgs, parse, setDefaults, shouldOpenInExistingInstance, @@ -13,6 +14,7 @@ import { } from "../../../src/node/cli" import { tmpdir } from "../../../src/node/constants" import { paths } from "../../../src/node/util" +import { useEnv } from "../../utils/helpers" type Mutable = { -readonly [P in keyof T]: T[P] @@ -515,3 +517,128 @@ describe("shouldRunVsCodeCli", () => { expect(actual).toBe(expected) }) }) + +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() + }) +})