Skip to content

Commit

Permalink
[TypeSpecValidation] Add tests for LinterRuleset (#29267)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeharder authored May 29, 2024
1 parent b692152 commit 19b6661
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 5 deletions.
8 changes: 3 additions & 5 deletions eng/tools/typespec-validation/src/rules/linter-ruleset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { readFile } from "fs/promises";
import { join } from "path";
import { parse as yamlParse } from "yaml";
import { Rule } from "../rule.js";
Expand All @@ -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"));
Expand All @@ -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 = "";
Expand Down
99 changes: 99 additions & 0 deletions eng/tools/typespec-validation/test/linter-ruleset.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 19b6661

Please sign in to comment.