Skip to content

Commit 41d0c9e

Browse files
authoredNov 17, 2024··
feat: Add file-coverage-root-path option to customize filer coverage report paths (#444)
* feat: add file-coverage-root-path option to customize coverage report paths * fix: adds missing "'" in yml definition * fix: fixes action yml syntax again, escpaing single quotes * fix: correct default value syntax in action.yml for file-coverage-root-path
1 parent 2ed2c62 commit 41d0c9e

6 files changed

+52
-4
lines changed
 

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,12 @@ This action requires the `pull-request: write` permission to add a comment to yo
8282
| `working-directory` | The main path to search for coverage- and configuration files (adjusting this is especially useful in monorepos). | `./` |
8383
| `json-summary-path` | The path to the json summary file. | `${working-directory}/coverage/coverage-summary.json` |
8484
| `json-final-path` | The path to the json final file. | `${working-directory}/coverage/coverage-final.json` |
85+
| `json-summary-compare-path` | The path to the json summary file to compare against. If given, will display a trend indicator and the difference in the summary. Respects the `working-directory` option. | undefined |
8586
| `vite-config-path` | The path to the vite config file. Will check the same paths as vite and vitest | Checks pattern `${working-directory}/vite[st].config.{t\|mt\|ct\|j\|mj\|cj}s` |
8687
| `github-token` | A GitHub access token with permissions to write to issues (defaults to `secrets.GITHUB_TOKEN`). | `${{ github.token }}` |
8788
| `file-coverage-mode` | Defines how file-based coverage is reported. Possible values are `all`, `changes` or `none`. | `changes` |
89+
| `file-coverage-root-path` | The root (or absolute) part of the path used within the json coverage reports to point to the covered files. You can change this if your reports were generated in a different context (e.g., a docker container) and the absolute paths don't match the current runner's workspace. Uses the runner's workspace path by default. | `${{ github.workspace }}` |
8890
| `name` | Give the report a custom name. This is useful if you want multiple reports for different test suites within the same PR. Needs to be unique. | '' |
89-
| `json-summary-compare-path` | The path to the json summary file to compare against. If given, will display a trend indicator and the difference in the summary. Respects the `working-directory` option. | undefined |
9091
| `pr-number` | The number of the PR to post a comment to. When using the `push` trigger, you can set this option to "auto" to make the action automaticaly search of a PR with a matching `sha` value and comment on it. | If in the context of a PR, the number of that PR.<br/> If in the context of a triggered workflow, the PR of the triggering workflow. <br/>If no PR context is found, it defaults to `undefined` |
9192
| `comment-on` | Specify where you want a comment to appear: "pr" for pull-request (if one can be found), "commit" for the commit in which context the action was run, or "none" for no comments. You can provide a comma-separated list of "pr" and "commit" to comment on both. | `pr` |
9293

‎action.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ inputs:
2424
required: false
2525
description: 'How to show summary for files coverage. Uses "changes" by default.'
2626
default: changes
27+
file-coverage-root-path:
28+
required: false
29+
description: 'The root (or absolute) part of the path used within the json coverage reports to point to the covered files. You can change this if your reports were generated in a different context (e.g., a docker container) and the absolute paths don''t match the current runner''s workspace. Uses the runner''s workspace path by default.'
30+
default: ${{ github.workspace }}
2731
working-directory:
2832
required: false
29-
description: 'Custom working directory'
33+
description: 'Working directory where to look for the vite config file and the coverage report files. Uses "./" by default.'
3034
default: ./
3135
name:
3236
required: false

‎src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const run = async () => {
6565
fileCoverageMode: options.fileCoverageMode,
6666
pullChanges,
6767
commitSHA: options.commitSHA,
68+
workspacePath: options.fileCoverageRootPath,
6869
});
6970
summary.addDetails("File Coverage", fileTable);
7071
}

‎src/inputs/options.ts

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Options = {
2020
prNumber: number | undefined;
2121
commitSHA: string;
2222
commentOn: Array<CommentOn>;
23+
fileCoverageRootPath: string;
2324
};
2425

