Skip to content

Commit 1b80244

Browse files
authored
Merge pull request #4265 from cdr/jsjoeio-add-tests
feat(cli): add test for defaultConfigFile
2 parents 999960e + 6c95f72 commit 1b80244

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/node/cli.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,21 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi
502502
} as DefaultedArgs // TODO: Technically no guarantee this is fulfilled.
503503
}
504504

505-
async function defaultConfigFile(): Promise<string> {
505+
/**
506+
* Helper function to return the default config file.
507+
*
508+
* @param {string} password - Password passed in (usually from generatePassword())
509+
* @returns The default config file:
510+
*
511+
* - bind-addr: 127.0.0.1:8080
512+
* - auth: password
513+
* - password: <password>
514+
* - cert: false
515+
*/
516+
export function defaultConfigFile(password: string): string {
506517
return `bind-addr: 127.0.0.1:8080
507518
auth: password
508-
password: ${await generatePassword()}
519+
password: ${password}
509520
cert: false
510521
`
511522
}
@@ -530,7 +541,8 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
530541
await fs.mkdir(path.dirname(configPath), { recursive: true })
531542

532543
try {
533-
await fs.writeFile(configPath, await defaultConfigFile(), {
544+
const generatedPassword = await generatePassword()
545+
await fs.writeFile(configPath, defaultConfigFile(generatedPassword), {
534546
flag: "wx", // wx means to fail if the path exists.
535547
})
536548
logger.info(`Wrote default config file to ${humanPath(configPath)}`)

test/unit/node/cli.test.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import * as path from "path"
66
import {
77
Args,
88
bindAddrFromArgs,
9+
defaultConfigFile,
910
parse,
1011
setDefaults,
1112
shouldOpenInExistingInstance,
1213
shouldRunVsCodeCli,
1314
splitOnFirstEquals,
1415
} from "../../../src/node/cli"
1516
import { tmpdir } from "../../../src/node/constants"
16-
import { paths } from "../../../src/node/util"
17+
import { generatePassword, paths } from "../../../src/node/util"
1718
import { useEnv } from "../../utils/helpers"
1819

1920
type Mutable<T> = {
@@ -642,3 +643,15 @@ describe("bindAddrFromArgs", () => {
642643
resetValue()
643644
})
644645
})
646+
647+
describe("defaultConfigFile", () => {
648+
it("should return the default config file as a string", async () => {
649+
const password = await generatePassword()
650+
const actual = defaultConfigFile(password)
651+
652+
expect(actual).toMatch(`bind-addr: 127.0.0.1:8080
653+
auth: password
654+
password: ${password}
655+
cert: false`)
656+
})
657+
})

0 commit comments

Comments
 (0)