diff --git a/__tests__/__snapshots__/functions.test.ts.snap b/__tests__/__snapshots__/functions.test.ts.snap index fc54fbf..c64223a 100644 --- a/__tests__/__snapshots__/functions.test.ts.snap +++ b/__tests__/__snapshots__/functions.test.ts.snap @@ -266,3 +266,29 @@ exports[`Generate Diffed Cobertura Markdown 2`] = ` _Minimum allowed coverage is_ \`0%\`_, this run produced_ \`49.83%\`" `; + +exports[`Only list changed files 1`] = ` +[ + "::debug::headCoverage: 49\\n", + "::debug::baseCoverage: 50.51\\n", + "::debug::overallDifferencePercentage: -1.51\\n", + "::debug::negativeDifferenceThreshold: 0\\n", + "Writing results to code-coverage-results.md\\n", + "\\n", + "::set-output name=file::code-coverage-results.md\\n", + "\\n", + "::set-output name=coverage::49\\n", + "Writing job summary\\n", +] +`; + +exports[`Only list changed files 2`] = ` +"# Code Coverage Report: Only Changed Files listed + +| Package | Base Coverage | New Coverage | Difference | +| ------- | ------------- | ------------ | ---------- | +| utils.ts | 🟠 70.94% | 🟠 69% | 🔴 -1.94% | +| **Overall Coverage** | **🟢 50.51%** | **🟢 49%** | **🔴 -1.51%** | + +_Minimum allowed coverage is_ \`0%\`_, this run produced_ \`49%\`" +`; diff --git a/__tests__/functions.test.ts b/__tests__/functions.test.ts index 8c44354..2552607 100644 --- a/__tests__/functions.test.ts +++ b/__tests__/functions.test.ts @@ -160,6 +160,21 @@ test('Fail if negative_difference_threshold is set and exceeded', async () => { expect(await getGithubStepSummary()).toMatchSnapshot() }) +test('Only list changed files', async () => { + process.env.INPUT_ONLY_LIST_CHANGED_FILES = 'true' + + const coverage = await loadJSONFixture('clover-parsed.json') + const coverageFail = JSON.parse(JSON.stringify(coverage)) + coverageFail.files[ + '7583809507a13391057c3aee722e422d50d961a87e2a3dbf05ea492dc6465c94' + ].coverage = 69 + coverageFail.coverage = 49 + + await generateMarkdown(coverageFail, coverage) + expect(getStdoutWriteCalls()).toMatchSnapshot() + expect(await getGithubStepSummary()).toMatchSnapshot() +}) + async function getGithubStepSummary(): Promise { const tempFileName = process.env.GITHUB_STEP_SUMMARY as string return fs.promises.readFile(tempFileName, 'utf8') diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 9748a44..1241de3 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -147,6 +147,7 @@ test('getInputs', () => { negativeDifferenceBy: 'package', negativeDifferenceThreshold: -0, retention: undefined, + onlyListChangedFiles: false, //This is a cheat withBaseCoverageTemplate: f.withBaseCoverageTemplate, withoutBaseCoverageTemplate: f.withoutBaseCoverageTemplate diff --git a/action.yml b/action.yml index 78844ff..0fceb4e 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,10 @@ inputs: description: 'GitHub token. This is sometimes needed on private repositories' required: false default: ${{ github.token }} + only_list_changed_files: + description: 'Only list changed files' + required: false + default: 'false' markdown_filename: description: 'The filename (without extension) for the resulting markdown file. Default is code-coverage-results.' required: false diff --git a/src/functions.ts b/src/functions.ts index d98da43..671b684 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -108,6 +108,7 @@ export async function generateMarkdown( headCoverage: Coverage, baseCoverage: Coverage | null = null ): Promise { + const inputs = getInputs() const { overallCoverageFailThreshold, failOnNegativeDifference, @@ -118,8 +119,9 @@ export async function generateMarkdown( negativeDifferenceBy, withBaseCoverageTemplate, withoutBaseCoverageTemplate, - negativeDifferenceThreshold - } = getInputs() + negativeDifferenceThreshold, + onlyListChangedFiles + } = inputs const overallDifferencePercentage = baseCoverage ? roundPercentage(headCoverage.coverage - baseCoverage.coverage) : null @@ -179,6 +181,25 @@ export async function generateMarkdown( minimum_allowed_coverage: `${overallCoverageFailThreshold}%`, new_coverage: `${headCoverage.coverage}%`, coverage: Object.entries(headCoverage.files) + .filter(([hash, file]) => { + if (baseCoverage === null) { + return !onlyListChangedFiles + } + + const baseCoveragePercentage = baseCoverage.files[hash] + ? baseCoverage.files[hash].coverage + : 0 + + const differencePercentage = baseCoveragePercentage + ? roundPercentage(file.coverage - baseCoveragePercentage) + : roundPercentage(file.coverage) + + if (onlyListChangedFiles && differencePercentage === 0) { + return false + } + + return true + }) .map(([hash, file]) => { if (baseCoverage === null) { return { @@ -229,7 +250,8 @@ export async function generateMarkdown( .sort((a, b) => a.package < b.package ? -1 : a.package > b.package ? 1 : 0 ), - overall_coverage: addOverallRow(headCoverage, baseCoverage) + overall_coverage: addOverallRow(headCoverage, baseCoverage), + inputs } if (badge) { diff --git a/src/interfaces.ts b/src/interfaces.ts index 45b2f27..d701a58 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -27,6 +27,7 @@ export interface Inputs { withBaseCoverageTemplate: string withoutBaseCoverageTemplate: string negativeDifferenceThreshold: number + onlyListChangedFiles: boolean } export interface Files { @@ -46,4 +47,5 @@ export interface HandlebarContext { new_coverage?: string coverage: HandlebarContextCoverage[] overall_coverage: HandlebarContextCoverage + inputs: Inputs } diff --git a/src/utils.ts b/src/utils.ts index 448f359..acd5964 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -347,6 +347,9 @@ export function getInputs(): Inputs { const failOnNegativeDifference = core.getInput('fail_on_negative_difference') === 'true' ? true : false + const onlyListChangedFiles = + core.getInput('only_list_changed_files') === 'true' ? true : false + const negativeDifferenceBy = core.getInput('negative_difference_by') === 'overall' ? 'overall' @@ -393,7 +396,8 @@ export function getInputs(): Inputs { retention: retentionDays, withoutBaseCoverageTemplate, withBaseCoverageTemplate, - negativeDifferenceThreshold + negativeDifferenceThreshold, + onlyListChangedFiles } } diff --git a/templates/with-base-coverage.hbs b/templates/with-base-coverage.hbs index 8f029f9..4961bd0 100644 --- a/templates/with-base-coverage.hbs +++ b/templates/with-base-coverage.hbs @@ -1,4 +1,4 @@ -# Code Coverage Report +# Code Coverage Report{{#if inputs.onlyListChangedFiles}}: Only Changed Files listed{{/if}} {{#if coverage_badge}} ![Code Coverage]({{{coverage_badge}}}) diff --git a/templates/without-base-coverage.hbs b/templates/without-base-coverage.hbs index 2cdf8b7..3889f28 100644 --- a/templates/without-base-coverage.hbs +++ b/templates/without-base-coverage.hbs @@ -1,4 +1,4 @@ -# Code Coverage Report +# Code Coverage Report{{#if inputs.onlyListChangedFiles}}: Only Changed Files listed{{/if}} {{#if coverage_badge}} ![Code Coverage]({{{coverage_badge}}})