diff --git a/src/cli/index.mjs b/src/cli/index.mjs index 711c094ce..5b64f663b 100644 --- a/src/cli/index.mjs +++ b/src/cli/index.mjs @@ -40,7 +40,7 @@ async function addKnownViolations(pCruiseOptions) { const { default: extractKnownViolations } = await import( "../config-utl/extract-known-violations.mjs" ); - const lKnownViolations = extractKnownViolations( + const lKnownViolations = await extractKnownViolations( pCruiseOptions.knownViolationsFile ); diff --git a/src/config-utl/extract-babel-config.mjs b/src/config-utl/extract-babel-config.mjs index d8c511096..67f5cf9c7 100644 --- a/src/config-utl/extract-babel-config.mjs +++ b/src/config-utl/extract-babel-config.mjs @@ -1,4 +1,4 @@ -import { readFileSync } from "node:fs"; +import { readFile } from "node:fs/promises"; import { extname } from "node:path"; import json5 from "json5"; @@ -35,11 +35,11 @@ async function getJSConfig(pBabelConfigFileName) { return lReturnValue; } -function getJSON5Config(pBabelConfigFileName) { +async function getJSON5Config(pBabelConfigFileName) { let lReturnValue = {}; try { - lReturnValue = json5.parse(readFileSync(pBabelConfigFileName, "utf8")); + lReturnValue = json5.parse(await readFile(pBabelConfigFileName, "utf8")); } catch (pError) { throw new Error( `Encountered an error while parsing the babel config '${pBabelConfigFileName}':` + diff --git a/src/config-utl/extract-depcruise-config/read-config.mjs b/src/config-utl/extract-depcruise-config/read-config.mjs index 8b3be4219..47a6afee8 100644 --- a/src/config-utl/extract-depcruise-config/read-config.mjs +++ b/src/config-utl/extract-depcruise-config/read-config.mjs @@ -11,6 +11,5 @@ export default async function readConfig(pAbsolutePathToConfigFile) { ); return config; } - const lConfig = await readFile(pAbsolutePathToConfigFile, "utf8"); - return json5.parse(lConfig); + return json5.parse(await readFile(pAbsolutePathToConfigFile, "utf8")); } diff --git a/src/config-utl/extract-known-violations.mjs b/src/config-utl/extract-known-violations.mjs index 620352ca5..ac58ee610 100644 --- a/src/config-utl/extract-known-violations.mjs +++ b/src/config-utl/extract-known-violations.mjs @@ -1,11 +1,11 @@ -import { readFileSync } from "node:fs"; +import { readFile } from "node:fs/promises"; import json5 from "json5"; import makeAbsolute from "./make-absolute.mjs"; -export default function extractKnownViolations(pKnownViolationsFileName) { +export default async function extractKnownViolations(pKnownViolationsFileName) { try { return json5.parse( - readFileSync(makeAbsolute(pKnownViolationsFileName), "utf8") + await readFile(makeAbsolute(pKnownViolationsFileName), "utf8") ); } catch (pError) { if (pError instanceof SyntaxError) { diff --git a/test/config-utl/extract-known-violations.spec.mjs b/test/config-utl/extract-known-violations.spec.mjs index ac87ebf61..9d5a9f169 100644 --- a/test/config-utl/extract-known-violations.spec.mjs +++ b/test/config-utl/extract-known-violations.spec.mjs @@ -8,38 +8,53 @@ describe("[I] config-utl/extractKnownViolations", () => { process.chdir(WORKINGDIR); }); - it("Throws when passed a non-existing file", () => { - expect(() => - extractKnownViolations("this_file_really_does_not_exist") - ).to.throw(); + it("Throws when passed a non-existing file", async () => { + let lError = "none"; + + try { + await extractKnownViolations("this_file_really_does_not_exist"); + } catch (pError) { + lError = pError.toString(); + } + expect(lError).to.contain(`ENOENT: no such file or directory, open`); + expect(lError).to.contain("this_file_really_does_not_exist"); }); - it("Throws a SyntaxError when passed non-json", () => { + it("Throws a SyntaxError when passed non-json", async () => { process.chdir("./test/config-utl/__mocks__/known-violations"); - expect(() => extractKnownViolations("this-is-no-json.txt")).to.throw( - SyntaxError - ); + + let lError = "none"; + + try { + await extractKnownViolations("this-is-no-json.txt"); + } catch (pError) { + lError = pError; + } + + expect(lError).to.be.instanceOf(SyntaxError); }); - it("Return the parsed json content of the violations file", () => { + it("Return the parsed json content of the violations file", async () => { process.chdir("./test/config-utl/__mocks__/known-violations"); - expect(extractKnownViolations("known-violations.json")).to.deep.equal([ - { - from: "src/schema/baseline-violations.schema.js", - to: "src/schema/baseline-violations.schema.js", - rule: { - severity: "error", - name: "not-unreachable-from-cli", + expect(await extractKnownViolations("known-violations.json")).to.deep.equal( + [ + { + from: "src/schema/baseline-violations.schema.js", + to: "src/schema/baseline-violations.schema.js", + rule: { + severity: "error", + name: "not-unreachable-from-cli", + }, }, - }, - { - from: "src/cli/format.js", - to: "src/cli/format.js", - rule: { - severity: "info", - name: "not-reachable-from-folder-index", + { + from: "src/cli/format.js", + to: "src/cli/format.js", + rule: { + severity: "info", + name: "not-reachable-from-folder-index", + }, }, - }, - ]); + ] + ); }); });