Skip to content
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
16 changes: 15 additions & 1 deletion eng/tools/lint-diff/src/generateReport.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { writeFile } from "node:fs/promises";
import { relative } from "node:path";
import { kebabCase } from "change-case";
Expand Down Expand Up @@ -44,11 +44,18 @@
const afterPath = getPath(after);
const beforePath = before ? getPath(before) : "";

outputMarkdown += `| ${afterName} | [${afterName}](${getFileLink(githubRepoPath, compareSha, afterPath)}) | [${beforeName}](${getFileLink(githubRepoPath, baseBranch, beforePath)}) |\n`;
outputMarkdown += `| ${afterName} | [${afterName}](${getFileLink(githubRepoPath, compareSha, afterPath)}) | [${beforeName}](${getFileLink(githubRepoPath, baseBranch, beforePath)}) ${getAutoRestFailedMessage(before)}|\n`;
}

outputMarkdown += `\n\n`;

for (const [, { before }] of runCorrelations.entries()) {
if (before && before.error) {
outputMarkdown += `> [!WARNING]\n`;
outputMarkdown += `> Autorest failed checking before state of ${relative(before.rootPath, before.readme.path)} ${before.tag}\n\n`;
}
}

const [newViolations, existingViolations] = getViolations(runCorrelations, affectedSwaggers);

for (const newItem of newViolations) {
Expand Down Expand Up @@ -278,3 +285,10 @@
const readmePathRelative = relative(rootPath, readme.path);
return tag ? `${readmePathRelative}#tag-${tag}` : readmePathRelative;
}

export function getAutoRestFailedMessage(result: AutorestRunResult | null): string {
if (result?.error) {
return "Autorest Failed";
}
return "";
}
3 changes: 3 additions & 0 deletions eng/tools/lint-diff/src/lint-diff.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { parseArgs, ParseArgsConfig } from "node:util";
import { pathExists, getDependencyVersion, getPathToDependency } from "./util.js";
import { getRunList } from "./processChanges.js";
Expand Down Expand Up @@ -146,7 +146,10 @@

// It may be possible to run these in parallel as they're running against
// different directories.
console.log("Running checks on before state...");
const beforeChecks = await runChecks(beforePath, beforeList);

console.log("Running checks on after state...");
const afterChecks = await runChecks(afterPath, afterList);

// If afterChecks has AutoRest errors, fail the run.
Expand Down
90 changes: 90 additions & 0 deletions eng/tools/lint-diff/test/generateReport.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { beforeEach, test, describe, expect, vi } from "vitest";
import {
compareLintDiffViolations,
generateAutoRestErrorReport,
generateLintDiffReport,
getAutoRestFailedMessage,
getDocUrl,
getFile,
getFileLink,
Expand Down Expand Up @@ -51,6 +52,36 @@
});
});

describe("getAutoRestFailedMessage", () => {
test("returns empty string when result is null", () => {
expect(getAutoRestFailedMessage(null)).toEqual("");
});

test("returns empty string when result has no error", () => {
const result: AutorestRunResult = {
error: null,
rootPath: "",
readme: new Readme("file.md"),
tag: "default",
stdout: "",
stderr: "",
};
expect(getAutoRestFailedMessage(result)).toEqual("");
});

test("returns 'Autorest Failed' when result has an error", () => {
const result: AutorestRunResult = {
error: new Error("Autorest failed"),
rootPath: "",
readme: new Readme("file.md"),
tag: "default",
stdout: "",
stderr: "",
};
expect(getAutoRestFailedMessage(result)).toEqual("Autorest Failed");
});
});

describe("getLine", () => {
test("returns the line number", () => {
const violation = {
Expand Down Expand Up @@ -465,6 +496,65 @@
`);
});

test.skipIf(isWindows())(
"passes and displays warning if before has errors",
async ({ expect }) => {
const afterViolation = {
extensionName: "@microsoft.azure/openapi-validator",
level: "warning",
code: "SomeCode",
message: "A warning occurred",
source: [],
details: {},
};

const beforeResult = {
error: new Error("Autorest failed"),
stdout: "",
stderr: "",
rootPath: "",
readme: new Readme("file1.md"),
tag: "",
} as AutorestRunResult;
const afterResult = {
error: null,
stdout: JSON.stringify(afterViolation),
stderr: "",
rootPath: "",
readme: new Readme("file1.md"),
tag: "",
} as AutorestRunResult;

const runCorrelations = new Map<string, BeforeAfter>([
["file1.md", { before: beforeResult, after: afterResult }],
]);

const outFile = "test-output-fatal.md";
const actual = await generateLintDiffReport(
runCorrelations,
new Set<string>([
"specification/contosowidgetmanager/data-plane/Azure.Contoso.WidgetManager/stable/2022-12-01/widgets.json",
]),
outFile,
"baseBranch",
"compareSha",
"repo/path",
);
expect(actual).toBe(true);
expect(await readFile(outFile, { encoding: "utf-8" })).toMatchInlineSnapshot(`
"| Compared specs ([v1.0.0](https://www.npmjs.com/package/@microsoft.azure/openapi-validator/v/1.0.0)) | new version | base version |
| --- | --- | --- |
| default | [default](https://github.com/repo/path/blob/compareSha/file1.md) | [default](https://github.com/repo/path/blob/baseBranch/file1.md) Autorest Failed|


> [!WARNING]
> Autorest failed checking before state of file1.md

"
`);
},
);

test.skipIf(isWindows())(
"passes if new violations do not include an error (warnings only)",
async ({ expect }) => {
Expand Down
Loading