This is an automated email from the ASF dual-hosted git repository. dcremonini pushed a commit to branch feat/check-existing-file-in-configurator in repository https://gitbox.apache.org/repos/asf/openserverless-task.git
commit c38c3eddea6474879f277c61fb37c1f72f095b07 Author: Daniele Cremonini <[email protected]> AuthorDate: Mon Oct 7 21:56:35 2024 +0200 fix(configurator): show a message stating that file was not found instead of being invalid. --- util/config/configurator/configurator.ts | 7 +++++++ util/config/configurator/tests/index.test.ts | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/util/config/configurator/configurator.ts b/util/config/configurator/configurator.ts index ec7befa..63a50e4 100644 --- a/util/config/configurator/configurator.ts +++ b/util/config/configurator/configurator.ts @@ -25,6 +25,7 @@ const OPS = process.env.OPS || "ops"; export const AdditionalArgsMsg = "Additional arguments will be ignored."; export const NotValidJsonMsg = "Not a valid JSON file"; +export const FileNotFoundJsonMsg = "The JSON file was not found. Pathname: "; export const BadConfigMsg = "Bad configuration file. Check the help message (-h) to see the expected format."; @@ -269,8 +270,14 @@ export function readPositionalFile(positionals: string[]): { export async function parsePositionalFile( path: string ): Promise<{ success: boolean; message?: string; body?: any }> { + const file = Bun.file(Bun.pathToFileURL(path)); + if (!await file.exists()) { + return { success: false, message: FileNotFoundJsonMsg + Bun.pathToFileURL(path) }; + } + + try { const contents = await file.json(); return { success: true, body: contents }; diff --git a/util/config/configurator/tests/index.test.ts b/util/config/configurator/tests/index.test.ts index 0d10cc3..492af72 100644 --- a/util/config/configurator/tests/index.test.ts +++ b/util/config/configurator/tests/index.test.ts @@ -7,6 +7,7 @@ import { AdditionalArgsMsg, isInputConfigValid, findMissingConfig, + FileNotFoundJsonMsg, } from "../configurator.ts"; describe("findMissingConfig", () => { @@ -96,9 +97,10 @@ describe("readPositionalFile", () => { describe("parsePositionalFile", () => { test("file does not exist", async () => { - const jsonRes = await parsePositionalFile("not-existing"); + const filename = "not-existing.json"; + const jsonRes = await parsePositionalFile(filename); expect(jsonRes.success).toBe(false); - expect(jsonRes.message).toBe(NotValidJsonMsg); + expect(jsonRes.message).toStartWith(FileNotFoundJsonMsg); }); test("not a json file", async () => {
