Skip to content

Commit

Permalink
add basic test coverage for codecov score calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Maj committed May 15, 2024
1 parent 1f6551b commit 27cfeba
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"lint": "eslint .",
"build": "ncc build src/index.js -o dist --license licenses.txt",
"local": "act public --eventpath .github/workflows/local/event.json --secret-file .github/workflows/local/.env --platform ubuntu-latest=node:20-buster",
"test:mocha": "mocha --config .mocharc.json test/*-test.js",
"test:mocha": "mocha --config .mocharc.json test/*-test.js test/**/*-test.js",
"test": "npm run lint && c8 npm run test:mocha"
},
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions src/score_components/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const RETRY_DELAY = 10000;
* @description Compiles the health score components
* @param {import('@actions/core')} core `@actions/core` GitHub Actions core helper utility
* @param {import('@actions/github')} github `@actions/github` GitHub Actions core helper utility
* @returns {number} Number of uncovered lines of code, or 0 in the case of no codecov token specified
* @returns {Promise<number>} Number of uncovered lines of code, or 0 in the case of no codecov token specified
*/
module.exports = async function retrieveCodeCoverage(core, github) {
// See if we can get a coverage overview for this commit from codecov
Expand All @@ -28,7 +28,7 @@ module.exports = async function retrieveCodeCoverage(core, github) {
repo_name: ctx.repo.repo,
commitid: sha,
});
// core.info(`codecov api response: ${JSON.stringify(coverage, null, 2)}`);
core.debug(`codecov api response: ${JSON.stringify(coverage, null, 2)}`);
if (coverage && coverage.data) {
if (coverage.data.totals && coverage.data.totals.misses) {
misses = coverage.data.totals.misses;
Expand Down
50 changes: 50 additions & 0 deletions test/score_components/coverage-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { assert } = require('chai');
const sinon = require('sinon');
const core = require('@actions/core');
const github = require('@actions/github');
const codecov = require('@api/codecov');
const cov = require('../../src/score_components/coverage');

describe('score component: code coverage', () => {
const fakeCore = sinon.stub(core);
const fakeGithub = sinon.stub(github);
const fakeCodecov = sinon.stub(codecov);
const originalGithubContext = fakeGithub.context;
beforeEach(() => {
sinon.reset();
fakeCore.info.callsFake(console.log);
fakeCore.error.callsFake(console.error);
fakeGithub.context = originalGithubContext;
});

it('should export a function', () => {
assert.isFunction(cov);
});
it('should return 0 if no `codecov_token` input provided', async () => {
fakeCore.getInput.withArgs('codecov_token').returns(null);
const res = await cov(fakeCore, fakeGithub);
assert.equal(res, 0);
});
it('should return total number of missed lines from codecov API response', async () => {
fakeCore.getInput.withArgs('codecov_token').returns('abcd1234');
fakeGithub.context = {
eventName: 'pull_request',
payload: {
after: 'abcd1234',
},
repo: {
owner: 'slackapi',
repo: 'slack-health-score',
},
};
fakeCodecov.repos_commits_retrieve.resolves({
data: {
totals: {
misses: 1337,
},
},
});
const res = await cov(fakeCore, fakeGithub);
assert.equal(res, 1337);
});
});

0 comments on commit 27cfeba

Please sign in to comment.