Skip to content

Commit 91cabbc

Browse files
authored
feat(testing): add test for optionDescriptions (#4970)
* feat(testing): add test for optionDescriptions * refactor(cli): optional arg in optionDescriptions * feat: add more tests for optionDescriptions
1 parent 77296c7 commit 91cabbc

File tree

2 files changed

+103
-4
lines changed

2 files changed

+103
-4
lines changed

src/node/cli.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ type OptionType<T> = T extends boolean
120120
? "string[]"
121121
: "unknown"
122122

123-
type Options<T> = {
123+
export type Options<T> = {
124124
[P in keyof T]: Option<OptionType<T[P]>>
125125
}
126126

127-
const options: Options<Required<UserProvidedArgs>> = {
127+
export const options: Options<Required<UserProvidedArgs>> = {
128128
auth: { type: AuthType, description: "The type of authentication to use." },
129129
password: {
130130
type: "string",
@@ -235,8 +235,8 @@ const options: Options<Required<UserProvidedArgs>> = {
235235
},
236236
}
237237

238-
export const optionDescriptions = (): string[] => {
239-
const entries = Object.entries(options).filter(([, v]) => !!v.description)
238+
export const optionDescriptions = (opts: Partial<Options<Required<UserProvidedArgs>>> = options): string[] => {
239+
const entries = Object.entries(opts).filter(([, v]) => !!v.description)
240240
const widths = entries.reduce(
241241
(prev, [k, v]) => ({
242242
long: k.length > prev.long ? k.length : prev.long,

test/unit/node/cli.test.ts

+99
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import {
1313
shouldOpenInExistingInstance,
1414
splitOnFirstEquals,
1515
toVsCodeArgs,
16+
optionDescriptions,
17+
options,
18+
Options,
19+
AuthType,
20+
OptionalString,
1621
} from "../../../src/node/cli"
1722
import { shouldSpawnCliProcess } from "../../../src/node/main"
1823
import { generatePassword, paths } from "../../../src/node/util"
@@ -753,3 +758,97 @@ describe("toVsCodeArgs", () => {
753758
})
754759
})
755760
})
761+
762+
describe("optionDescriptions", () => {
763+
it("should return the descriptions of all the available options", () => {
764+
const expectedOptionDescriptions = Object.entries(options)
765+
.flat()
766+
.filter((item: any) => {
767+
if (item.description) {
768+
return item.description
769+
}
770+
})
771+
.map((item: any) => item.description)
772+
const actualOptionDescriptions = optionDescriptions()
773+
// We need both the expected and the actual
774+
// Both of these are string[]
775+
// We then loop through the expectedOptionDescriptions
776+
// and check that this expectedDescription exists in the
777+
// actualOptionDescriptions
778+
779+
// To do that we need to loop through actualOptionDescriptions
780+
// and make sure we have a substring match
781+
expectedOptionDescriptions.forEach((expectedDescription) => {
782+
const exists = actualOptionDescriptions.find((desc) => {
783+
if (
784+
desc.replace(/\n/g, " ").replace(/ /g, "").includes(expectedDescription.replace(/\n/g, " ").replace(/ /g, ""))
785+
) {
786+
return true
787+
}
788+
return false
789+
})
790+
expect(exists).toBeTruthy()
791+
})
792+
})
793+
it("should visually align multiple options", () => {
794+
const opts: Partial<Options<Required<UserProvidedArgs>>> = {
795+
"cert-key": { type: "string", path: true, description: "Path to certificate key when using non-generated cert." },
796+
"cert-host": {
797+
type: "string",
798+
description: "Hostname to use when generating a self signed certificate.",
799+
},
800+
"disable-update-check": {
801+
type: "boolean",
802+
description:
803+
"Disable update check. Without this flag, code-server checks every 6 hours against the latest github release and \n" +
804+
"then notifies you once every week that a new release is available.",
805+
},
806+
}
807+
expect(optionDescriptions(opts)).toStrictEqual([
808+
" --cert-key Path to certificate key when using non-generated cert.",
809+
" --cert-host Hostname to use when generating a self signed certificate.",
810+
` --disable-update-check Disable update check. Without this flag, code-server checks every 6 hours against the latest github release and
811+
then notifies you once every week that a new release is available.`,
812+
])
813+
})
814+
it("should add all valid options for enumerated types", () => {
815+
const opts: Partial<Options<Required<UserProvidedArgs>>> = {
816+
auth: { type: AuthType, description: "The type of authentication to use." },
817+
}
818+
expect(optionDescriptions(opts)).toStrictEqual([" --auth The type of authentication to use. [password, none]"])
819+
})
820+
821+
it("should show if an option is deprecated", () => {
822+
const opts: Partial<Options<Required<UserProvidedArgs>>> = {
823+
link: {
824+
type: OptionalString,
825+
description: `
826+
Securely bind code-server via our cloud service with the passed name. You'll get a URL like
827+
https://hostname-username.coder.co at which you can easily access your code-server instance.
828+
Authorization is done via GitHub.
829+
`,
830+
deprecated: true,
831+
},
832+
}
833+
expect(optionDescriptions(opts)).toStrictEqual([
834+
` --link (deprecated) Securely bind code-server via our cloud service with the passed name. You'll get a URL like
835+
https://hostname-username.coder.co at which you can easily access your code-server instance.
836+
Authorization is done via GitHub.`,
837+
])
838+
})
839+
840+
it("should show newlines in description", () => {
841+
const opts: Partial<Options<Required<UserProvidedArgs>>> = {
842+
"install-extension": {
843+
type: "string[]",
844+
description:
845+
"Install or update a VS Code extension by id or vsix. The identifier of an extension is `${publisher}.${name}`.\n" +
846+
"To install a specific version provide `@${version}`. For example: 'vscode.csharp@1.2.3'.",
847+
},
848+
}
849+
expect(optionDescriptions(opts)).toStrictEqual([
850+
` --install-extension Install or update a VS Code extension by id or vsix. The identifier of an extension is \`\${publisher}.\${name}\`.
851+
To install a specific version provide \`@\${version}\`. For example: 'vscode.csharp@1.2.3'.`,
852+
])
853+
})
854+
})

0 commit comments

Comments
 (0)