Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add input to change the desired coverage percentage #37

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
18 changes: 14 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113290,7 +113290,7 @@ const minimist_1 = __importDefault(__nccwpck_require__(13566));
exports.COVERAGE_DIR = ".coverage";
const run = async (isLocal) => {
try {
const workingDirectory = (0, core_1.getInput)("working-directory");
const workingDirectory = isLocal ? "." : (0, core_1.getInput)("working-directory");
// Check if the working directory is different from the current directory
if (workingDirectory && workingDirectory !== process.cwd()) {
process.chdir(workingDirectory);
Expand All @@ -113302,6 +113302,7 @@ const run = async (isLocal) => {
const runPrevCoverage = isLocal ? true : (0, core_1.getBooleanInput)("run-prev-coverage");
const runBehindBy = isLocal ? true : (0, core_1.getBooleanInput)("run-behind-by");
const createComment = isLocal ? true : (0, core_1.getBooleanInput)("create-comment");
const score = isLocal ? "90" : (0, core_1.getInput)("coverage-pass-score");
const octokit = (0, github_1.getOctokit)(token);
let prevCoverage;
if (runPrevCoverage) {
Expand All @@ -113316,7 +113317,9 @@ const run = async (isLocal) => {
await (0, setup_1.setup)();
const analyzeStr = runAnalyze ? await (0, analyze_1.getAnalyze)() : undefined;
const testStr = runTests ? await (0, runTests_1.getTest)(exports.COVERAGE_DIR) : undefined;
const coverageStr = runCoverage ? (0, coverage_1.getCoverage)(prevCoverage, exports.COVERAGE_DIR) : undefined;
const coverageStr = runCoverage
? (0, coverage_1.getCoverage)(prevCoverage, exports.COVERAGE_DIR, score)
: undefined;
const comment = createComment
? (0, comment_1.createComment)(analyzeStr, testStr, coverageStr, behindByStr)
: undefined;
Expand Down Expand Up @@ -113611,9 +113614,16 @@ exports.COV_FAILURE = "⚠️ - Coverage check failed";
* @param coverageDirectory - Directory to store coverage report
* @returns Coverage report as a stepResponse object
*/
const getCoverage = (prevCoverage, coverageDirectory) => {
const getCoverage = (prevCoverage, coverageDirectory, scoreStr) => {
(0, core_1.startGroup)("Checking test coverage");
let response;
let score = 90;
try {
score = parseInt(scoreStr);
}
catch (error) {
console.error("Error parsing score", "Will default to 90", error);
}
try {
const contents = (0, node_fs_1.readFileSync)(`${coverageDirectory}/lcov.info`, "utf8");
const lcov = (0, lcov_utils_1.parse)(contents);
Expand All @@ -113624,7 +113634,7 @@ const getCoverage = (prevCoverage, coverageDirectory) => {
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>`;
});
(0, core_2.debug)(`Coverage at ${totalPercent}%`);
Expand Down
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("../..");
});