Skip to content

feat(cli): add test for defaultConfigFile #4265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,21 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi
} as DefaultedArgs // TODO: Technically no guarantee this is fulfilled.
}

async function defaultConfigFile(): Promise<string> {
/**
* Helper function to return the default config file.
*
* @param {string} password - Password passed in (usually from generatePassword())
* @returns The default config file:
*
* - bind-addr: 127.0.0.1:8080
* - auth: password
* - password: <password>
* - cert: false
*/
export function defaultConfigFile(password: string): string {
return `bind-addr: 127.0.0.1:8080
auth: password
password: ${await generatePassword()}
password: ${password}
cert: false
`
}
Expand All @@ -530,7 +541,8 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
await fs.mkdir(path.dirname(configPath), { recursive: true })

try {
await fs.writeFile(configPath, await defaultConfigFile(), {
const generatedPassword = await generatePassword()
await fs.writeFile(configPath, defaultConfigFile(generatedPassword), {
flag: "wx", // wx means to fail if the path exists.
})
logger.info(`Wrote default config file to ${humanPath(configPath)}`)
Expand Down
15 changes: 14 additions & 1 deletion test/unit/node/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import * as path from "path"
import {
Args,
bindAddrFromArgs,
defaultConfigFile,
parse,
setDefaults,
shouldOpenInExistingInstance,
shouldRunVsCodeCli,
splitOnFirstEquals,
} from "../../../src/node/cli"
import { tmpdir } from "../../../src/node/constants"
import { paths } from "../../../src/node/util"
import { generatePassword, paths } from "../../../src/node/util"
import { useEnv } from "../../utils/helpers"

type Mutable<T> = {
Expand Down Expand Up @@ -642,3 +643,15 @@ describe("bindAddrFromArgs", () => {
resetValue()
})
})

describe("defaultConfigFile", () => {
it("should return the default config file as a string", async () => {
const password = await generatePassword()
const actual = defaultConfigFile(password)

expect(actual).toMatch(`bind-addr: 127.0.0.1:8080
auth: password
password: ${password}
cert: false`)
})
})