Skip to content

Commit

Permalink
[TypeSpecValidation/LinterRuleset] Fail if deprecated rulesets detect…
Browse files Browse the repository at this point in the history
…ed (Azure#29270)
  • Loading branch information
mikeharder authored May 30, 2024
1 parent 19b6661 commit 34f8f3f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
33 changes: 32 additions & 1 deletion eng/tools/typespec-validation/src/rules/linter-ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { Rule } from "../rule.js";
import { RuleResult } from "../rule-result.js";
import { TsvHost } from "../tsv-host.js";

// Maps deprecated rulesets to the replacement rulesets
const deprecatedRulesets = new Map<string, string>([
["@azure-tools/typespec-azure-core/all", "@azure-tools/typespec-azure-rulesets/data-plane"],
[
"@azure-tools/typespec-azure-resource-manager/all",
"@azure-tools/typespec-azure-rulesets/resource-manager",
],
]);

export class LinterRulesetRule implements Rule {
readonly name = "LinterRuleset";

Expand Down Expand Up @@ -33,7 +42,7 @@ export class LinterRulesetRule implements Rule {
}
stdOutput += `files: ${JSON.stringify(files)}\n`;

const linterExtends = config?.linter?.extends;
const linterExtends: string[] = config?.linter?.extends;
stdOutput += `linter.extends: ${JSON.stringify(linterExtends)}`;

let requiredRuleset = "";
Expand All @@ -57,6 +66,28 @@ export class LinterRulesetRule implements Rule {
' azure-resource-provider-folder: "data-plane" | "resource-manager"\n';
}

if (linterExtends) {
for (const ruleset of linterExtends) {
if (deprecatedRulesets.has(ruleset)) {
const newRuleset = deprecatedRulesets.get(ruleset);

success = false;
errorOutput +=
"tspconfig.yaml references the following ruleset which is deprecated:\n" +
"\n" +
"linter:\n" +
" extends:\n" +
` - "${ruleset}"\n` +
"\n" +
"It should be replaced with the following:\n" +
"\n" +
"linter:\n" +
" extends:\n" +
` - "${newRuleset}"`;
}
}
}

if (requiredRuleset && !linterExtends?.includes(requiredRuleset)) {
success = false;
errorOutput +=
Expand Down
31 changes: 31 additions & 0 deletions eng/tools/typespec-validation/test/linter-ruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,35 @@ linter:
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder);
assert(!result.success);
});

it("fails with data-plane/old-and-new", async function () {
const host = new TsvTestHost();
host.readTspConfig = async (_folder: string) => `
options:
"@azure-tools/typespec-autorest":
azure-resource-provider-folder: "data-plane"
linter:
extends:
- "@azure-tools/typespec-azure-core/all"
- "@azure-tools/typespec-azure-rulesets/data-plane"
`;
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder);
assert(!result.success);
});

it("fails with resource-manager/old-and-new", async function () {
const host = new TsvTestHost();
host.readTspConfig = async (_folder: string) => `
options:
"@azure-tools/typespec-autorest":
azure-resource-provider-folder: "resource-manager"
linter:
extends:
- "@azure-tools/typespec-azure-resource-manager/all"
- "@azure-tools/typespec-azure-rulesets/resource-manager"
`;
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder);

assert(!result.success);
});
});

0 comments on commit 34f8f3f

Please sign in to comment.