Skip to content

Commit

Permalink
[ARM Incremental TypeSpec] Fail if file is deleted (#32905)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeharder authored Feb 28, 2025
1 parent ff6f078 commit f407c80
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/src/arm-incremental-typespec-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,19 @@ export default async function incrementalTypeSpec({ github, context, core }) {

// If any changed file is not typespec-generated, return false
for (const file of changedRmSwaggerFiles) {
const swagger = await show("HEAD", file, core);
let swagger;
try {
swagger = await show("HEAD", file, core);
} catch (e) {
if (e instanceof Error && e.message.includes("does not exist")) {
// To simplify logic, if PR deletes a swagger file, it's not "incremental typespec"
core.info(`File "${file}" has been deleted`);
return false;
} else {
// Unknown error
throw e;
}
}

const swaggerObj = JSON.parse(swagger);

Expand Down
58 changes: 57 additions & 1 deletion .github/workflows/test/arm-incremental-typespec-preview.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as git from "../src/git.js";

const core = createMockCore();

const swaggerHandWritten = JSON.stringify("foo");

const swaggerTypeSpecGenerated = JSON.stringify({
info: {
"x-typespec-generated": [{ emitter: "@azure-tools/typespec-autorest" }],
Expand Down Expand Up @@ -35,7 +37,7 @@ describe("incrementalTypeSpec", () => {
"getChangedResourceManagerSwaggerFiles",
).mockResolvedValue([swaggerPath]);

vi.spyOn(git, "show").mockResolvedValue('"foo"');
vi.spyOn(git, "show").mockResolvedValue(swaggerHandWritten);

await expect(incrementalTypeSpec({ core })).resolves.toBe(false);
});
Expand All @@ -57,6 +59,60 @@ describe("incrementalTypeSpec", () => {
await expect(incrementalTypeSpec({ core })).resolves.toBe(false);
});

it("returns false if swagger deleted", async () => {
const swaggerPath =
"/specification/contosowidgetmanager/resource-manager/Microsoft.Contoso/preview/2021-10-01-preview/contoso.json";

vi.spyOn(
changedFiles,
"getChangedResourceManagerSwaggerFiles",
).mockResolvedValue([swaggerPath]);

vi.spyOn(git, "show").mockRejectedValue(
new Error("path contoso.json does not exist in 'HEAD'"),
);

await expect(incrementalTypeSpec({ core })).resolves.toBe(false);
});

it("returns false if tsp conversion", async () => {
const swaggerPath =
"/specification/contosowidgetmanager/resource-manager/Microsoft.Contoso/preview/2021-10-01-preview/contoso.json";

vi.spyOn(
changedFiles,
"getChangedResourceManagerSwaggerFiles",
).mockResolvedValue([swaggerPath]);

vi.spyOn(git, "show").mockImplementation((treeIsh) =>
treeIsh == "HEAD" ? swaggerTypeSpecGenerated : swaggerHandWritten,
);

vi.spyOn(git, "lsTree").mockImplementation(
async (_treeIsh, _path, _core, options) => {
return options?.includes("-r --name-only")
? swaggerPath.substring(1)
: "040000 tree abc123 specification/contosowidgetmanager";
},
);

await expect(incrementalTypeSpec({ core })).resolves.toBe(false);
});

it("throws if git show returns unknown error", async () => {
const swaggerPath =
"/specification/contosowidgetmanager2/resource-manager/Microsoft.Contoso/preview/2021-10-01-preview/contoso.json";

vi.spyOn(
changedFiles,
"getChangedResourceManagerSwaggerFiles",
).mockResolvedValue([swaggerPath]);

vi.spyOn(git, "show").mockRejectedValue("string error");

await expect(incrementalTypeSpec({ core })).rejects.toThrowError();
});

it("returns true if changed files are incremental changes to an existing TypeSpec RP", async () => {
const swaggerPath =
"/specification/contosowidgetmanager/resource-manager/Microsoft.Contoso/preview/2021-10-01-preview/contoso.json";
Expand Down

0 comments on commit f407c80

Please sign in to comment.