Skip to content

Commit

Permalink
test(validate): adds automated non-regression tests for viaOnly rules…
Browse files Browse the repository at this point in the history
… with dependencyTypes
  • Loading branch information
sverweij committed Dec 21, 2023
1 parent d25822a commit a928dfb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 22 deletions.
14 changes: 7 additions & 7 deletions src/validate/matchers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ function toViaOnly(pRule, pDependency, pGroups) {
name.match(replaceGroupPlaceholders(pRule.to.viaOnly.pathNot, pGroups)),
);
}
// if (pRule.to.viaOnly.dependencyTypes) {
// lReturnValue &&= pDependency.cycle.every(({ dependencyTypes }) =>
// pRule.to.viaOnly.dependencyTypes.some((pRuleDependencyType) =>
// dependencyTypes.includes(pRuleDependencyType)
// )
// );
// }
if (pRule.to.viaOnly.dependencyTypes) {
lReturnValue &&= pDependency.cycle.every(({ dependencyTypes }) =>
pRule.to.viaOnly.dependencyTypes.some((pRuleDependencyType) =>
dependencyTypes.includes(pRuleDependencyType),
),
);
}
if (pRule.to.viaOnly.dependencyTypesNot) {
lReturnValue &&= !pDependency.cycle.some(({ dependencyTypes }) =>
pRule.to.viaOnly.dependencyTypesNot.some((pRuleDependencyType) =>
Expand Down
96 changes: 81 additions & 15 deletions test/validate/index.cycle-via-only.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,7 @@ describe("[I] validate/index dependency - cycle viaOnly", () => {
},
],
});
const lCycleViaNotTypeOnlyRuleSet = parseRuleSet({
forbidden: [
{
name: "no-runtime-cycles",
from: {},
severity: "error",
to: {
circular: true,
viaOnly: { dependencyTypesNot: ["type-only"] },
},
},
],
});

it("a => ba => bb => bc => a doesn't get flagged when the cycle doesn't go via the viaOnly", () => {
deepEqual(
validate.dependency(
Expand Down Expand Up @@ -119,7 +107,22 @@ describe("[I] validate/index dependency - cycle viaOnly", () => {
},
);
});
it("a => aa => ab => ac => a doesn't get flagged when one of the dependencyTypes is in a viaNot", () => {

const lCycleViaNotTypeOnlyRuleSet = parseRuleSet({
forbidden: [
{
name: "no-runtime-cycles",
from: {},
severity: "error",
to: {
circular: true,
viaOnly: { dependencyTypesNot: ["type-only"] },
},
},
],
});

it("a => aa => ab => ac => a doesn't get flagged when one of the dependencyTypes is in a pathNot", () => {
deepEqual(
validate.dependency(
lCycleViaNotTypeOnlyRuleSet,
Expand All @@ -141,7 +144,7 @@ describe("[I] validate/index dependency - cycle viaOnly", () => {
);
});

it("a => aa => ab => ac => a does get flagged when none of the dependencyTypes is in a viaNot", () => {
it("a => aa => ab => ac => a does get flagged when none of the dependencyTypes is in a pathNot", () => {
deepEqual(
validate.dependency(
lCycleViaNotTypeOnlyRuleSet,
Expand All @@ -168,6 +171,69 @@ describe("[I] validate/index dependency - cycle viaOnly", () => {
},
);
});

const lCycleViaTypeOnlyRuleSet = parseRuleSet({
forbidden: [
{
name: "flags-import-only-cycles",
from: {},
severity: "error",
to: {
circular: true,
viaOnly: { dependencyTypes: ["import"] },
},
},
],
});
it("a => aa => ab => ac => a does get flagged when none of the dependencyTypes is in a via", () => {
deepEqual(
validate.dependency(
lCycleViaTypeOnlyRuleSet,
{ source: "tmp/a.js" },
{
resolved: "tmp/aa.js",
circular: true,
cycle: [
{ name: "tmp/aa.js", dependencyTypes: ["import"] },
{ name: "tmp/ab.js", dependencyTypes: ["import"] },
{ name: "tmp/ac.js", dependencyTypes: ["import"] },
{ name: "tmp/a.js", dependencyTypes: ["import"] },
],
},
),
{
rules: [
{
name: "flags-import-only-cycles",
severity: "error",
},
],
valid: false,
},
);
});

it("a => aa => ab => ac => a doesn't get flagged when none of the dependencyTypes is in a via", () => {
deepEqual(
validate.dependency(
lCycleViaTypeOnlyRuleSet,
{ source: "tmp/a.js" },
{
resolved: "tmp/aa.js",
circular: true,
cycle: [
{ name: "tmp/aa.js", dependencyTypes: ["import"] },
{ name: "tmp/ab.js", dependencyTypes: ["import"] },
{ name: "tmp/ac.js", dependencyTypes: ["require"] },
{ name: "tmp/a.js", dependencyTypes: ["import"] },
],
},
),
{
valid: true,
},
);
});
});

describe("[I] validate/index dependency - cycle viaOnly - with group matching", () => {
Expand Down

0 comments on commit a928dfb

Please sign in to comment.