blob: 16fdf264d2139e22953e8b77ff3d23c7ff27dbfb [file] [log] [blame]
diff --git a/build.gradle b/build.gradle
index 5db7b0218ee..9da5a84c12b 100644
--- a/build.gradle
+++ b/build.gradle
@@ -90,6 +90,7 @@ apply from: file('gradle/testing/per-project-summary.gradle')
apply from: file('gradle/testing/slowest-tests-at-end.gradle')
apply from: file('gradle/testing/failed-tests-at-end.gradle')
apply from: file('gradle/testing/profiling.gradle')
+apply from: file('gradle/testing/coverage.gradle')
apply from: file('gradle/help.gradle')
// Ant-compatibility layer. ALL OF THESE SHOULD BE GONE at some point. They are
diff --git a/gradle/testing/coverage.gradle b/gradle/testing/coverage.gradle
new file mode 100644
index 00000000000..af43389454e
--- /dev/null
+++ b/gradle/testing/coverage.gradle
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+if (Boolean.parseBoolean(propertyOrDefault("tests.coverage", "false"))) {
+ allprojects {
+ apply plugin: "jacoco"
+ tasks.withType(Test) {
+ // XXX: too many things called "workingDir" in gradle!
+ def targetDir = "${buildDir}/tmp/tests-cwd"
+ jacoco {
+ destinationFile = file("${targetDir}/jacoco.db")
+ }
+ doFirst {
+ // delete any previous results
+ project.delete fileTree(dir: targetDir, include: 'jacoco.db')
+ // set sysprop with location of resolved jar, to give it AllPermission
+ systemProperty 'jacoco.agent.jar', jacoco.getAgentClasspath().getSingleFile().toPath().toAbsolutePath().toString()
+ }
+ }
+ }
+}
diff --git a/gradle/testing/defaults-tests.gradle b/gradle/testing/defaults-tests.gradle
index 583b76e7646..1e0a05ec49f 100644
--- a/gradle/testing/defaults-tests.gradle
+++ b/gradle/testing/defaults-tests.gradle
@@ -87,6 +87,7 @@ allprojects {
// Pass these to RandomizedRunner so that it doesn't attempt to set them.
systemProperty 'junit4.childvm.count', '1'
systemProperty 'junit4.childvm.id', '0'
+ systemProperty 'junit4.childvm.cwd', workingDir as String
// Set up cwd and temp locations.
systemProperty("java.io.tmpdir", testsTmpDir)
diff --git a/help/tests.txt b/help/tests.txt
index 2caef222d43..9bcaa6889cb 100644
--- a/help/tests.txt
+++ b/help/tests.txt
@@ -141,6 +141,16 @@ to increase the top-N count:
gradlew -p lucene/core test -Ptests.profile=true -Ptests.profile.count=100
+Generating Coverage Reports
+------------------------------
+
+Adding the property "tests.coverage=true" will run the tests with instrumentation to
+record coverage. The "jacocoTestReport" target will generate a report from those files.
+
+Example:
+
+gradlew -p lucene/core test jacocoTestReport -Ptests.coverage=true
+
Testing against different JVMs
------------------------------
diff --git a/lucene/tools/junit4/replicator-tests.policy b/lucene/tools/junit4/replicator-tests.policy
index 7fa4a0f2857..c5e82eb37b1 100644
--- a/lucene/tools/junit4/replicator-tests.policy
+++ b/lucene/tools/junit4/replicator-tests.policy
@@ -68,6 +68,8 @@ grant {
permission java.io.FilePermission "${junit4.childvm.cwd}${/}jacoco.db", "write";
// needed by jacoco to dump coverage on shutdown
permission java.lang.RuntimePermission "shutdownHooks";
+ // needed by jacoco to instrument classes
+ permission java.lang.RuntimePermission "defineClass";
};
// Grant all permissions to Gradle test runner classes.
diff --git a/lucene/tools/junit4/tests.policy b/lucene/tools/junit4/tests.policy
index 8dedb3b6fd8..4b0cf222f49 100644
--- a/lucene/tools/junit4/tests.policy
+++ b/lucene/tools/junit4/tests.policy
@@ -96,6 +96,8 @@ grant {
permission java.io.FilePermission "${junit4.childvm.cwd}${/}jacoco.db", "write";
// needed by jacoco to dump coverage on shutdown
permission java.lang.RuntimePermission "shutdownHooks";
+ // needed by jacoco to instrument classes
+ permission java.lang.RuntimePermission "defineClass";
};
// Grant all permissions to Gradle test runner classes.