From fb2625dbe85b8da930dbb4872d8d87192128d99e Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Tue, 28 Sep 2021 15:38:59 -0700 Subject: [PATCH 2/4] chore(cli): export defaultConfigFile + add JSDoc --- src/node/cli.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/node/cli.ts b/src/node/cli.ts index 2433ddf2e..91260604d 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -502,7 +502,17 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi } as DefaultedArgs // TODO: Technically no guarantee this is fulfilled. } -async function defaultConfigFile(): Promise { +/** + * Helper function to return the default config file. + * + * @returns The default config file: + * + * - bind-addr: 127.0.0.1:8080 + * - auth: password + * - password: + * - cert: false + */ +export async function defaultConfigFile(): Promise { return `bind-addr: 127.0.0.1:8080 auth: password password: ${await generatePassword()} From 77c1150b8dbd2c71300c84fda827240aa494fba7 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Tue, 28 Sep 2021 15:45:44 -0700 Subject: [PATCH 3/4] feat(cli): add test for defaultConfigFile --- test/unit/node/cli.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/unit/node/cli.test.ts b/test/unit/node/cli.test.ts index e92cb9758..9bf9fbe67 100644 --- a/test/unit/node/cli.test.ts +++ b/test/unit/node/cli.test.ts @@ -6,6 +6,7 @@ import * as path from "path" import { Args, bindAddrFromArgs, + defaultConfigFile, parse, setDefaults, shouldOpenInExistingInstance, @@ -642,3 +643,18 @@ describe("bindAddrFromArgs", () => { resetValue() }) }) + +describe("defaultConfigFile", () => { + it("should return the dfeault config file as a string", async () => { + const actualDefaultConfigFile = await defaultConfigFile() + // Since the password is autogenerated within the function + // we can't assert it with .toMatch + // but we can check that the config at least includes + // these strings. + const expectedStrings = [`bind-addr: 127.0.0.1:8080`, `auth: password`, `password`, `cert: false`] + + expectedStrings.forEach((str) => { + expect(actualDefaultConfigFile).toContain(str) + }) + }) +}) From 6c95f72d2bc864c91d3829c76e77e2b8ad0165ab Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Tue, 28 Sep 2021 15:51:00 -0700 Subject: [PATCH 4/4] refactor: make password param to defaultConfigFile --- src/node/cli.ts | 10 ++++++---- test/unit/node/cli.test.ts | 19 ++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/node/cli.ts b/src/node/cli.ts index 91260604d..088431b7a 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -505,17 +505,18 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi /** * 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 async function defaultConfigFile(): Promise { +export function defaultConfigFile(password: string): string { return `bind-addr: 127.0.0.1:8080 auth: password -password: ${await generatePassword()} +password: ${password} cert: false ` } @@ -540,7 +541,8 @@ export async function readConfigFile(configPath?: string): Promise { 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)}`) diff --git a/test/unit/node/cli.test.ts b/test/unit/node/cli.test.ts index 9bf9fbe67..97b648788 100644 --- a/test/unit/node/cli.test.ts +++ b/test/unit/node/cli.test.ts @@ -14,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 = { @@ -645,16 +645,13 @@ describe("bindAddrFromArgs", () => { }) describe("defaultConfigFile", () => { - it("should return the dfeault config file as a string", async () => { - const actualDefaultConfigFile = await defaultConfigFile() - // Since the password is autogenerated within the function - // we can't assert it with .toMatch - // but we can check that the config at least includes - // these strings. - const expectedStrings = [`bind-addr: 127.0.0.1:8080`, `auth: password`, `password`, `cert: false`] + it("should return the default config file as a string", async () => { + const password = await generatePassword() + const actual = defaultConfigFile(password) - expectedStrings.forEach((str) => { - expect(actualDefaultConfigFile).toContain(str) - }) + expect(actual).toMatch(`bind-addr: 127.0.0.1:8080 +auth: password +password: ${password} +cert: false`) }) })