Skip to content
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jobs:
- name: Check totals 🛡
run: node bin/check-total --min 90

- name: Update code coverage badge 🥇
run: node bin/update-badge

- name: Semantic Release 🚀
uses: cycjimmy/semantic-release-action@v2
env:
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# check-code-coverage [![ci status][ci image]][ci url]
# check-code-coverage [![ci status][ci image]][ci url] ![mock coverage](https://img.shields.io/badge/code--coverage-100-brightgreen)
> Utilities for checking the coverage produced by NYC against extra or missing files

## Use
Expand Down Expand Up @@ -30,5 +30,19 @@ check-total
check-total --from coverage/coverage-summary.json --min 80
```

## update-badge

If your README.md includes Shields.io badge, like this

![code coverage](https://img.shields.io/badge/code--coverage-80-brightgreen)

You can update it using statements covered percentage from `coverage/coverage-summary.json` by running

```shell
update-badge
```

Related project: [dependency-version-badge](https://github.com/bahmutov/dependency-version-badge)

[ci image]: https://github.com/bahmutov/check-code-coverage/workflows/ci/badge.svg?branch=master
[ci url]: https://github.com/bahmutov/check-code-coverage/actions
Empty file modified bin/check-total.js
100644 → 100755
Empty file.
36 changes: 36 additions & 0 deletions bin/update-badge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env node
// @ts-check

const path = require('path')
const fs = require('fs')

function updateBadge() {
const coverageFilename = path.join(process.cwd(), 'coverage', 'coverage-summary.json')
const coverage = require(coverageFilename)
const pct = coverage.total.statements.pct

const readmeFilename = path.join(process.cwd(), 'README.md')
const readmeText = fs.readFileSync(readmeFilename, 'utf8')

function replaceShield() {
// note, Shields.io escaped '-' with '--'
const coverageRe = new RegExp(
'https://img\\.shields\\.io/badge/code--coverage-(\\d+)-brightgreen',
)
const coverageBadge = `https://img.shields.io/badge/code--coverage-${pct}-brightgreen`

const updatedReadmeText = readmeText.replace(
coverageRe,
coverageBadge,
)
return updatedReadmeText
}

const maybeChangedText = replaceShield()
if (maybeChangedText !== readmeText) {
console.log('saving updated readme with coverage %d%%', pct)
fs.writeFileSync(readmeFilename, maybeChangedText, 'utf8')
}
}

updateBadge()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"bin": {
"check-coverage": "bin/check-coverage.js",
"only-covered": "bin/only-covered.js",
"check-total": "bin/check-total.js"
"check-total": "bin/check-total.js",
"update-badge": "bin/update-badge.js"
},
"files": [
"bin"
Expand Down