sedgreppytestcoverage.py

Get value of total coverage percentage from coverage.py using a regular expression (REGEX)


This question is basically on how to use regular expressions but I couldn't find any answer to it in a lot of very closely related questions.

I create coverage reports in a gitlab pipeline using coverage.py and py.test which look like the following piped into a file like coverage37.log:

-------------- generated xml file: /builds/utils/foo/report.xml --------------
---------- coverage: platform linux, python 3.7.11-final-0 -----------
Name                          Stmts   Miss  Cover
-------------------------------------------------
foo/tests/bar1.py                52      0   100%
...
foo/tests/bar2.py                 0      0   100%
-------------------------------------------------
TOTAL                           431      5    99%
======================= 102 passed, 9 warnings in 4.35s ========================

Now I want to create a badge for the total coverage i.e. here the 99% value and only get number (99) in order to assign it to a variable. This variable can then be used to create a flexible coverage badge using the anybadge package.

My naive approach would be something like:

COVERAGE_SCORE=$(sed -n 'what to put here' coverage37.log)
echo "Coverage is $COVERAGE_SCORE"

Note that I know that gitlab, github, etc. offer specific functionalities to create badges automatically. But I want to create it manually in order to have more control and create the badge per branch.

Any hints are welcome. Thanks in advance!


Solution

  • It is easier to use awk here:

    cov_score=$(awk '$1 == "TOTAL" {print $NF+0}' coverage37.log)
    

    Here $1 == "TOTAL" matches a line with first word as TOTAL and print $NF+0 prints number part of last field.