Gradle Task to Validate Clover On OFF Comment

One way to tell Clover to skip instrumentation of a chunk of code is to enclose the code with “/// CLOVER:OFF” and “/// CLOVER:ON”. For example, you may want to skip the JMockit expectation and verifications chunks.


/// CLOVER:OFF
new Expectations() {
{...}
}
/// CLOVER:ON
/// CLOVER:OFF
new Verifications() {
{...}
}
/// CLOVER:ON

To avoid mismatched OFF/ON pairs, I wrote a Gradle task to detect such kind of mismatch. The task is pegged to the compileTestJava task, which requires you use Gradle Java plugin. To use it, simply put the following into your build.gradle file.

apply plugin: 'java'
compileTestJava.doFirst {
task validateCloverOnOff(type: CloverOnOffCheckTask){
srcTree = source
}
validateCloverOnOff.execute()
}

class CloverOnOffCheckTask extends DefaultTask {
FileTree srcTree

@TaskAction
def check() {
srcTree.getFiles().each {
file -> //logger.lifecycle("To compile test class: $file")
def lineCount = 0
def lastOffLine = -1
def lastOnLine = -1
file.eachLine{
line ->
lineCount++
if(line.find(/\/\/\/\s*CLOVER:OFF/)!=null){
if(lastOffLine != -1){
throw new GradleException ("Detected consecutive Clover:OFF at line " + lastOffLine + " and line " + lineCount);
}
lastOnLine = -1;
lastOffLine = lineCount;
}
if(line.find(/\/\/\/\s*CLOVER:ON/)!=null){
if(lastOffLine == -1){
throw new GradleException ("Detected Clover:ON at line " + lineCount + " without proceeding Clover:OFF ");
}
if(lastOnLine != -1){
throw new GradleException ("Detected consecutive Clover:ON at line " + lastOnLine + " and line " + lineCount);
}
lastOnLine = lineCount;
lastOffLine = -1;
}
}
}
}
}

Advertisements
This entry was posted in Build, Java, Tool and Debug and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s