Skip to content

Commit 061f4e9

Browse files
authored
[.github/shared] Add tests for 100% codecov (#36459)
- Fix swagger.versionKind on windows
1 parent b0fd1e6 commit 061f4e9

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

.github/shared/src/github.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* v8 ignore start */
2+
13
// @ts-check
24

35
export const PER_PAGE_MAX = 100;

.github/shared/src/swagger.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { readFile } from "fs/promises";
55
import { dirname, relative, resolve } from "path";
66
import { mapAsync } from "./array.js";
77
import { example } from "./changed-files.js";
8+
import { includesFolder } from "./path.js";
89
import { SpecModelError } from "./spec-model-error.js";
910

1011
/**
@@ -191,7 +192,7 @@ export class Swagger {
191192
* @returns {string} version kind (stable or preview)
192193
*/
193194
get versionKind() {
194-
return dirname(this.#path).includes("/preview/")
195+
return includesFolder(this.#path, "preview")
195196
? API_VERSION_LIFECYCLE_STAGES.PREVIEW
196197
: API_VERSION_LIFECYCLE_STAGES.STABLE;
197198
}

.github/shared/test/changed-files.test.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ describe("changedFiles", () => {
4141
vi.mocked(simpleGit.simpleGit().diff).mockResolvedValue(files.join("\n"));
4242

4343
await expect(getChangedFiles(options)).resolves.toEqual(files);
44+
expect(simpleGit.simpleGit().diff).toHaveBeenCalledWith(["--name-only", "HEAD^", "HEAD"]);
45+
46+
const specFiles = files.filter((f) => f.startsWith("specification"));
47+
vi.mocked(simpleGit.simpleGit().diff).mockResolvedValue(specFiles.join("\n"));
48+
await expect(getChangedFiles({ ...options, paths: ["specification"] })).resolves.toEqual(
49+
specFiles,
50+
);
51+
expect(simpleGit.simpleGit().diff).toHaveBeenCalledWith([
52+
"--name-only",
53+
"HEAD^",
54+
"HEAD",
55+
"--",
56+
"specification",
57+
]);
4458
});
4559

4660
const files = [
@@ -174,6 +188,7 @@ describe("changedFiles", () => {
174188
"should categorize files correctly with all types of changes (%o)",
175189
async (options) => {
176190
const gitOutput = [
191+
"M\t.github/src/changed-files.js",
177192
"A\tspecification/new-service/readme.md",
178193
"M\tspecification/existing-service/main.tsp",
179194
"D\tspecification/old-service/contoso.json",
@@ -183,7 +198,31 @@ describe("changedFiles", () => {
183198
].join("\n");
184199

185200
vi.mocked(simpleGit.simpleGit().diff).mockResolvedValue(gitOutput);
186-
const result = await getChangedFilesStatuses(options);
201+
let result = await getChangedFilesStatuses(options);
202+
expect(result).toEqual({
203+
additions: ["specification/new-service/readme.md", "specification/service/derived.json"],
204+
modifications: [
205+
".github/src/changed-files.js",
206+
"specification/existing-service/main.tsp",
207+
"specification/service/type-changed.json",
208+
],
209+
deletions: ["specification/old-service/contoso.json"],
210+
renames: [
211+
{
212+
from: "specification/service/old-name.json",
213+
to: "specification/service/new-name.json",
214+
},
215+
],
216+
total: 7,
217+
});
218+
expect(simpleGit.simpleGit().diff).toHaveBeenCalledWith(["--name-status", "HEAD^", "HEAD"]);
219+
220+
const specGitOutput = gitOutput
221+
.split("\n")
222+
.filter((f) => f.includes("specification/"))
223+
.join("\n");
224+
vi.mocked(simpleGit.simpleGit().diff).mockResolvedValue(specGitOutput);
225+
result = await getChangedFilesStatuses({ ...options, paths: ["specification"] });
187226
expect(result).toEqual({
188227
additions: ["specification/new-service/readme.md", "specification/service/derived.json"],
189228
modifications: [
@@ -199,6 +238,13 @@ describe("changedFiles", () => {
199238
],
200239
total: 6,
201240
});
241+
expect(simpleGit.simpleGit().diff).toHaveBeenCalledWith([
242+
"--name-status",
243+
"HEAD^",
244+
"HEAD",
245+
"--",
246+
"specification",
247+
]);
202248
},
203249
);
204250

.github/shared/test/swagger.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { dirname, join, resolve } from "path";
44
import { describe, expect, it } from "vitest";
5-
import { Swagger } from "../src/swagger.js";
5+
import { API_VERSION_LIFECYCLE_STAGES, Swagger } from "../src/swagger.js";
66

77
import { fileURLToPath } from "url";
88
import { ConsoleLogger } from "../src/logger.js";
@@ -71,6 +71,14 @@ describe("Swagger", () => {
7171
);
7272
});
7373

74+
it("computes versionKind from path", () => {
75+
let swagger = new Swagger(resolve("foo/preview/2025-01-01-preview/foo.json"));
76+
expect(swagger.versionKind).toEqual(API_VERSION_LIFECYCLE_STAGES.PREVIEW);
77+
78+
swagger = new Swagger(resolve("foo/stable/2025-01-01/foo.json"));
79+
expect(swagger.versionKind).toEqual(API_VERSION_LIFECYCLE_STAGES.STABLE);
80+
});
81+
7482
describe("getOperations", () => {
7583
it("should return normal operations", async () => {
7684
const testFixturePath = join(__dirname, "fixtures", "swagger", "specification");

0 commit comments

Comments
 (0)