|
| 1 | +dataFile = "cobertura.ser" |
| 2 | + |
| 3 | +codeCoverageExclusionList = [ |
| 4 | + "**/*BootStrap*", |
| 5 | + "Config*", |
| 6 | + "**/*DataSource*", |
| 7 | + "**/*resources*", |
| 8 | + "**/*UrlMappings*", |
| 9 | + "**/*Tests*", |
| 10 | + "**/grails/test/**", |
| 11 | + "**/org/codehaus/groovy/grails/**", |
| 12 | + "**/PreInit*", |
| 13 | + "*GrailsPlugin*" ] |
| 14 | + |
| 15 | +ant.path(id: "cobertura.classpath"){ |
| 16 | + fileset(dir:"${codeCoveragePluginDir}/lib"){ |
| 17 | + include(name:"*.jar") |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +eventTestPhasesStart = { |
| 22 | + println 'instrumenting classes for coverage' |
| 23 | + |
| 24 | + ant.delete(file:"${dataFile}") |
| 25 | + |
| 26 | + ant.taskdef ( classpathRef : 'cobertura.classpath', resource:"tasks.properties" ) |
| 27 | + |
| 28 | + if (config.coverage.exclusions) { |
| 29 | + codeCoverageExclusionList += config.coverage.exclusions |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + //for now, instrument classes in the same directory grails creates for testClasses |
| 34 | + //TODO - need to figure out how to put cobertura instrumented classes in different dir |
| 35 | + //and put that dir in front of testClasses in the classpath |
| 36 | + ant.'cobertura-instrument' (datafile:"${dataFile}") { |
| 37 | + fileset(dir:classesDirPath) { |
| 38 | + include(name:"**/*.class") |
| 39 | + codeCoverageExclusionList.each { pattern -> |
| 40 | + exclude(name:pattern) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + catch(Exception e) { |
| 46 | + event("StatusFinal", ["Error instrumenting classes: ${e.message}"]) |
| 47 | + exit(1) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +eventTestPhasesEnd = { |
| 52 | + flushReportData() |
| 53 | + |
| 54 | + coverageReportDir = "${config.grails.testing.reports.destDir ?: testReportsDir}/cobertura" |
| 55 | + ant.mkdir(dir:"${coverageReportDir}") |
| 56 | + ant.taskdef ( classpathRef : 'cobertura.classpath', resource:"tasks.properties" ) |
| 57 | + |
| 58 | + ant.'cobertura-report'(destDir:"${coverageReportDir}", datafile:"${dataFile}", format:'html'){ |
| 59 | + //load all these dirs independently so the dir structure is flattened, |
| 60 | + //otherwise the source isn't found for the reports |
| 61 | + fileset(dir:"${basedir}/grails-app/controllers") |
| 62 | + fileset(dir:"${basedir}/grails-app/domain") |
| 63 | + fileset(dir:"${basedir}/grails-app/services") |
| 64 | + fileset(dir:"${basedir}/grails-app/taglib") |
| 65 | + fileset(dir:"${basedir}/grails-app/utils") |
| 66 | + fileset(dir:"${basedir}/src/groovy") |
| 67 | + fileset(dir:"${basedir}/src/java") |
| 68 | + if (config.coverage?.sourceInclusions){ |
| 69 | + config.coverage.sourceInclusions.each { |
| 70 | + fileset(dir:"${basedir}/${it}") |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +def flushReportData(){ |
| 77 | + try { |
| 78 | + net.sourceforge.cobertura.coveragedata.ProjectData.saveGlobalProjectData() |
| 79 | + } catch (Exception e){ |
| 80 | + event("StatusError", [""" |
| 81 | +-------------------------------------------- |
| 82 | +***********WARNING************* |
| 83 | +Unable to flush code coverage data. |
| 84 | +This usually happens when tests don't actually test anything; |
| 85 | +e.g. none of the instrumented classes were exercised by tests! |
| 86 | +-------------------------------------------- |
| 87 | +"""]) |
| 88 | + } |
| 89 | +} |
0 commit comments