diff --git a/README.md b/README.md index f013113..afb51ee 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml index d3c7f30..fdacb96 100644 --- a/action.yml +++ b/action.yml @@ -50,3 +50,8 @@ inputs: required: false default: true type: boolean + + coverage-pass-score: + description: "Coverage passing percentage" + required: false + default: "90" diff --git a/dist/index.js b/dist/index.js index 6299f2c..5b3b40c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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); @@ -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) { @@ -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; @@ -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); @@ -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 `${fileName}${percent}%${passing}`; }); (0, core_2.debug)(`Coverage at ${totalPercent}%`); diff --git a/src/main.ts b/src/main.ts index ec69b1d..b1a4764 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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); @@ -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; @@ -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) diff --git a/src/scripts/coverage.ts b/src/scripts/coverage.ts index f769ad3..2a4287d 100644 --- a/src/scripts/coverage.ts +++ b/src/scripts/coverage.ts @@ -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"); @@ -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 `${fileName}${percent}%${passing}`; }); debug(`Coverage at ${totalPercent}%`); diff --git a/tests/src/scripts/coverage.test.ts b/tests/src/scripts/coverage.test.ts index a6e63a9..fa81fdc 100644 --- a/tests/src/scripts/coverage.test.ts +++ b/tests/src/scripts/coverage.test.ts @@ -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({ @@ -64,7 +64,8 @@ LF:12 LH:12 end_of_record `), - "coverage" + "coverage", + "90" ); expect(result.output.includes(" (🔻 down from")).toBe(true); @@ -92,7 +93,8 @@ LF:12 LH:0 end_of_record `), - COVERAGE_DIR + COVERAGE_DIR, + "90" ); expect(result.output.includes(" (⬆️ up from")).toBe(true); @@ -120,7 +122,8 @@ LF:12 LH:10 end_of_record `), - COVERAGE_DIR + COVERAGE_DIR, + "90" ); expect(result.output.includes(" (no change)")).toBe(true); @@ -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); @@ -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); } @@ -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("../.."); +});