Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Gitlab CI Test coverage parsing

I have been struggling to find a regular expression that will work in the "Test coverage parsing" input on the "CI/CD Pipelines" page. It doesn't help that the examples show inconsistent examples, i.e. some escape parenthesis and other don't, same with percent symbols, some capture the explicit result in $1, etc. Anyway, here is my text - whats the regex?

CI Badge:

![coverage](https://gitlab.com/mycompany/master/badges/master/coverage.svg?job=coverage)

Output text:

Unit test coverage:
   10.01% blocks
   10.01% lines

Tried so far: (added the slashes simply to represent the space at front or regex)

/ \(\d+\.\d+\%\) blocks/
/ (\d+\.\d+\%) blocks/
/ (\d+\.\d+%) blocks/
/ (\d+\.\d+)% blocks/
like image 729
Dan Avatar asked Feb 09 '17 02:02

Dan


People also ask

How do I add a test coverage badge in Gitlab?

On the left sidebar, select Settings > General. Expand Badges. Under "Link", enter the URL that the badges should point to and under "Badge image URL" the URL of the image that should be displayed. Select Add badge.

How do you use code coverage?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.


2 Answers

There is a useful Ruby regex web page.

http://www.rubular.com/

I didn't not help for me with my GitLab code coverage parsing. I think it is because there seems to be a bug registered for a regex problem in GitLab. https://gitlab.com/gitlab-org/gitlab-ce/issues/21495

enter image description here

like image 102
user1728363 Avatar answered Oct 15 '22 03:10

user1728363


This regex should grab the blocks coverage:

^\s*(\d+(?:\.\d+)?%)\s*blocks

If you want to grab the line coverage instead then use this:

^\s*(\d+(?:\.\d+)?%)\s*lines

GitLab adds the leading and trailing / automatically so you don't need to add them to your regex.

For future reference, GitLab is written in Ruby so you can test this regular expression in Ruby over at rubular.com

like image 40
BrokenBinary Avatar answered Oct 15 '22 03:10

BrokenBinary