Skip to content

Commit

Permalink
feat: add file-coverage-root-path option to customize coverage report…
Browse files Browse the repository at this point in the history
… paths
  • Loading branch information
davelosert committed Nov 17, 2024
1 parent 2ed2c62 commit bee4365
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ This action requires the `pull-request: write` permission to add a comment to yo
| `working-directory` | The main path to search for coverage- and configuration files (adjusting this is especially useful in monorepos). | `./` |
| `json-summary-path` | The path to the json summary file. | `${working-directory}/coverage/coverage-summary.json` |
| `json-final-path` | The path to the json final file. | `${working-directory}/coverage/coverage-final.json` |
| `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 |
| `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` |
| `github-token` | A GitHub access token with permissions to write to issues (defaults to `secrets.GITHUB_TOKEN`). | `${{ github.token }}` |
| `file-coverage-mode` | Defines how file-based coverage is reported. Possible values are `all`, `changes` or `none`. | `changes` |
| `file-coverage-root-path` | The root (or absolute) bit of the path used within the json coverage reports to point to the covered files. You can change this if your reports where 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 }}` |
| `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. | '' |
| `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 |
| `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` |
| `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` |

Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ inputs:
required: false
description: 'How to show summary for files coverage. Uses "changes" by default.'
default: changes
file-coverage-root-path:
required: false
description: 'The root (or absolute) bit of the path used within the json coverage reports to point to the covered files. You can change this if your reports where 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.'
default: ${{ github.workspace }}
working-directory:
required: false
description: 'Custom working directory'
description: 'Working directory where to look for the vite config file and the coverage report files. Uses "./" by default.'
default: ./
name:
required: false
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const run = async () => {
fileCoverageMode: options.fileCoverageMode,
pullChanges,
commitSHA: options.commitSHA,
workspacePath: options.fileCoverageRootPath,
});
summary.addDetails("File Coverage", fileTable);
}
Expand Down
4 changes: 4 additions & 0 deletions src/inputs/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Options = {
prNumber: number | undefined;
commitSHA: string;
commentOn: Array<CommentOn>;
fileCoverageRootPath: string;
};

