Skip to content

Commit

Permalink
refactor(config-utl): makes babel json5 configs and known violations …
Browse files Browse the repository at this point in the history
…read async as well
  • Loading branch information
sverweij committed Apr 27, 2023
1 parent 52f5fa0 commit 00965ee
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/cli/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
6 changes: 3 additions & 3 deletions src/config-utl/extract-babel-config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from "node:fs";
import { readFile } from "node:fs/promises";

import { extname } from "node:path";
import json5 from "json5";
Expand Down Expand Up @@ -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}':` +
Expand Down
3 changes: 1 addition & 2 deletions src/config-utl/extract-depcruise-config/read-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
6 changes: 3 additions & 3 deletions src/config-utl/extract-known-violations.mjs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
65 changes: 40 additions & 25 deletions test/config-utl/extract-known-violations.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
]);
]
);
});
});

0 comments on commit 00965ee

Please sign in to comment.