-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0e0edb
commit 72e1b41
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
.github/workflows/test/arm-incremental-typespec-preview.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
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); | ||
}); | ||
}); |