2526
async function readOptions(octokit: Octokit): Promise<Options> {
@@ -70,6 +71,8 @@ async function readOptions(octokit: Octokit): Promise<Options> {
7071
prNumber = await getPullRequestNumber(octokit);
7172
}
7273

74+
const fileCoverageRootPath = core.getInput("file-coverage-root-path");
75+
7376
return {
7477
fileCoverageMode,
7578
jsonFinalPath,
@@ -81,6 +84,7 @@ async function readOptions(octokit: Octokit): Promise<Options> {
8184
prNumber,
8285
commitSHA,
8386
commentOn,
87+
fileCoverageRootPath,
8488
};
8589
}
8690

‎src/report/generateFileCoverageHtml.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe("generateFileCoverageHtml()", () => {
3636
fileCoverageMode: FileCoverageMode.All,
3737
pullChanges: [],
3838
commitSHA: "test-sha",
39+
workspacePath: process.cwd(),
3940
});
4041

4142
const firstTableLine = getTableLine(1, html);
@@ -58,6 +59,7 @@ describe("generateFileCoverageHtml()", () => {
5859
fileCoverageMode: FileCoverageMode.All,
5960
pullChanges: [relativeChangedFilePath],
6061
commitSHA: "test-sha",
62+
workspacePath: process.cwd(),
6163
});
6264

6365
expect(getTableLine(1, html)).toContain("Changed Files");
@@ -79,6 +81,7 @@ describe("generateFileCoverageHtml()", () => {
7981
fileCoverageMode: FileCoverageMode.All,
8082
pullChanges: [changedFileName],
8183
commitSHA: "test-sha",
84+
workspacePath: process.cwd(),
8285
});
8386

8487
expect(html).not.toContain("Unchanged Files");
@@ -96,6 +99,7 @@ describe("generateFileCoverageHtml()", () => {
9699
fileCoverageMode: FileCoverageMode.Changes,
97100
pullChanges: [],
98101
commitSHA: "test-sha",
102+
workspacePath: process.cwd(),
99103
});
100104

101105
expect(html).toContain("No changed files found.");
@@ -118,6 +122,7 @@ describe("generateFileCoverageHtml()", () => {
118122
fileCoverageMode: FileCoverageMode.All,
119123
pullChanges: [],
120124
commitSHA: "test-sha",
125+
workspacePath: process.cwd(),
121126
});
122127

123128
const tableLine = getTableLine(2, html);
@@ -148,6 +153,7 @@ describe("generateFileCoverageHtml()", () => {
148153
fileCoverageMode: FileCoverageMode.All,
149154
pullChanges: [],
150155
commitSHA: "test-sha",
156+
workspacePath: process.cwd(),
151157
});
152158

153159
const tableLine = getTableLine(2, html);
@@ -176,6 +182,7 @@ describe("generateFileCoverageHtml()", () => {
176182
fileCoverageMode: FileCoverageMode.All,
177183
pullChanges: [],
178184
commitSHA: "test-sha",
185+
workspacePath: process.cwd(),
179186
});
180187

181188
const tableLine = getTableLine(2, html);
@@ -207,6 +214,7 @@ describe("generateFileCoverageHtml()", () => {
207214
fileCoverageMode: FileCoverageMode.All,
208215
pullChanges: [],
209216
commitSHA: "test-sha",
217+
workspacePath: process.cwd(),
210218
});
211219

212220
const tableLine = getTableLine(2, html);
@@ -236,6 +244,7 @@ describe("generateFileCoverageHtml()", () => {
236244
fileCoverageMode: FileCoverageMode.All,
237245
pullChanges: ["file1.ts"],
238246
commitSHA: "test-sha",
247+
workspacePath: process.cwd(),
239248
});
240249

241250
expect(html).toContain("file1.ts");
@@ -270,6 +279,7 @@ describe("generateFileCoverageHtml()", () => {
270279
fileCoverageMode: FileCoverageMode.All,
271280
pullChanges: ["file1.ts"],
272281
commitSHA: "test-sha",
282+
workspacePath: process.cwd(),
273283
});
274284

275285
expect(html).toContain("file1.ts");
@@ -304,6 +314,7 @@ describe("generateFileCoverageHtml()", () => {
304314
fileCoverageMode: FileCoverageMode.All,
305315
pullChanges: ["file1.ts"],
306316
commitSHA: "test-sha",
317+
workspacePath: process.cwd(),
307318
});
308319

309320
expect(html).toContain("file1.ts");
@@ -312,4 +323,26 @@ describe("generateFileCoverageHtml()", () => {
312323
.length;
313324
expect(equalSignCount).toBe(1);
314325
});
326+
327+
it("correctly handles a different workspacePath than the current working directory", () => {
328+
const differentWorkspacePath = "/path/to/different/workspace";
329+
const filePath = path.join(differentWorkspacePath, "src", "exampleFile.ts");
330+
const relativeFilePath = "src/exampleFile.ts";
331+
332+
const jsonSummary: JsonSummary = createMockJsonSummary({
333+
[filePath]: createMockCoverageReport(),
334+
});
335+
336+
const html = generateFileCoverageHtml({
337+
jsonSummary,
338+
jsonSummaryCompare: undefined,
339+
jsonFinal: {},
340+
fileCoverageMode: FileCoverageMode.All,
341+
pullChanges: [relativeFilePath],
342+
commitSHA: "test-sha",
343+
workspacePath: differentWorkspacePath,
344+
});
345+
346+
expect(html).toContain(relativeFilePath);
347+
});
315348
});

