Skip to content
Open
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
24 changes: 8 additions & 16 deletions eng/tools/typespec-validation/src/rules/linter-ruleset.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { join } from "path";
import { RuleResult } from "../rule-result.js";
import { Rule } from "../rule.js";
Expand Down Expand Up @@ -27,10 +27,6 @@
const configText = await readTspConfig(folder);
const config = parse(configText);

const rpFolder =
config?.options?.["@azure-tools/typespec-autorest"]?.["azure-resource-provider-folder"];
stdOutput += `azure-resource-provider-folder: ${JSON.stringify(rpFolder)}\n`;

const mainTspExists = await fileExists(join(folder, "main.tsp"));
const clientTspExists = await fileExists(join(folder, "client.tsp"));
const files = [];
Expand All @@ -45,25 +41,21 @@
const linterExtends = config?.linter?.extends;
stdOutput += `linter.extends: ${JSON.stringify(linterExtends)}`;

// Normalize path separators
const normalizedFolder = folder.replace(/\\/g, "/");

let requiredRuleset = "";
if (rpFolder?.trim()?.endsWith("resource-manager")) {
if (
normalizedFolder.includes("/resource-manager/") ||
normalizedFolder.trim().endsWith(".Management")
) {
requiredRuleset = "@azure-tools/typespec-azure-rulesets/resource-manager";
} else if (rpFolder?.trim()?.endsWith("data-plane")) {
requiredRuleset = "@azure-tools/typespec-azure-rulesets/data-plane";
} else if (clientTspExists && !mainTspExists) {
// Assume folders with no autorest setting, containing only "client.tsp" but no "main.tsp",
// are data-plane (e.g. HealthInsights.TrialMatcher)
requiredRuleset = "@azure-tools/typespec-azure-rulesets/data-plane";
} else {
// Cannot determine if spec is data-plane or resource-manager, so cannot know
// which linter ruleset is required.
success = false;
errorOutput +=
"tspconfig.yaml must define the following property:\n" +
"\n" +
"options:\n" +
' "@azure-tools/typespec-autorest":\n' +
' azure-resource-provider-folder: "data-plane" | "resource-manager"\n';
requiredRuleset = "@azure-tools/typespec-azure-rulesets/data-plane";
}

if (linterExtends) {
Expand Down
40 changes: 8 additions & 32 deletions eng/tools/typespec-validation/test/linter-ruleset.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { contosoTspConfig } from "@azure-tools/specs-shared/test/examples";
import { strict as assert } from "node:assert";
import { join } from "path";
Expand Down Expand Up @@ -29,30 +29,24 @@
it("succeeds with resource-manager/resource-manager", async function () {
readTspConfigSpy.mockImplementation(() =>
Promise.resolve(`
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(mockFolder);
const result = await new LinterRulesetRule().execute("specification/foo/resource-manager/Foo");
assert(result.success);
});

it("succeeds with data-plane/data-plane", async function () {
readTspConfigSpy.mockImplementation(() =>
Promise.resolve(`
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(mockFolder);
const result = await new LinterRulesetRule().execute("specification/foo/data-plane/Foo");
assert(result.success);
});

Expand Down Expand Up @@ -80,76 +74,58 @@
});

it("fails with resource-manager/no-linter", async function () {
readTspConfigSpy.mockImplementation(() =>
Promise.resolve(`
options:
"@azure-tools/typespec-autorest":
azure-resource-provider-folder: "resource-manager"
`),
);
const result = await new LinterRulesetRule().execute(mockFolder);
readTspConfigSpy.mockImplementation(() => Promise.resolve(``));
const result = await new LinterRulesetRule().execute("specification/foo/resource-manager/Foo");
assert(!result.success);
});

it("fails with resource-manager/data-plane", async function () {
readTspConfigSpy.mockImplementation(() =>
Promise.resolve(`
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(mockFolder);
const result = await new LinterRulesetRule().execute("specification/foo/Foo.Management");
assert(!result.success);
});

it("fails with data-plane/resource-manager", async function () {
readTspConfigSpy.mockImplementation(() =>
Promise.resolve(`
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(mockFolder);
const result = await new LinterRulesetRule().execute("specification/foo/data-plane/Foo");
assert(!result.success);
});

it("fails with data-plane/old-and-new", async function () {
readTspConfigSpy.mockImplementation(() =>
Promise.resolve(`
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(mockFolder);
const result = await new LinterRulesetRule().execute("specification/foo/data-plane/Foo");
assert(!result.success);
});

it("fails with resource-manager/old-and-new", async function () {
readTspConfigSpy.mockImplementation(() =>
Promise.resolve(`
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(mockFolder);
const result = await new LinterRulesetRule().execute("specification/foo/resource-manager/Foo");

assert(!result.success);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ linter:
- "@azure-tools/typespec-azure-rulesets/data-plane"
options:
"@azure-tools/typespec-autorest":
# TODO: Does anything need this set, if it's not used in output-file?
azure-resource-provider-folder: "data-plane"
emit-lro-options: "none"
emitter-output-dir: "{project-root}"
output-file: "{version-status}/{version}/widgetanalytics.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ options:
"@azure-tools/typespec-autorest":
use-read-only-status-schema: true
emitter-output-dir: "{project-root}"
# TODO: Does anything need this set, if it's not used in output-file? Currently required by TSV.
azure-resource-provider-folder: "resource-manager"
output-file: "{version-status}/{version}/widget.json"
arm-types-dir: "{project-root}/../../../../common-types/resource-management"
"@azure-typespec/http-client-csharp-mgmt":
Expand Down
Loading