diff --git a/eng/tools/typespec-validation/src/rules/linter-ruleset.ts b/eng/tools/typespec-validation/src/rules/linter-ruleset.ts index 818245000003..c6aee71db104 100644 --- a/eng/tools/typespec-validation/src/rules/linter-ruleset.ts +++ b/eng/tools/typespec-validation/src/rules/linter-ruleset.ts @@ -1,4 +1,3 @@ -import { readFile } from "fs/promises"; import { join } from "path"; import { parse as yamlParse } from "yaml"; import { Rule } from "../rule.js"; @@ -16,12 +15,11 @@ export class LinterRulesetRule implements Rule { let stdOutput = ""; let errorOutput = ""; - const configFile = join(folder, "tspconfig.yaml"); - const configText = await readFile(configFile, "utf8"); + const configText = await host.readTspConfig(folder); const config = yamlParse(configText); const rpFolder = - config.options?.["@azure-tools/typespec-autorest"]?.["azure-resource-provider-folder"]; + config?.options?.["@azure-tools/typespec-autorest"]?.["azure-resource-provider-folder"]; stdOutput += `azure-resource-provider-folder: ${JSON.stringify(rpFolder)}\n`; const mainTspExists = await host.checkFileExists(join(folder, "main.tsp")); @@ -35,7 +33,7 @@ export class LinterRulesetRule implements Rule { } stdOutput += `files: ${JSON.stringify(files)}\n`; - const linterExtends = config.linter?.extends; + const linterExtends = config?.linter?.extends; stdOutput += `linter.extends: ${JSON.stringify(linterExtends)}`; let requiredRuleset = ""; diff --git a/eng/tools/typespec-validation/test/linter-ruleset.test.ts b/eng/tools/typespec-validation/test/linter-ruleset.test.ts new file mode 100644 index 000000000000..b830b262e8f5 --- /dev/null +++ b/eng/tools/typespec-validation/test/linter-ruleset.test.ts @@ -0,0 +1,99 @@ +import { describe, it } from "vitest"; +import { join } from "path"; +import { LinterRulesetRule } from "../src/rules/linter-ruleset.js"; +import { TsvTestHost } from "./tsv-test-host.js"; +import { strict as assert } from "node:assert"; + +describe("linter-ruleset", function () { + it("succeeds with default config", async function () { + const host = new TsvTestHost(); + const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); + assert(result.success); + }); + + it("succeeds with resource-manager/resource-manager", 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-rulesets/resource-manager" +`; + const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); + assert(result.success); + }); + + it("succeeds with data-plane/data-plane", 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-rulesets/data-plane" +`; + const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); + assert(result.success); + }); + + it("succeeds with client.tsp/data-plane", async function () { + const host = new TsvTestHost(); + host.checkFileExists = async (file: string) => file === join(TsvTestHost.folder, "client.tsp"); + host.readTspConfig = async (_folder: string) => ` +linter: + extends: + - "@azure-tools/typespec-azure-rulesets/data-plane" +`; + const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); + assert(result.success); + }); + + it("fails with no-config", async function () { + const host = new TsvTestHost(); + host.readTspConfig = async (_folder: string) => ""; + const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); + assert(!result.success); + }); + + it("fails with resource-manager/no-linter", async function () { + const host = new TsvTestHost(); + host.readTspConfig = async (_folder: string) => ` +options: + "@azure-tools/typespec-autorest": + azure-resource-provider-folder: "resource-manager" +`; + const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); + assert(!result.success); + }); + + it("fails with resource-manager/data-plane", 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-rulesets/data-plane" +`; + const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); + assert(!result.success); + }); + + it("fails with data-plane/resource-manager", 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-rulesets/resource-manager" +`; + const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); + assert(!result.success); + }); +});