Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeSpecValidation] Add tests for LinterRuleset #29267

Merged
merged 9 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

@mikeharder mikeharder May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rules should only read config via host. Makes this rule consistent with others.

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"];
Copy link
Member Author

@mikeharder mikeharder May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle empty config. Makes this rule consistent with others.

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;
Copy link
Member Author

@mikeharder mikeharder May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle empty config. Makes this rule consistent with others.

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);
});
});