File tree Expand file tree Collapse file tree 2 files changed +25
-4
lines changed Expand file tree Collapse file tree 2 files changed +25
-4
lines changed Original file line number Diff line number Diff line change 1- # check-code-coverage [ ![ ci status] [ ci image ]] [ ci url ] ![ mock 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-100%-brightgreen )
22> Utilities for checking the coverage produced by NYC against extra or missing files
33
44## Use
@@ -34,7 +34,7 @@ check-total --from coverage/coverage-summary.json --min 80
3434
3535If your README.md includes Shields.io badge, like this
3636
37- 
37+ 
3838
3939You can update it using statements covered percentage from ` coverage/coverage-summary.json ` by running
4040
@@ -44,12 +44,14 @@ update-badge
4444
4545If the coverage summary has 96%, then the above badge would be updated to
4646
47- 
47+ 
4848
4949Related project: [ dependency-version-badge] ( https://github.com/bahmutov/dependency-version-badge )
5050
5151The badges will have different colors, depending on the coverage, see [ bin/update-badge.js] ( bin/update-badge.js )
5252
53+ If the code coverage badge is not found, a new badge is inserted on the first line.
54+
5355## Debug
5456
5557To see verbose log messages, run with ` DEBUG=check-code-coverage ` environment variable
Original file line number Diff line number Diff line change 44const debug = require ( 'debug' ) ( 'check-code-coverage' )
55const path = require ( 'path' )
66const fs = require ( 'fs' )
7+ const os = require ( 'os' )
78
89const availableColors = [ 'red' , 'yellow' , 'green' , 'brightgreen' ]
910
@@ -47,13 +48,31 @@ function updateBadge() {
4748 debug ( 'coverage regex: "%s"' , coverageRe )
4849 debug ( 'new coverage badge: "%s"' , coverageBadge )
4950
50- const updatedReadmeText = readmeText . replace (
51+ let found
52+ let updatedReadmeText = readmeText . replace (
5153 coverageRe ,
5254 ( match ) => {
55+ found = true
5356 debug ( 'match: %o' , match )
5457 return coverageBadge
5558 } ,
5659 )
60+
61+ if ( ! found ) {
62+ console . log ( '⚠️ Could not find code coverage badge in file %s' , readmeFilename )
63+ console . log ( 'Insert new badge on the first line' )
64+ // use NPM package name as label to flag where this badge is coming from
65+ const badge = ``
66+ debug ( 'inserting new badge: %s' , badge )
67+
68+ const lines = readmeText . split ( os . EOL )
69+ if ( lines . length < 1 ) {
70+ console . error ( 'File %s has no lines, cannot insert code coverage badge' , readmeFilename )
71+ return readmeText
72+ }
73+ lines [ 0 ] += ' ' + badge
74+ updatedReadmeText = lines . join ( os . EOL )
75+ }
5776 return updatedReadmeText
5877 }
5978
You can’t perform that action at this time.
0 commit comments