Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building grails project successfully only if coverage check passes

I am working on a grails application. We use cobertura to generate the code coverage reports. Now I want to modify the grails project such that the build should fail if the code coverage is less than say, 90%. How can I achieve this in grails?

like image 863
Npa Avatar asked Oct 06 '22 20:10

Npa


1 Answers

I don't think the code-coverage plugin supports this directly, but it's easy enough to do by hooking into the powerful grails build events infrastructure. By placing this in your scripts/_Events.groovy, the build will fail if coverage is below a certain threshold:

eventStatusFinal = { message ->
  if (message ==~ /.*Cobertura Code Coverage Complete.*/) {
    def report = new XmlSlurper().parse(new File("target/test-reports/cobertura/coverage.xml"))
    if (Float.parseFloat(report.'@line-rate'.text()) < 0.90) {
      throw new RuntimeException("coverage too low!")
    }
  }
}   

This requires you to turn on the XML report generation with this in grails-app/conf/BuildConfig.groovy:

coverage {
    xml = true
}

Adjust the attribute (line-rate, branch-rate) and value as needed.

like image 189
ataylor Avatar answered Oct 10 '22 21:10

ataylor