Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
23 changes: 19 additions & 4 deletions eng/tools/typespec-validation/src/rules/folder-structure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "path";
import { parse as yamlParse } from "yaml";
import { Rule } from "../rule.js";
import { RuleResult } from "../rule-result.js";
import { TsvHost } from "../tsv-host.js";
Expand Down Expand Up @@ -63,6 +64,7 @@ export class FolderStructureRule implements Rule {
// Verify tspconfig, main.tsp, examples/
let mainExists = await host.checkFileExists(path.join(folder, "main.tsp"));
let clientExists = await host.checkFileExists(path.join(folder, "client.tsp"));
let tspConfigExists = await host.checkFileExists(path.join(folder, "tspconfig.yaml"));

if (!mainExists && !clientExists) {
errorOutput += `Invalid folder structure: Spec folder must contain main.tsp or client.tsp.`;
Expand All @@ -74,14 +76,27 @@ export class FolderStructureRule implements Rule {
success = false;
}

if (
!packageFolder.includes("Shared") &&
!(await host.checkFileExists(path.join(folder, "tspconfig.yaml")))
) {
if (!packageFolder.includes("Shared") && !tspConfigExists) {
errorOutput += `Invalid folder structure: Spec folder must contain tspconfig.yaml.`;
success = false;
}

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

if (rpFolder?.trim()?.endsWith("resource-manager") && !packageFolder.endsWith(".Management")) {
errorOutput += `Invalid folder structure: TypeSpec for resource-manager specs must be in a folder ending with '.Management'`;
success = false;
}
else if (!rpFolder?.trim()?.endsWith("resource-manager") && packageFolder.endsWith(".Management")) {
errorOutput += `Invalid folder structure: TypeSpec for data-plane specs or shared code must be in a folder NOT ending with '.Management'`;
success = false;
}
}

return {
success: success,
stdOutput: stdOutput,
Expand Down
90 changes: 90 additions & 0 deletions eng/tools/typespec-validation/test/folder-structure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,94 @@ describe("folder-structure", function () {
assert(result.errorOutput);
assert(result.errorOutput.includes("must contain"));
});

it("should succeed with resource-manager/Management", async function() {
let host = new TsvTestHost();
host.globby = async () => {
return ["/foo/Foo.Management/tspconfig.yaml"];
};
host.normalizePath = () => {
return "/gitroot";
};
host.readTspConfig = async (_folder: string) => `
options:
"@azure-tools/typespec-autorest":
azure-resource-provider-folder: "resource-manager"
`;

const result = await new FolderStructureRule().execute(
host,
"/gitroot/specification/foo/Foo.Management",
);

assert(result.success);
});

it("should succeed with data-plane/NoManagement", async function() {
let host = new TsvTestHost();
host.globby = async () => {
return ["/foo/Foo/tspconfig.yaml"];
};
host.normalizePath = () => {
return "/gitroot";
};
host.readTspConfig = async (_folder: string) => `
options:
"@azure-tools/typespec-autorest":
azure-resource-provider-folder: "data-plane"
`;

const result = await new FolderStructureRule().execute(
host,
"/gitroot/specification/foo/Foo",
);

assert(result.success);
});

it("should fail with resource-manager/NoManagement", async function() {
let host = new TsvTestHost();
host.globby = async () => {
return ["/foo/Foo/tspconfig.yaml"];
};
host.normalizePath = () => {
return "/gitroot";
};
host.readTspConfig = async (_folder: string) => `
options:
"@azure-tools/typespec-autorest":
azure-resource-provider-folder: "resource-manager"
`;

const result = await new FolderStructureRule().execute(
host,
"/gitroot/specification/foo/Foo",
);

assert(result.errorOutput);
assert(result.errorOutput.includes(".Management"));
});

it("should fail with data-plane/Management", async function() {
let host = new TsvTestHost();
host.globby = async () => {
return ["/foo/Foo.Management/tspconfig.yaml"];
};
host.normalizePath = () => {
return "/gitroot";
};
host.readTspConfig = async (_folder: string) => `
options:
"@azure-tools/typespec-autorest":
azure-resource-provider-folder: "data-plane"
`;

const result = await new FolderStructureRule().execute(
host,
"/gitroot/specification/foo/Foo.Management",
);

assert(result.errorOutput);
assert(result.errorOutput.includes(".Management"));
});
});