Skip to content

Commit 4f13227

Browse files
committed
add check task
1 parent c2c6e5c commit 4f13227

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.maiflai
2+
3+
import org.gradle.api.DefaultTask
4+
import org.gradle.api.GradleException
5+
import org.gradle.api.tasks.TaskAction
6+
7+
class OverallCheckTask extends DefaultTask {
8+
9+
File cobertura
10+
double minimumLineRate = 1
11+
12+
@TaskAction
13+
void requireLineCoverage() {
14+
15+
def reportDirName = project.extensions[ScctPlugin.CONFIGURATION_NAME].reportDirName
16+
if (cobertura == null){
17+
cobertura = project.file("$project.buildDir/reports/$reportDirName/cobertura.xml")
18+
}
19+
20+
def xml = new XmlParser().parse(cobertura)
21+
def overallLineRate = xml.attribute('line-rate').toDouble()
22+
def difference = (minimumLineRate - overallLineRate)
23+
if (difference > 0.001) throw new GradleException("Line coverage of $overallLineRate is below $minimumLineRate by $difference")
24+
}
25+
26+
}

src/main/groovy/com/github/maiflai/ScctExtension.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class ScctExtension {
3737
dependsOn(project.tasks[ScctPlugin.COMPILE_NAME])
3838
}
3939

40+
project.tasks.create(ScctPlugin.CHECK_NAME, OverallCheckTask.class) {
41+
dependsOn(project.tasks[ScctPlugin.TEST_NAME])
42+
}
43+
4044
}
4145

4246
private Action<Project> configureRuntimeOptions = new Action<Project>() {
@@ -65,6 +69,7 @@ class ScctExtension {
6569

6670
String reportDirName = 'scct'
6771

72+
6873
}
6974

7075

src/main/groovy/com/github/maiflai/ScctPlugin.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ScctPlugin implements Plugin<Project> {
88
static String CONFIGURATION_NAME = 'scct'
99

1010
static String TEST_NAME = 'testScct'
11+
static String CHECK_NAME = 'checkScct'
1112
static String COMPILE_NAME = 'compileScctScala'
1213

1314
@Override
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.maiflai
2+
3+
import org.gradle.api.Project
4+
import org.gradle.api.tasks.TaskExecutionException
5+
import org.gradle.testfixtures.ProjectBuilder
6+
import org.hamcrest.Description
7+
import org.hamcrest.TypeSafeMatcher
8+
import org.junit.Rule
9+
import org.junit.Test
10+
import org.junit.rules.ExpectedException
11+
12+
class OverallCheckTaskTest {
13+
14+
@Rule
15+
public ExpectedException expectedException = ExpectedException.none()
16+
17+
private Project projectForLineRate(Number lineRate) {
18+
Project project = ProjectBuilder.builder().build()
19+
project.tasks.create('bob', OverallCheckTask) {
20+
minimumLineRate = lineRate
21+
cobertura = new File('src/test/resources/cobertura.xml')
22+
}
23+
project
24+
}
25+
26+
@Test
27+
void failsWhenLineRateIsBelowTarget(){
28+
Project project = projectForLineRate(1)
29+
expectedException.expect(TaskExecutionException)
30+
project.tasks.bob.execute()
31+
}
32+
33+
@Test
34+
void doesNotFailWhenLineRateIsAtTarget() throws Exception {
35+
Project project = projectForLineRate(0.66)
36+
project.tasks.bob.execute()
37+
}
38+
39+
@Test
40+
void doesNotFailWhenLineRateIsAboveTarget() throws Exception {
41+
Project project = projectForLineRate(0.6)
42+
project.tasks.bob.execute()
43+
}
44+
45+
}

src/test/resources/cobertura.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<coverage line-rate="0.66">
2+
<packages>
3+
<package line-rate="0.66" name="&lt;root&gt;">
4+
<classes>
5+
<class line-rate="1" name="Bob" filename="src/main/scala/My.scala">
6+
<methods/>
7+
<lines>
8+
<line number="10" hits="1"/>
9+
</lines>
10+
</class>
11+
<class line-rate="0" name="HelloWorld" filename="src/main/scala/My.scala">
12+
<methods/>
13+
<lines>
14+
<line number="6" hits="0"/>
15+
</lines>
16+
</class>
17+
<class line-rate="1" name="Rita" filename="src/main/myscala/Rita.scala">
18+
<methods/>
19+
<lines>
20+
<line number="1" hits="1"/>
21+
</lines>
22+
</class>
23+
</classes>
24+
</package>
25+
</packages>
26+
</coverage>

0 commit comments

Comments
 (0)