Skip to content

Commit

Permalink
Merge pull request #919 from koopjs/coverage-changes
Browse files Browse the repository at this point in the history
chore: report coverage changes
  • Loading branch information
rgwozdz authored Feb 24, 2024
2 parents c44c46d + 212d7ca commit b8c7eed
Show file tree
Hide file tree
Showing 21 changed files with 653 additions and 65 deletions.
145 changes: 145 additions & 0 deletions .coverage_json/coverage-summary.json

Large diffs are not rendered by default.

20 changes: 9 additions & 11 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
name: CI tests
name: Master CI Tests

on:
push:
branches: [ master ]
pull_request:
branches:
- master
- beta
paths:
- "./.github/**.yml"
- "**/packages/**.js"
- "**/packages/**/package.json"
- "test/**/*.js"

jobs:
pr-tests:
Expand All @@ -24,7 +15,14 @@ jobs:
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Checkout actions
uses: actions/checkout@v3
with:
fetch-depth: 0
- run: |
git checkout -b master origin/master
git checkout -
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
Expand Down
72 changes: 72 additions & 0 deletions .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: PR CI

on:
pull_request:
branches:
- master
- beta
paths:
- "./.github/**.yml"
- "**/packages/**.js"
- "**/packages/**/package.json"
- "test/**/*.js"

jobs:
pr-tests:
name: Install, lint, test
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [18.x]
os: [ubuntu-latest, windows-latest]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- name: Checkout actions
uses: actions/checkout@v3
with:
fetch-depth: 0
- run: |
git checkout -b master origin/master
git checkout -
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install npm 7
run: npm i -g npm@7 --registry=https://registry.npmjs.org

- name: Install
run: npm ci

- name: Lint
run: npm run lint:ci

- name: Unit tests
run: npm test

- name: E2E tests
run: npm run test:e2e

- name: Run Coverage
if: ${{ matrix.os == 'ubuntu-latest' }}
run: npm run cov:changes:json && npm run cov:changes:2:md

- name: Find previous coverage comment
if: ${{ matrix.os == 'ubuntu-latest' }}
uses: peter-evans/find-comment@v1
id: findcomment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: Coverage Report

- name: Create or update comment
if: ${{ matrix.os == 'ubuntu-latest' }}
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.findcomment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body-path: .branch-coverage-changes.md
edit-mode: replace
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Update coverage badges and report
run: npm run cov:unit:json

- name: Commit version and changelog updates
run: "git add --all && git commit -m 'chore: version and changelog' --no-verify"

Expand Down
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
node_modules
coverage
coverage_unit
.nyc_output
.nyc_output_unit
.coverage_html
.coverage_changes_json
.coverage
.vscode
**/.DS_Store
.env
.npmrc
.npmrc
.branch-coverage-changes.md
5 changes: 5 additions & 0 deletions ci/clean-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const shell = require('shelljs');

shell.rm('-rf', './**/.coverage');
shell.rm('-rf', '.coverage');
shell.rm('.branch-coverage-changes.md');
74 changes: 74 additions & 0 deletions ci/format-branch-coverage-changes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { writeFileSync, existsSync } = require('fs');
const json2md = require('json2md');
const coverageSummary = require('../.coverage_json/coverage-summary.json');
const markdownFilePath = '.branch-coverage-changes.md';

if (!existsSync('../.coverage_changes_json/coverage-summary.json')) {
writeFileSync(markdownFilePath, `## Coverage Report (change vs master)
No changes.
`, 'utf8');
return;
}

const coverageChangesSummary = require('../.coverage_changes_json/coverage-summary.json');
const rows = Object.entries(coverageChangesSummary)
.filter(([filePath]) => {
return filePath !== 'total';
})
.map(([filePath, changesCoverage]) => {
const masterCoverage = coverageSummary[filePath];
const packagePath = filePath.split('packages')[1];
return [
`packages${packagePath}`,
formatCovComparison(changesCoverage.statements.pct, masterCoverage.statements.pct),
formatCovComparison(changesCoverage.branches.pct, masterCoverage.branches.pct),
formatCovComparison(changesCoverage.functions.pct, masterCoverage.functions.pct),
formatCovComparison(changesCoverage.lines.pct, masterCoverage.lines.pct)
];
});

const headers = ['File Path', 'Statements', 'Branches', 'Functions', 'Lines'];

const table = json2md([
{ h2: 'Coverage Report (change vs master)' },
{ table: { headers, rows } },
]);

const alignedTable = table.replace(
'| --------- | ---------- | -------- | --------- | ----- |',
'| :--------- | ----------: | --------: | ---------: | -----: |',
);

const markdown = `[g-img]: https://github.com/koopjs/koop/assets/4369192/fd82d4b7-7f6e-448c-a56c-82ac6781a629
[yg-img]: https://github.com/koopjs/koop/assets/4369192/683b2df8-7379-4e4f-bb36-f5e20b2631d6
[y-img]: https://github.com/koopjs/koop/assets/4369192/d5214a5c-c5a9-4449-82ca-8a4e922ef9ef
[o-img]: https://github.com/koopjs/koop/assets/4369192/8651f10c-e986-491d-8b51-bc559aac88a2
[r-img]: https://github.com/koopjs/koop/assets/4369192/a37a56ac-228d-40d9-8ebc-804dbbf08355
${alignedTable}`;

writeFileSync(markdownFilePath, markdown, 'utf8');

function formatCovComparison(changePct, mainPct) {
return `${formatCovPct(changePct)} vs ${formatCovPct(mainPct)}`;
}

