diff --git a/README.md b/README.md index bceff3f..e8bf346 100644 --- a/README.md +++ b/README.md @@ -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.
If in the context of a triggered workflow, the PR of the triggering workflow.
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` | diff --git a/action.yml b/action.yml index f002556..84d0bf3 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/index.ts b/src/index.ts index a92ee6d..3499e05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -65,6 +65,7 @@ const run = async () => { fileCoverageMode: options.fileCoverageMode, pullChanges, commitSHA: options.commitSHA, + workspacePath: options.fileCoverageRootPath, }); summary.addDetails("File Coverage", fileTable); } diff --git a/src/inputs/options.ts b/src/inputs/options.ts index e1aa355..ef3ff58 100644 --- a/src/inputs/options.ts +++ b/src/inputs/options.ts @@ -20,6 +20,7 @@ type Options = { prNumber: number | undefined; commitSHA: string; commentOn: Array; + fileCoverageRootPath: string; }; async function readOptions(octokit: Octokit): Promise { @@ -70,6 +71,8 @@ async function readOptions(octokit: Octokit): Promise { prNumber = await getPullRequestNumber(octokit); } + const fileCoverageRootPath = core.getInput("file-coverage-root-path"); + return { fileCoverageMode, jsonFinalPath, @@ -81,6 +84,7 @@ async function readOptions(octokit: Octokit): Promise { prNumber, commitSHA, commentOn, + fileCoverageRootPath, }; } diff --git a/src/report/generateFileCoverageHtml.test.ts b/src/report/generateFileCoverageHtml.test.ts index a69add2..fee6c3e 100644 --- a/src/report/generateFileCoverageHtml.test.ts +++ b/src/report/generateFileCoverageHtml.test.ts @@ -36,6 +36,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.All, pullChanges: [], commitSHA: "test-sha", + workspacePath: process.cwd(), }); const firstTableLine = getTableLine(1, html); @@ -58,6 +59,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.All, pullChanges: [relativeChangedFilePath], commitSHA: "test-sha", + workspacePath: process.cwd(), }); expect(getTableLine(1, html)).toContain("Changed Files"); @@ -79,6 +81,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.All, pullChanges: [changedFileName], commitSHA: "test-sha", + workspacePath: process.cwd(), }); expect(html).not.toContain("Unchanged Files"); @@ -96,6 +99,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.Changes, pullChanges: [], commitSHA: "test-sha", + workspacePath: process.cwd(), }); expect(html).toContain("No changed files found."); @@ -118,6 +122,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.All, pullChanges: [], commitSHA: "test-sha", + workspacePath: process.cwd(), }); const tableLine = getTableLine(2, html); @@ -148,6 +153,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.All, pullChanges: [], commitSHA: "test-sha", + workspacePath: process.cwd(), }); const tableLine = getTableLine(2, html); @@ -176,6 +182,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.All, pullChanges: [], commitSHA: "test-sha", + workspacePath: process.cwd(), }); const tableLine = getTableLine(2, html); @@ -207,6 +214,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.All, pullChanges: [], commitSHA: "test-sha", + workspacePath: process.cwd(), }); const tableLine = getTableLine(2, html); @@ -236,6 +244,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.All, pullChanges: ["file1.ts"], commitSHA: "test-sha", + workspacePath: process.cwd(), }); expect(html).toContain("file1.ts"); @@ -270,6 +279,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.All, pullChanges: ["file1.ts"], commitSHA: "test-sha", + workspacePath: process.cwd(), }); expect(html).toContain("file1.ts"); @@ -304,6 +314,7 @@ describe("generateFileCoverageHtml()", () => { fileCoverageMode: FileCoverageMode.All, pullChanges: ["file1.ts"], commitSHA: "test-sha", + workspacePath: process.cwd(), }); expect(html).toContain("file1.ts"); @@ -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); + }); }); diff --git a/src/report/generateFileCoverageHtml.ts b/src/report/generateFileCoverageHtml.ts index 12824e3..58d060d 100644 --- a/src/report/generateFileCoverageHtml.ts +++ b/src/report/generateFileCoverageHtml.ts @@ -17,10 +17,9 @@ type FileCoverageInputs = { fileCoverageMode: FileCoverageMode; pullChanges: string[]; commitSHA: string; + workspacePath: string; }; -const workspacePath = process.cwd(); - const generateFileCoverageHtml = ({ jsonSummary, jsonSummaryCompare, @@ -28,6 +27,7 @@ const generateFileCoverageHtml = ({ fileCoverageMode, pullChanges, commitSHA, + workspacePath, }: FileCoverageInputs) => { const filePaths = Object.keys(jsonSummary).filter((key) => key !== "total"); @@ -36,6 +36,7 @@ const generateFileCoverageHtml = ({ const [changedFiles, unchangedFiles] = splitFilesByChangeStatus( filePaths, pullChanges, + workspacePath, ); if ( @@ -56,6 +57,7 @@ const generateFileCoverageHtml = ({ jsonSummaryCompare, jsonFinal, commitSHA, + workspacePath, ), ) .join("")} @@ -73,6 +75,7 @@ const generateFileCoverageHtml = ({ undefined, jsonFinal, commitSHA, + workspacePath, ), ) .join("")} @@ -104,6 +107,7 @@ function generateRow( jsonSummaryCompare: JsonSummary | undefined, jsonFinal: JsonFinal, commitSHA: string, + workspacePath: string, ): string { const coverageSummary = jsonSummary[filePath]; const coverageSummaryCompare = jsonSummaryCompare @@ -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) => {