How to extract test coverage from a report with resume text from Istanbul with regular expression?

Gitlab CI requires that you specify a regular expression to extract the contents of the statement code (so that they can display it). Given the build result below (with a joke and istanbul), I managed to get to: /Statements.*(\d+\%)/

 ... (other build output) =============================== Coverage summary =============================== Statements : 53.07% ( 95/179 ) Branches : 66.67% ( 28/42 ) Functions : 30.99% ( 22/71 ) Lines : 50.96% ( 80/157 ) ================================================================================ ... (other build output) 

Part of Statements : 53.07% stands out here Statements : 53.07% (see here http://regexr.com/3e9sl ). However, I only need to match part 53.07, how do I do this?

+7
javascript gitlab regex code-coverage istanbul
source share
1 answer

I need to combine only part 53.07,

Use lazy .*? add (?:\.\d+)? To also combine floats and access the capture group:

 var re = /Statements.*?(\d+(?:\.\d+)?)%/; var str = '... (other build output)\n=============================== Coverage summary ===============================\nStatements : 53.07% ( 95/179 )\nBranches : 66.67% ( 28/42 )\nFunctions : 30.99% ( 22/71 )\nLines : 50.96% ( 80/157 )\n================================================================================\n... (other build output)'; var res = (m = re.exec(str)) ? m[1] : ""; console.log(res); 

Note that Statements.*?(\d+(?:\.\d+)?)% Also accepts integer values, not just floats.

Template Description :

  • Statements - literal string
  • .*? - zero or more characters except spaces, but as little as possible
  • (\d+(?:\.\d+)?) - Group 1 (the value you need will be written to this group), fixing 1 + digits and an optional sequence . and 1 + digits after it
  • % - percent sign (if you need to print it, move it to the parentheses above)

See the regex demo.

+9
source share

All Articles