Skip to content

Commit

Permalink
fix: Add input to change the desired coverage percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Aug 22, 2024
1 parent 9a2facd commit 4ba5f0d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 20 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ jobs:
## Inputs
| Name | Description | Required | Default |
| ----------------- | ----------------------------------------------------------------- | -------- | ------- |
| token | Token used for pushing fixes and commenting on PRs. | true | |
| run-tests | Whether tests should be run. | false | true |
| run-analysis | Whether static analysis should be run. | false | true |
| run-coverage | Whether code coverage should be run. | false | true |
| run-prev-coverage | Whether code coverage should be compared with the base branch. | false | true |
| run-behind-by | Whether action should check if HEAD branch is behind base branch. | false | true |
| create-comment | Whether the action should comment the output status. | false | true |
| working-directory | Working directory to run the action in | false | "." |
| Name | Description | Required | Default |
| ------------------- | ----------------------------------------------------------------- | -------- | ------- |
| token | Token used for pushing fixes and commenting on PRs. | true | |
| run-tests | Whether tests should be run. | false | true |
| run-analysis | Whether static analysis should be run. | false | true |
| run-coverage | Whether code coverage should be run. | false | true |
| run-prev-coverage | Whether code coverage should be compared with the base branch. | false | true |
| run-behind-by | Whether action should check if HEAD branch is behind base branch. | false | true |
| create-comment | Whether the action should comment the output status. | false | true |
| working-directory | Working directory to run the action in | false | "." |
| coverage-pass-score | Coverage passing percentage | false | "90" |
## Coverage
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ inputs:
required: false
default: true
type: boolean

coverage-pass-score:
description: "Coverage passing percentage"
required: false
default: "90"
7 changes: 5 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const COVERAGE_DIR = ".coverage";

const run = async (isLocal: boolean) => {
try {
const workingDirectory = getInput("working-directory");
const workingDirectory = isLocal ? "." : getInput("working-directory");
// Check if the working directory is different from the current directory
if (workingDirectory && workingDirectory !== process.cwd()) {
process.chdir(workingDirectory);
Expand All @@ -30,6 +30,7 @@ const run = async (isLocal: boolean) => {
const runPrevCoverage = isLocal ? true : getBooleanInput("run-prev-coverage");
const runBehindBy = isLocal ? true : getBooleanInput("run-behind-by");
const createComment = isLocal ? true : getBooleanInput("create-comment");
const score = isLocal ? "90" : getInput("coverage-pass-score");

const octokit = getOctokit(token);
let prevCoverage: Lcov | undefined;
Expand All @@ -46,7 +47,9 @@ const run = async (isLocal: boolean) => {

const analyzeStr: stepResponse | undefined = runAnalyze ? await getAnalyze() : undefined;
const testStr: stepResponse | undefined = runTests ? await getTest(COVERAGE_DIR) : undefined;
const coverageStr: stepResponse | undefined = runCoverage ? getCoverage(prevCoverage, COVERAGE_DIR) : undefined;
const coverageStr: stepResponse | undefined = runCoverage
? getCoverage(prevCoverage, COVERAGE_DIR, score)
: undefined;

const comment: string | undefined = createComment
? getComment(analyzeStr, testStr, coverageStr, behindByStr)
Expand Down
15 changes: 13 additions & 2 deletions src/scripts/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@ export const COV_FAILURE = "⚠️ - Coverage check failed";
* @param coverageDirectory - Directory to store coverage report
* @returns Coverage report as a stepResponse object
*/
export const getCoverage = (prevCoverage: Lcov | undefined, coverageDirectory: string): stepResponse => {
export const getCoverage = (
prevCoverage: Lcov | undefined,
coverageDirectory: string,
scoreStr: string
): stepResponse => {
startGroup("Checking test coverage");
let response: stepResponse | undefined;
let score = 90;

try {
score = parseInt(scoreStr);
} catch (error) {
console.error("Error parsing score", "Will default to 90", error);
}

try {
const contents = readFileSync(`${coverageDirectory}/lcov.info`, "utf8");
Expand All @@ -28,7 +39,7 @@ export const getCoverage = (prevCoverage: Lcov | undefined, coverageDirectory: s
const arr = Object.values(lcov).map((e) => {
const fileName = e.sf;
const percent = Math.round((e.lh / e.lf) * 1000) / 10;
const passing = percent > 96 ? "✅" : "⛔️";
const passing = percent > score ? "✅" : "⛔️";
return `<tr><td>${fileName}</td><td>${percent}%</td><td>${passing}</td></tr>`;
});
debug(`Coverage at ${totalPercent}%`);
Expand Down
46 changes: 40 additions & 6 deletions tests/src/scripts/coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ LF:12
LH:12
end_of_record
`);
const result: stepResponse = getCoverage(oldCoverage, COVERAGE_DIR);
const result: stepResponse = getCoverage(oldCoverage, COVERAGE_DIR, "90");

expect(result).toEqual(
expect.objectContaining({
Expand Down Expand Up @@ -64,7 +64,8 @@ LF:12
LH:12
end_of_record
`),
"coverage"
"coverage",
"90"
);

expect(result.output.includes(" (🔻 down from")).toBe(true);
Expand Down Expand Up @@ -92,7 +93,8 @@ LF:12
LH:0
end_of_record
`),
COVERAGE_DIR
COVERAGE_DIR,
"90"
);
expect(result.output.includes(" (⬆️ up from")).toBe(true);

Expand Down Expand Up @@ -120,7 +122,8 @@ LF:12
LH:10
end_of_record
`),
COVERAGE_DIR
COVERAGE_DIR,
"90"
);
expect(result.output.includes(" (no change)")).toBe(true);

Expand All @@ -130,7 +133,7 @@ end_of_record
test("no old coverage", () => {
process.chdir("tests/pass_repo");

const result: stepResponse = getCoverage(undefined, COVERAGE_DIR);
const result: stepResponse = getCoverage(undefined, COVERAGE_DIR, "90");
expect(result.output.includes(" (🔻 down from 95%)")).toBe(false);
expect(result.output.includes(" (⬆️ up from 5%)")).toBe(false);
expect(result.output.includes(" (no change)")).toBe(false);
Expand All @@ -141,7 +144,7 @@ test("no old coverage", () => {
test("fail", () => {
process.chdir("tests/fail_repo");
try {
const result: stepResponse = getCoverage(undefined, COVERAGE_DIR);
const result: stepResponse = getCoverage(undefined, COVERAGE_DIR, "90");
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
Expand All @@ -155,3 +158,34 @@ test("oldCoverage pass", () => {
expect(result).toEqual(83.33);
process.chdir("../..");
});
test("differet coverage scores", () => {
process.chdir("tests/pass_repo");

const testLcov = `SF:lib/main.dart
DA:3,1
DA:8,1
DA:10,1
DA:17,1
DA:21,1
DA:22,1
DA:28,1
DA:30,1
DA:32,1
DA:33,1
DA:34,1
DA:35,1
LF:12
LH:12
end_of_record
`;

const result: stepResponse = getCoverage(parse(testLcov), COVERAGE_DIR, "50");

expect(result.output.includes("✅")).toBe(true);

const result2: stepResponse = getCoverage(parse(testLcov), COVERAGE_DIR, "99");

expect(result2.output.includes("✅")).toBe(false);

process.chdir("../..");
});

0 comments on commit 4ba5f0d

Please sign in to comment.