Skip to content

Commit

Permalink
Add passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeharder committed Feb 13, 2025
1 parent e0e0edb commit 72e1b41
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions .github/workflows/test/arm-incremental-typespec-preview.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { vol } from "memfs";

Check failure on line 1 in .github/workflows/test/arm-incremental-typespec-preview.test.js

View workflow job for this annotation

GitHub Actions / Protected Files

File '.github/workflows/test/arm-incremental-typespec-preview.test.js' should only be updated by the Azure SDK team. If intentional, the PR may be merged by the Azure SDK team via bypassing the branch protections.
import { join } from "path";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createMockCore } from "../../test/mocks.js";
import incrementalTypespec from "../src/arm-incremental-typespec-preview.js";
import * as changedFiles from "../src/changed-files.js";
import * as git from "../src/git.js";

vi.mock("fs/promises", async () => {
const memfs = await import("memfs");
return {
...memfs.fs.promises,
};
});

const core = createMockCore();

const swaggerTypeSpecGenerated = JSON.stringify({
info: {
"x-typespec-generated": [{ emitter: "@azure-tools/typespec-autorest" }],
},
});

describe("incrementalTypeSpec", () => {
beforeEach(() => {
// TODO: Reset other global mocks like "core"
vol.reset();
});

it("rejects if inputs null", async () => {
await expect(incrementalTypespec({})).rejects.toThrow();
});

it("returns false if no changed RM files", async () => {
vi.spyOn(
changedFiles,
"getChangedResourceManagerSwaggerFiles",
).mockResolvedValue([]);

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

it("returns false if a changed file is not typespec-generated", async () => {
const swaggerPath =
"/specification/contosowidgetmanager/resource-manager/Microsoft.Contoso/preview/2021-10-01-preview/contoso.json";

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

vol.fromJSON({
[join(process.env.GITHUB_WORKSPACE || "", swaggerPath)]: '"foo"',
});

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

it("returns false if changed files add a new RP", async () => {
const swaggerPath =
"/specification/contosowidgetmanager2/resource-manager/Microsoft.Contoso/preview/2021-10-01-preview/contoso.json";

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

vol.fromJSON({
[join(process.env.GITHUB_WORKSPACE || "", swaggerPath)]:
swaggerTypeSpecGenerated,
});

// "git ls-tree" returns "" if the spec folder doesn't exist in the base branch
vi.spyOn(git, "lsTree").mockResolvedValue("");

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

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";

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

vol.fromJSON({
[join(process.env.GITHUB_WORKSPACE || "", swaggerPath)]:
swaggerTypeSpecGenerated,
});

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

vi.spyOn(git, "show").mockImplementation(async (_treeIsh, _path, _core) => {
return swaggerTypeSpecGenerated;
});

await expect(incrementalTypespec({ core })).resolves.toBe(true);
});
});

0 comments on commit 72e1b41

Please sign in to comment.