async function readOptions(octokit: Octokit): Promise<Options> {
Expand Down Expand Up @@ -70,6 +71,8 @@ async function readOptions(octokit: Octokit): Promise<Options> {
prNumber = await getPullRequestNumber(octokit);
}

const fileCoverageRootPath = core.getInput("file-coverage-root-path");

return {
fileCoverageMode,
jsonFinalPath,
Expand All @@ -81,6 +84,7 @@ async function readOptions(octokit: Octokit): Promise<Options> {
prNumber,
commitSHA,
commentOn,
fileCoverageRootPath,
};
}

Expand Down
33 changes: 33 additions & 0 deletions src/report/generateFileCoverageHtml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe("generateFileCoverageHtml()", () => {
fileCoverageMode: FileCoverageMode.All,
pullChanges: [],
commitSHA: "test-sha",
workspacePath: process.cwd(),
});

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

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

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

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

const tableLine = getTableLine(2, html);
Expand Down Expand Up @@ -148,6 +153,7 @@ describe("generateFileCoverageHtml()", () => {
fileCoverageMode: FileCoverageMode.All,
pullChanges: [],
commitSHA: "test-sha",
workspacePath: process.cwd(),
});

const tableLine = getTableLine(2, html);
Expand Down Expand Up @@ -176,6 +182,7 @@ describe("generateFileCoverageHtml()", () => {
fileCoverageMode: FileCoverageMode.All,
pullChanges: [],
commitSHA: "test-sha",
workspacePath: process.cwd(),
});

const tableLine = getTableLine(2, html);
Expand Down Expand Up @@ -207,6 +214,7 @@ describe("generateFileCoverageHtml()", () => {
fileCoverageMode: FileCoverageMode.All,
pullChanges: [],
commitSHA: "test-sha",
workspacePath: process.cwd(),
});

const tableLine = getTableLine(2, html);
Expand Down Expand Up @@ -236,6 +244,7 @@ describe("generateFileCoverageHtml()", () => {
fileCoverageMode: FileCoverageMode.All,
pullChanges: ["file1.ts"],
commitSHA: "test-sha",
workspacePath: process.cwd(),
});

expect(html).toContain("file1.ts");
Expand Down Expand Up @@ -270,6 +279,7 @@ describe("generateFileCoverageHtml()", () => {
fileCoverageMode: FileCoverageMode.All,
pullChanges: ["file1.ts"],
commitSHA: "test-sha",
workspacePath: process.cwd(),
});

expect(html).toContain("file1.ts");
Expand Down Expand Up @@ -304,6 +314,7 @@ describe("generateFileCoverageHtml()", () => {
fileCoverageMode: FileCoverageMode.All,
pullChanges: ["file1.ts"],
commitSHA: "test-sha",
workspacePath: process.cwd(),
});

expect(html).toContain("file1.ts");
Expand All @@ -312,4 +323,26 @@ describe("generateFileCoverageHtml()", () => {
.length;
expect(equalSignCount).toBe(1);
});

it("correctly handles a different workspacePath than the current working directory", () => {
const differentWorkspacePath = "/path/to/different/workspace";
const filePath = path.join(differentWorkspacePath, "src", "exampleFile.ts");
const relativeFilePath = "src/exampleFile.ts";

const jsonSummary: JsonSummary = createMockJsonSummary({
[filePath]: createMockCoverageReport(),
});

const html = generateFileCoverageHtml({
jsonSummary,
jsonSummaryCompare: undefined,
jsonFinal: {},
fileCoverageMode: FileCoverageMode.All,
pullChanges: [relativeFilePath],
commitSHA: "test-sha",
workspacePath: differentWorkspacePath,
});

expect(html).toContain(relativeFilePath);
});
});
9 changes: 7 additions & 2 deletions src/report/generateFileCoverageHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ type FileCoverageInputs = {
fileCoverageMode: FileCoverageMode;
pullChanges: string[];
commitSHA: string;
workspacePath: string;
};

const workspacePath = process.cwd();

const generateFileCoverageHtml = ({
jsonSummary,
jsonSummaryCompare,
jsonFinal,
fileCoverageMode,
pullChanges,
commitSHA,
workspacePath,
}: FileCoverageInputs) => {
const filePaths = Object.keys(jsonSummary).filter((key) => key !== "total");

Expand All @@ -36,6 +36,7 @@ const generateFileCoverageHtml = ({
const [changedFiles, unchangedFiles] = splitFilesByChangeStatus(
filePaths,
pullChanges,
workspacePath,
);

if (
Expand All @@ -56,6 +57,7 @@ const generateFileCoverageHtml = ({
jsonSummaryCompare,
jsonFinal,
commitSHA,
workspacePath,
),
)
.join("")}
Expand All @@ -73,6 +75,7 @@ const generateFileCoverageHtml = ({
undefined,
jsonFinal,
commitSHA,
workspacePath,
),
)
.join("")}
Expand Down Expand Up @@ -104,6 +107,7 @@ function generateRow(
jsonSummaryCompare: JsonSummary | undefined,
jsonFinal: JsonFinal,
commitSHA: string,
workspacePath: string,
): string {
const coverageSummary = jsonSummary[filePath];
const coverageSummaryCompare = jsonSummaryCompare
Expand Down Expand Up @@ -169,6 +173,7 @@ function createRangeURLs(uncoveredLines: LineRange[], url: string): string {
function splitFilesByChangeStatus(
filePaths: string[],
pullChanges: string[],
workspacePath: string,
): [string[], string[]] {
return filePaths.reduce(
([changedFiles, unchangedFiles], filePath) => {
Expand Down

0 comments on commit bee4365

Please sign in to comment.