‎src/report/generateFileCoverageHtml.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ type FileCoverageInputs = {
1717
fileCoverageMode: FileCoverageMode;
1818
pullChanges: string[];
1919
commitSHA: string;
20+
workspacePath: string;
2021
};
2122

22-
const workspacePath = process.cwd();
23-
2423
const generateFileCoverageHtml = ({
2524
jsonSummary,
2625
jsonSummaryCompare,
2726
jsonFinal,
2827
fileCoverageMode,
2928
pullChanges,
3029
commitSHA,
30+
workspacePath,
3131
}: FileCoverageInputs) => {
3232
const filePaths = Object.keys(jsonSummary).filter((key) => key !== "total");
3333

@@ -36,6 +36,7 @@ const generateFileCoverageHtml = ({
3636
const [changedFiles, unchangedFiles] = splitFilesByChangeStatus(
3737
filePaths,
3838
pullChanges,
39+
workspacePath,
3940
);
4041

4142
if (
@@ -56,6 +57,7 @@ const generateFileCoverageHtml = ({
5657
jsonSummaryCompare,
5758
jsonFinal,
5859
commitSHA,
60+
workspacePath,
5961
),
6062
)
6163
.join("")}
@@ -73,6 +75,7 @@ const generateFileCoverageHtml = ({
7375
undefined,
7476
jsonFinal,
7577
commitSHA,
78+
workspacePath,
7679
),
7780
)
7881
.join("")}
@@ -104,6 +107,7 @@ function generateRow(
104107
jsonSummaryCompare: JsonSummary | undefined,
105108
jsonFinal: JsonFinal,
106109
commitSHA: string,
110+
workspacePath: string,
107111
): string {
108112
const coverageSummary = jsonSummary[filePath];
109113
const coverageSummaryCompare = jsonSummaryCompare
@@ -169,6 +173,7 @@ function createRangeURLs(uncoveredLines: LineRange[], url: string): string {
169173
function splitFilesByChangeStatus(
170174
filePaths: string[],
171175
pullChanges: string[],
176+
workspacePath: string,
172177
): [string[], string[]] {
173178
return filePaths.reduce(
174179
([changedFiles, unchangedFiles], filePath) => {

0 commit comments

Comments
 (0)
Please sign in to comment.