Merge pull request #4265 from cdr/jsjoeio-add-tests

feat(cli): add test for defaultConfigFile
This commit is contained in:
Joe Previte 2021-09-28 16:19:32 -07:00 committed by GitHub
commit 1b80244af7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View File

@ -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
`
}
@ -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)}`)

View File

@ -6,6 +6,7 @@ import * as path from "path"
import {
Args,
bindAddrFromArgs,
defaultConfigFile,
parse,
setDefaults,
shouldOpenInExistingInstance,
@ -13,7 +14,7 @@ import {
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> = {
@ -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`)
})
})