You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2019/12/05 12:23:53 UTC

[lucene-solr] branch gradle-master updated: Fail the build if --tests filter is applied and no tests execute during the entire build (this allows for an empty set of filtered tests at single project level).

This is an automated email from the ASF dual-hosted git repository.

dweiss pushed a commit to branch gradle-master
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/gradle-master by this push:
     new 62a810c  Fail the build if --tests filter is applied and no tests execute during the entire build (this allows for an empty set of filtered tests at single project level).
62a810c is described below

commit 62a810cda736380693586ec8e7aafc607b4e034b
Author: Dawid Weiss <dw...@apache.org>
AuthorDate: Thu Dec 5 13:23:43 2019 +0100

    Fail the build if --tests filter is applied and no tests execute during the entire build (this allows for an empty set of filtered tests at single project level).
---
 build.gradle                           |  1 +
 gradle/testing/fail-on-no-tests.gradle | 41 ++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/build.gradle b/build.gradle
index b5d39d3..4305712 100644
--- a/build.gradle
+++ b/build.gradle
@@ -29,6 +29,7 @@ apply from: file('gradle/defaults-java.gradle')
 apply from: file('gradle/testing/defaults-tests.gradle')
 apply from: file('gradle/testing/defaults-tests-solr.gradle')
 apply from: file('gradle/testing/randomization.gradle')
+apply from: file('gradle/testing/fail-on-no-tests.gradle')
 
 // Maven publishing.
 apply from: file('gradle/maven/defaults-maven.gradle')
diff --git a/gradle/testing/fail-on-no-tests.gradle b/gradle/testing/fail-on-no-tests.gradle
new file mode 100644
index 0000000..dd148d1
--- /dev/null
+++ b/gradle/testing/fail-on-no-tests.gradle
@@ -0,0 +1,41 @@
+// If we run the test task with a filter we want to fail if no test actually ran (everything was excluded).
+
+configure(allprojects) {
+  plugins.withType(JavaPlugin) {
+    test {
+      filter {
+        failOnNoMatchingTests = false
+      }
+    }
+  }
+}
+
+gradle.taskGraph.whenReady { graph ->
+  def args = gradle.startParameter.taskNames
+  def filters = args.findAll({ arg ->
+    return arg == /--tests/
+  })
+
+  // Only apply the check if we are actually filtering.
+  if (!filters.isEmpty()) {
+    def testTasks = graph.allTasks.findAll { task -> task instanceof Test }
+    // ... and there are some test tasks in the execution graph.
+    if (!testTasks.isEmpty()) {
+      def executedTests = 0
+
+      testTasks.each { task ->
+        task.afterSuite { desc, result ->
+          executedTests += result.testCount
+        }
+      }
+
+      // After the build is finished, check the test count.
+      gradle.buildFinished {
+        if (executedTests == 0) {
+          throw new GradleException("No tests found for the given filters?")
+        }
+      }
+    }
+  }
+}
+