Skip to content

Commit f847575

Browse files
committed
feat: add tests for bindAddrFromArgs
1 parent a673cf2 commit f847575

File tree

2 files changed

+135
-6
lines changed

2 files changed

+135
-6
lines changed

src/node/cli.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,11 @@ interface Addr {
594594
port: number
595595
}
596596

597-
function bindAddrFromArgs(addr: Addr, args: Args): Addr {
597+
/**
598+
* This function creates the bind address
599+
* using the CLI args.
600+
*/
601+
export function bindAddrFromArgs(addr: Addr, args: Args): Addr {
598602
addr = { ...addr }
599603
if (args["bind-addr"]) {
600604
addr = parseBindAddr(args["bind-addr"])
@@ -626,13 +630,11 @@ function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
626630
}
627631

628632
export const shouldRunVsCodeCli = (args: Args): boolean => {
629-
// Create new interface with only these keys
630-
// Pick<Args, "list-extensions" | "install-extension" | "uninstall-extension">
631-
// Get the keys of new interface
632-
// keyof ...
633+
// Create new interface with only Arg keys
634+
// keyof Args
633635
// Turn that into an array
634636
// Array<...>
635-
type ExtensionArgs = Array<keyof Pick<Args, "list-extensions" | "install-extension" | "uninstall-extension">>
637+
type ExtensionArgs = Array<keyof Args>
636638
const extensionRelatedArgs: ExtensionArgs = ["list-extensions", "install-extension", "uninstall-extension"]
637639

638640
const argKeys = Object.keys(args)

test/unit/node/cli.test.ts

+127
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as os from "os"
55
import * as path from "path"
66
import {
77
Args,
8+
bindAddrFromArgs,
89
parse,
910
setDefaults,
1011
shouldOpenInExistingInstance,
@@ -13,6 +14,7 @@ import {
1314
} from "../../../src/node/cli"
1415
import { tmpdir } from "../../../src/node/constants"
1516
import { paths } from "../../../src/node/util"
17+
import { useEnv } from "../../utils/helpers"
1618

1719
type Mutable<T> = {
1820
-readonly [P in keyof T]: T[P]
@@ -515,3 +517,128 @@ describe("shouldRunVsCodeCli", () => {
515517
expect(actual).toBe(expected)
516518
})
517519
})
520+
521+
describe("bindAddrFromArgs", () => {
522+
it("should return the bind address", () => {
523+
const args = {
524+
_: [],
525+
}
526+
527+
const addr = {
528+
host: "localhost",
529+
port: 8080,
530+
}
531+
532+
const actual = bindAddrFromArgs(addr, args)
533+
const expected = addr
534+
535+
expect(actual).toStrictEqual(expected)
536+
})
537+
538+
it("should use the bind-address if set in args", () => {
539+
const args = {
540+
_: [],
541+
["bind-addr"]: "localhost:3000",
542+
}
543+
544+
const addr = {
545+
host: "localhost",
546+
port: 8080,
547+
}
548+
549+
const actual = bindAddrFromArgs(addr, args)
550+
const expected = {
551+
host: "localhost",
552+
port: 3000,
553+
}
554+
555+
expect(actual).toStrictEqual(expected)
556+
})
557+
558+
it("should use the host if set in args", () => {
559+
const args = {
560+
_: [],
561+
["host"]: "coder",
562+
}
563+
564+
const addr = {
565+
host: "localhost",
566+
port: 8080,
567+
}
568+
569+
const actual = bindAddrFromArgs(addr, args)
570+
const expected = {
571+
host: "coder",
572+
port: 8080,
573+
}
574+
575+
expect(actual).toStrictEqual(expected)
576+
})
577+
578+
it("should use process.env.PORT if set", () => {
579+
const [setValue, resetValue] = useEnv("PORT")
580+
setValue("8000")
581+
582+
const args = {
583+
_: [],
584+
}
585+
586+
const addr = {
587+
host: "localhost",
588+
port: 8080,
589+
}
590+
591+
const actual = bindAddrFromArgs(addr, args)
592+
const expected = {
593+
host: "localhost",
594+
port: 8000,
595+
}
596+
597+
expect(actual).toStrictEqual(expected)
598+
resetValue()
599+
})
600+
601+
it("should set port if in args", () => {
602+
const args = {
603+
_: [],
604+
port: 3000,
605+
}
606+
607+
const addr = {
608+
host: "localhost",
609+
port: 8080,
610+
}
611+
612+
const actual = bindAddrFromArgs(addr, args)
613+
const expected = {
614+
host: "localhost",
615+
port: 3000,
616+
}
617+
618+
expect(actual).toStrictEqual(expected)
619+
})
620+
621+
it("should use the args.port over process.env.PORT if both set", () => {
622+
const [setValue, resetValue] = useEnv("PORT")
623+
setValue("8000")
624+
625+
const args = {
626+
_: [],
627+
port: 3000,
628+
}
629+
630+
const addr = {
631+
host: "localhost",
632+
port: 8080,
633+
}
634+
635+
const actual = bindAddrFromArgs(addr, args)
636+
const expected = {
637+
host: "localhost",
638+
port: 3000,
639+
}
640+
641+
expect(actual).toStrictEqual(expected)
642+
resetValue()
643+
})
644+
})

0 commit comments

Comments
 (0)