Skip to content

Commit e8063c7

Browse files
authored
Merge pull request #4211 from cdr/jsjoeio-add-moar-tests
feat: add tests for shouldRunVsCodeCli and bindAddrFromArgs
2 parents 1440b26 + f847575 commit e8063c7

File tree

2 files changed

+197
-3
lines changed

2 files changed

+197
-3
lines changed

src/node/cli.ts

+17-2
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,7 +630,18 @@ function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
626630
}
627631

628632
export const shouldRunVsCodeCli = (args: Args): boolean => {
629-
return !!args["list-extensions"] || !!args["install-extension"] || !!args["uninstall-extension"]
633+
// Create new interface with only Arg keys
634+
// keyof Args
635+
// Turn that into an array
636+
// Array<...>
637+
type ExtensionArgs = Array<keyof Args>
638+
const extensionRelatedArgs: ExtensionArgs = ["list-extensions", "install-extension", "uninstall-extension"]
639+
640+
const argKeys = Object.keys(args)
641+
642+
// If any of the extensionRelatedArgs are included in args
643+
// then we don't want to run the vscode cli
644+
return extensionRelatedArgs.some((arg) => argKeys.includes(arg))
630645
}
631646

632647
/**

test/unit/node/cli.test.ts

+180-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@ import { promises as fs } from "fs"
33
import * as net from "net"
44
import * as os from "os"
55
import * as path from "path"
6-
import { Args, parse, setDefaults, shouldOpenInExistingInstance, splitOnFirstEquals } from "../../../src/node/cli"
6+
import {
7+
Args,
8+
bindAddrFromArgs,
9+
parse,
10+
setDefaults,
11+
shouldOpenInExistingInstance,
12+
shouldRunVsCodeCli,
13+
splitOnFirstEquals,
14+
} from "../../../src/node/cli"
715
import { tmpdir } from "../../../src/node/constants"
816
import { paths } from "../../../src/node/util"
17+
import { useEnv } from "../../utils/helpers"
918

1019
type Mutable<T> = {
1120
-readonly [P in keyof T]: T[P]
@@ -463,3 +472,173 @@ describe("splitOnFirstEquals", () => {
463472
expect(actual).toEqual(expect.arrayContaining(expected))
464473
})
465474
})
475+
476+
describe("shouldRunVsCodeCli", () => {
477+
it("should return false if no 'extension' related args passed in", () => {
478+
const args = {
479+
_: [],
480+
}
481+
const actual = shouldRunVsCodeCli(args)
482+
const expected = false
483+
484+
expect(actual).toBe(expected)
485+
})
486+
487+
it("should return true if 'list-extensions' passed in", () => {
488+
const args = {
489+
_: [],
490+
["list-extensions"]: true,
491+
}
492+
const actual = shouldRunVsCodeCli(args)
493+
const expected = true
494+
495+
expect(actual).toBe(expected)
496+
})
497+
498+
it("should return true if 'install-extension' passed in", () => {
499+
const args = {
500+
_: [],
501+
["install-extension"]: ["hello.world"],
502+
}
503+
const actual = shouldRunVsCodeCli(args)
504+
const expected = true
505+
506+
expect(actual).toBe(expected)
507+
})
508+
509+
it("should return true if 'uninstall-extension' passed in", () => {
510+
const args = {
511+
_: [],
512+
["uninstall-extension"]: ["hello.world"],
513+
}
514+
const actual = shouldRunVsCodeCli(args)
515+
const expected = true
516+
517+
expect(actual).toBe(expected)
518+
})
519+
})
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)