Skip to content

Commit f4654aa

Browse files
authored
[.github/shared] Add tests for 100% codecov (Azure#35890)
- Fixes Azure#35855
1 parent 9cf18b6 commit f4654aa

File tree

5 files changed

+128
-133
lines changed

5 files changed

+128
-133
lines changed

.github/shared/.prettierignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 71 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -45,102 +45,91 @@ export async function getChangedFiles(options = {}) {
4545
*/
4646
export async function getChangedFilesStatuses(options = {}) {
4747
const { baseCommitish = "HEAD^", cwd, headCommitish = "HEAD", logger } = options;
48-
try {
49-
const result = await simpleGit(cwd).diff(["--name-status", baseCommitish, headCommitish]);
50-
51-
const categorizedFiles = {
52-
additions: /** @type {string[]} */ ([]),
53-
modifications: /** @type {string[]} */ ([]),
54-
deletions: /** @type {string[]} */ ([]),
55-
renames: /** @type {{from: string, to: string}[]} */ ([]),
56-
total: 0,
57-
};
58-
59-
if (result.trim()) {
60-
const lines = result.trim().split("\n");
61-
62-
for (const line of lines) {
63-
const parts = line.split("\t");
64-
const status = parts[0];
65-
66-
switch (status[0]) {
67-
case "A":
68-
categorizedFiles.additions.push(parts[1]);
69-
break;
70-
case "M":
71-
categorizedFiles.modifications.push(parts[1]);
72-
break;
73-
case "D":
74-
categorizedFiles.deletions.push(parts[1]);
75-
break;
76-
case "R":
77-
categorizedFiles.renames.push({
78-
from: parts[1],
79-
to: parts[2],
80-
});
81-
break;
82-
case "C":
83-
categorizedFiles.additions.push(parts[2]);
84-
break;
85-
default:
86-
categorizedFiles.modifications.push(parts[1]);
87-
}
48+
const result = await simpleGit(cwd).diff(["--name-status", baseCommitish, headCommitish]);
49+
50+
const categorizedFiles = {
51+
additions: /** @type {string[]} */ ([]),
52+
modifications: /** @type {string[]} */ ([]),
53+
deletions: /** @type {string[]} */ ([]),
54+
renames: /** @type {{from: string, to: string}[]} */ ([]),
55+
total: 0,
56+
};
57+
58+
if (result.trim()) {
59+
const lines = result.trim().split("\n");
60+
61+
for (const line of lines) {
62+
const parts = line.split("\t");
63+
const status = parts[0];
64+
65+
switch (status[0]) {
66+
case "A":
67+
categorizedFiles.additions.push(parts[1]);
68+
break;
69+
case "M":
70+
categorizedFiles.modifications.push(parts[1]);
71+
break;
72+
case "D":
73+
categorizedFiles.deletions.push(parts[1]);
74+
break;
75+
case "R":
76+
categorizedFiles.renames.push({
77+
from: parts[1],
78+
to: parts[2],
79+
});
80+
break;
81+
case "C":
82+
categorizedFiles.additions.push(parts[2]);
83+
break;
84+
default:
85+
categorizedFiles.modifications.push(parts[1]);
8886
}
89-
90-
categorizedFiles.total =
91-
categorizedFiles.additions.length +
92-
categorizedFiles.modifications.length +
93-
categorizedFiles.deletions.length +
94-
categorizedFiles.renames.length;
9587
}
9688

97-
// Log all changed files by categories
98-
if (logger) {
99-
logger.info("Categorized Changed Files:");
89+
categorizedFiles.total =
90+
categorizedFiles.additions.length +
91+
categorizedFiles.modifications.length +
92+
categorizedFiles.deletions.length +
93+
categorizedFiles.renames.length;
94+
}
10095

101-
if (categorizedFiles.additions.length > 0) {
102-
logger.info(` Additions (${categorizedFiles.additions.length}):`);
103-
for (const file of categorizedFiles.additions) {
104-
logger.info(` + ${file}`);
105-
}
106-
}
96+
// Log all changed files by categories
97+
if (logger) {
98+
logger.info("Categorized Changed Files:");
10799

108-
if (categorizedFiles.modifications.length > 0) {
109-
logger.info(` Modifications (${categorizedFiles.modifications.length}):`);
110-
for (const file of categorizedFiles.modifications) {
111-
logger.info(` M ${file}`);
112-
}
100+
if (categorizedFiles.additions.length > 0) {
101+
logger.info(` Additions (${categorizedFiles.additions.length}):`);
102+
for (const file of categorizedFiles.additions) {
103+
logger.info(` + ${file}`);
113104
}
105+
}
114106

115-
if (categorizedFiles.deletions.length > 0) {
116-
logger.info(` Deletions (${categorizedFiles.deletions.length}):`);
117-
for (const file of categorizedFiles.deletions) {
118-
logger.info(` - ${file}`);
119-
}
107+
if (categorizedFiles.modifications.length > 0) {
108+
logger.info(` Modifications (${categorizedFiles.modifications.length}):`);
109+
for (const file of categorizedFiles.modifications) {
110+
logger.info(` M ${file}`);
120111
}
112+
}
121113

122-
if (categorizedFiles.renames.length > 0) {
123-
logger.info(` Renames (${categorizedFiles.renames.length}):`);
124-
for (const rename of categorizedFiles.renames) {
125-
logger.info(` R ${rename.from} -> ${rename.to}`);
126-
}
114+
if (categorizedFiles.deletions.length > 0) {
115+
logger.info(` Deletions (${categorizedFiles.deletions.length}):`);
116+
for (const file of categorizedFiles.deletions) {
117+
logger.info(` - ${file}`);
127118
}
119+
}
128120

129-
logger.info(` Total: ${categorizedFiles.total} files`);
130-
logger.info("");
121+
if (categorizedFiles.renames.length > 0) {
122+
logger.info(` Renames (${categorizedFiles.renames.length}):`);
123+
for (const rename of categorizedFiles.renames) {
124+
logger.info(` R ${rename.from} -> ${rename.to}`);
125+
}
131126
}
132127

133-
return categorizedFiles;
134-
} catch (error) {
135-
logger?.error(`Error getting categorized changed files: ${error}`);
136-
return {
137-
additions: /** @type {string[]} */ ([]),
138-
modifications: /** @type {string[]} */ ([]),
139-
deletions: /** @type {string[]} */ ([]),
140-
renames: /** @type {{from: string, to: string}[]} */ ([]),
141-
total: 0,
142-
};
128+
logger.info(` Total: ${categorizedFiles.total} files`);
129+
logger.info("");
143130
}
131+
132+
return categorizedFiles;
144133
}
145134

146135
// Functions suitable for passing to string[].filter(), ordered roughly in order of increasing specificity

.github/shared/src/swagger.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,38 +139,40 @@ export class Swagger {
139139
// Process regular paths
140140
if (swagger.paths) {
141141
for (const [path, pathItem] of Object.entries(swagger.paths)) {
142-
for (const [method, operation] of Object.entries(pathItem)) {
143-
if (typeof operation === "object" && operation.operationId && method !== "parameters") {
144-
const operationObj = {
145-
id: operation.operationId,
146-
httpMethod: method.toUpperCase(),
147-
path: path,
148-
};
149-
this.#operations.set(operation.operationId, operationObj);
150-
}
151-
}
142+
this.addOperations(this.#operations, path, pathItem);
152143
}
153144
}
154145

155146
// Process x-ms-paths (Azure extension)
156147
if (swagger["x-ms-paths"]) {
157148
for (const [path, pathItem] of Object.entries(swagger["x-ms-paths"])) {
158-
for (const [method, operation] of Object.entries(pathItem)) {
159-
if (typeof operation === "object" && operation.operationId && method !== "parameters") {
160-
const operationObj = {
161-
id: operation.operationId,
162-
httpMethod: method.toUpperCase(),
163-
path: path,
164-
};
165-
this.#operations.set(operation.operationId, operationObj);
166-
}
167-
}
149+
this.addOperations(this.#operations, path, pathItem);
168150
}
169151
}
170152
}
171153
return this.#operations;
172154
}
173155

156+
/**
157+
*
158+
* @param {Map<string, Operation>} operations
159+
* @param {string} path
160+
* @param {any} pathItem
161+
* @returns {void}
162+
*/
163+
addOperations(operations, path, pathItem) {
164+
for (const [method, operation] of Object.entries(pathItem)) {
165+
if (typeof operation === "object" && operation.operationId && method !== "parameters") {
166+
const operationObj = {
167+
id: operation.operationId,
168+
httpMethod: method.toUpperCase(),
169+
path: path,
170+
};
171+
operations.set(operation.operationId, operationObj);
172+
}
173+
}
174+
}
175+
174176
/**
175177
* @returns {string} absolute path
176178
*/

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

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import {
1717
json,
1818
readme,
1919
resourceManager,
20+
scenario,
2021
specification,
2122
swagger,
22-
scenario,
2323
} from "../src/changed-files.js";
2424
import { debugLogger } from "../src/logger.js";
2525

@@ -134,34 +134,37 @@ describe("changedFiles", () => {
134134
});
135135

136136
describe("getChangedFilesStatuses", () => {
137-
it("should categorize files correctly with all types of changes", async () => {
138-
const gitOutput = [
139-
"A\tspecification/new-service/readme.md",
140-
"M\tspecification/existing-service/main.tsp",
141-
"D\tspecification/old-service/contoso.json",
142-
"R100\tspecification/service/old-name.json\tspecification/service/new-name.json",
143-
"C90\tspecification/template/base.json\tspecification/service/derived.json",
144-
"T\tspecification/service/type-changed.json",
145-
].join("\n");
146-
147-
vi.mocked(simpleGit.simpleGit().diff).mockResolvedValue(gitOutput);
148-
const result = await getChangedFilesStatuses();
149-
expect(result).toEqual({
150-
additions: ["specification/new-service/readme.md", "specification/service/derived.json"],
151-
modifications: [
152-
"specification/existing-service/main.tsp",
153-
"specification/service/type-changed.json",
154-
],
155-
deletions: ["specification/old-service/contoso.json"],
156-
renames: [
157-
{
158-
from: "specification/service/old-name.json",
159-
to: "specification/service/new-name.json",
160-
},
161-
],
162-
total: 6,
163-
});
164-
});
137+
it.each([{}, { logger: debugLogger }])(
138+
"should categorize files correctly with all types of changes (%o)",
139+
async (options) => {
140+
const gitOutput = [
141+
"A\tspecification/new-service/readme.md",
142+
"M\tspecification/existing-service/main.tsp",
143+
"D\tspecification/old-service/contoso.json",
144+
"R100\tspecification/service/old-name.json\tspecification/service/new-name.json",
145+
"C90\tspecification/template/base.json\tspecification/service/derived.json",
146+
"T\tspecification/service/type-changed.json",
147+
].join("\n");
148+
149+
vi.mocked(simpleGit.simpleGit().diff).mockResolvedValue(gitOutput);
150+
const result = await getChangedFilesStatuses(options);
151+
expect(result).toEqual({
152+
additions: ["specification/new-service/readme.md", "specification/service/derived.json"],
153+
modifications: [
154+
"specification/existing-service/main.tsp",
155+
"specification/service/type-changed.json",
156+
],
157+
deletions: ["specification/old-service/contoso.json"],
158+
renames: [
159+
{
160+
from: "specification/service/old-name.json",
161+
to: "specification/service/new-name.json",
162+
},
163+
],
164+
total: 6,
165+
});
166+
},
167+
);
165168

166169
it("should handle empty git output", async () => {
167170
vi.mocked(simpleGit.simpleGit().diff).mockResolvedValue("");

.github/shared/test/fixtures/swagger/specification/servicelinker/resource-manager/Microsoft.ServiceLinker/stable/2024-04-01/test.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
"nextLinkName": "nextLink"
5959
}
6060
}
61-
},
61+
}
62+
},
63+
"x-ms-paths": {
6264
"/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.ServiceLinker/locations/{location}/dryruns/{dryrunName}": {
6365
"get": {
6466
"tags": [

0 commit comments

Comments
 (0)