Skip to content

Commit b98efca

Browse files
authored
feat: add --set code coverage parameter (#12)
``` update-badge --set 78 update-badge --set 78% ```
1 parent ec31b0c commit b98efca

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# check-code-coverage [![ci status][ci image]][ci url] ![check-code-coverage](https://img.shields.io/badge/code--coverage-100%-brightgreen)
1+
# check-code-coverage [![ci status][ci image]][ci url] ![check-code-coverage](https://img.shields.io/badge/code--coverage-66%-yellow)
22
> Utilities for checking the coverage produced by NYC against extra or missing files
33
44
## Use
@@ -46,11 +46,23 @@ If the coverage summary has 96%, then the above badge would be updated to
4646

4747
![check-code-coverage](https://img.shields.io/badge/code--coverage-96%-brightgreen)
4848

49-
Related project: [dependency-version-badge](https://github.com/bahmutov/dependency-version-badge)
49+
- The badges will have different colors, depending on the coverage, see [bin/update-badge.js](bin/update-badge.js)
50+
- If the code coverage badge is not found, a new badge is inserted on the first line.
51+
52+
You can change the JSON summary filename to read coverage from:
53+
54+
```shell
55+
update-badge --from path/to/json/summary/file.json
56+
```
5057

51-
The badges will have different colors, depending on the coverage, see [bin/update-badge.js](bin/update-badge.js)
58+
You can also skip reading file and set the coverage number directly
5259

53-
If the code coverage badge is not found, a new badge is inserted on the first line.
60+
```shell
61+
update-badge --set 78
62+
update-badge --set 78%
63+
```
64+
65+
Related project: [dependency-version-badge](https://github.com/bahmutov/dependency-version-badge)
5466

5567
## Debug
5668

bin/update-badge.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ const debug = require('debug')('check-code-coverage')
55
const path = require('path')
66
const fs = require('fs')
77
const os = require('os')
8+
const arg = require('arg')
9+
10+
const args = arg({
11+
'--from': String, // input json-summary filename, by default "coverage/coverage-summary.json"
12+
'--set': String // so we can convert "78%" into numbers ourselves
13+
})
14+
debug('args: %o', args)
815

916
const availableColors = ['red', 'yellow', 'green', 'brightgreen']
1017

@@ -23,10 +30,30 @@ function getColor(coveredPercent) {
2330
return 'brightgreen'
2431
}
2532

26-
function updateBadge() {
27-
const coverageFilename = path.join(process.cwd(), 'coverage', 'coverage-summary.json')
28-
const coverage = require(coverageFilename)
29-
const pct = coverage.total.statements.pct
33+
function readCoverage(filename) {
34+
if (!filename) {
35+
filename = path.join(process.cwd(), 'coverage', 'coverage-summary.json')
36+
}
37+
debug('reading coverage summary from: %s', filename)
38+
const coverage = require(filename)
39+
return coverage.total.statements.pct
40+
}
41+
42+
function updateBadge(args) {
43+
let pct = 0
44+
if (args['--set']) {
45+
// make sure we can handle "--set 70" and "--set 70%"
46+
pct = parseFloat(args['--set'])
47+
debug('using coverage number: %d', pct)
48+
} else {
49+
pct = readCoverage(args['--from'])
50+
}
51+
if (pct < 0) {
52+
pct = 0
53+
} else if (pct > 100) {
54+
pct = 100
55+
}
56+
debug('clamped coverage: %d', pct)
3057

3158
const readmeFilename = path.join(process.cwd(), 'README.md')
3259
const readmeText = fs.readFileSync(readmeFilename, 'utf8')
@@ -85,4 +112,4 @@ function updateBadge() {
85112
}
86113
}
87114

88-
updateBadge()
115+
updateBadge(args)

0 commit comments

Comments
 (0)