-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #925 from koopjs/coverage-changes
Coverage changes
- Loading branch information
Showing
21 changed files
with
653 additions
and
65 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.