function formatCovPct(pct) {
if (pct === 100) {
return `${pct} ![green][g-img]`;
}

if (pct > 90) {
return `${pct} ![yellowGreen][yg-img]`;
}

if (pct > 80) {
return `${pct} ![yellow][y-img]`;
}

if (pct > 70) {
return `${pct} ![orange][o-img]`;
}

return `${pct} ![red][r-img]`;
}
35 changes: 35 additions & 0 deletions ci/merge-coverage-changes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { workspaces } = require('../package.json');
const shell = require('shelljs');
const fs = require('fs');
const path = require('path');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const argv = yargs(hideBin(process.argv)).argv;
const context = argv.context || 'all';

workspaces.forEach((workspace) => {
process.chdir(workspace);
const package = workspace.split(path.sep).pop();

const coverageDir = path.join('.coverage', context);

if (!fs.existsSync(coverageDir)) {
process.chdir('../..');
return;
}

console.log(`Package "${package}":`);
process.stdout.write(` - merging ${context} test coverage results...`);
shell.exec(getCmd(package, coverageDir));
process.stdout.write('completed.\n\n');
process.chdir('../..');
});

function getCmd(package, coverageDir) {
const destination = path.join('..', '..', coverageDir);
if(package === 'output-geoservices') {
return `cp ${coverageDir}/coverage-final.json ${destination}/output-geoservices.json > /dev/null`;
}

return `npx nyc merge ${coverageDir}/analysis ${destination}/${package}.json > /dev/null`;
}
56 changes: 56 additions & 0 deletions ci/run-coverage-on-branch-changes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const shell = require('shelljs');
const git = require('simple-git');
const path = require('path');

const { workspaces } = require('../package.json');

async function execute () {
for (let i = 0; i < workspaces.length; i++) {
const workspace = workspaces[i];
process.chdir(workspace);
const package = workspace.split(path.sep).pop();

const {files} = await git().diffSummary(['--name-only', '--relative', 'origin/master']);

const srcFiles = files.filter(({ file }) => {
return file.endsWith('.js') && !file.endsWith('spec.js');
}).map(({ file }) => {
return `-n ${file}`;
});

if (srcFiles.length === 0) {
process.chdir('../..');
continue;
}

console.log(`Package "${package}":`);
process.stdout.write(' - running branch-changes test coverage...');
shell.exec(getCovCmd(package, srcFiles));
process.stdout.write('completed.\n\n');
process.chdir('../..');
}
}

execute().then(() => {
process.exit();
}).catch(error => {
console.error(error);
process.exit(1);
});


function getCovCmd(package, srcFiles) {
if (package === 'output-geoservices') {
return 'npm test -- --coverage --changedSince=master --coverageDirectory=.coverage/changes --reporters --silent > /dev/null';
}

return `npx nyc -r=json --report-dir=.coverage/changes --temp-dir=.coverage/changes/analysis ${srcFiles.join(' ')} ${getTestCmd(package)}`;
}

function getTestCmd(package) {
if (package === 'featureserver' || package === 'winnow') {
return 'npm run test:unit > /dev/null';
}

return 'npm run test > /dev/null';
}
9 changes: 9 additions & 0 deletions ci/run-report-on-branch-changes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const shell = require('shelljs');
const fs = require('fs');

if (!fs.existsSync('.coverage/changes')) {
console.log('No changes to report!');
return;
}

shell.exec('nyc report --temp-dir=.coverage/changes --reporter=json-summary --report-dir=.coverage_changes_json');
45 changes: 45 additions & 0 deletions ci/run-test-coverage-analysis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

const { workspaces } = require('../package.json');
const shell = require('shelljs');
const path = require('path');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const argv = yargs(hideBin(process.argv)).argv;
const context = argv.context || 'all';

workspaces.forEach((workspace) => {
process.chdir(workspace);
const package = workspace.split(path.sep).pop();
console.log(`Package "${package}":`);
process.stdout.write(` - running ${context} test coverage...`);
shell.exec(getCovCmd(package, context));
process.stdout.write('completed.\n');
process.stdout.write(` - generating ${context} test coverage badge...`);
shell.exec(getBadgeCmd(package, context));
process.stdout.write('completed.\n\n');
process.chdir('../..');
});

function getCovCmd(package, context) {
if (package === 'output-geoservices') {
return 'npm test -- --coverage --coverageDirectory=.coverage/all --reporters --silent > /dev/null';
}

return `npx cross-env SUPPRESS_NO_CONFIG_WARNING=true nyc -r=json-summary -r=json --report-dir=.coverage/all --temp-dir=.coverage/all/analysis ${getTestCmd(package, context)}`;
}

function getBadgeCmd(package, context) {
if (context === 'unit' && (package === 'featureserver' || package === 'winnow')) {
return 'npx coverage-badges-cli --source .coverage/all/coverage-summary.json --output ./coverage-unit.svg > /dev/null';
}

return 'npx coverage-badges-cli --source .coverage/all/coverage-summary.json --output ./coverage.svg > /dev/null';
}

function getTestCmd(package, context) {
if (context === 'unit' && (package === 'featureserver' || package === 'winnow')) {
return 'npm run test:unit > /dev/null';
}

return 'npm run test > /dev/null';
}
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const config = {
// projects: undefined,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
//reporters: [],

// Automatically reset mock state before every test
// resetMocks: false,
Expand Down
Loading

0 comments on commit b8c7eed

Please sign in to comment.