From 65913b2eaceac555b603086153f5e5882ada7eab Mon Sep 17 00:00:00 2001 From: nb051436 Date: Fri, 7 Dec 2018 15:54:32 -0600 Subject: [PATCH] Add verify stage --- pom.xml | 2 +- .../org/scalatest/tools/maven/TestMojo.java | 36 ++++++++++++- .../org/scalatest/tools/maven/VerifyMojo.java | 50 +++++++++++++++++++ src/site/apt/usage.apt | 40 ++++++++++++++- 4 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/scalatest/tools/maven/VerifyMojo.java diff --git a/pom.xml b/pom.xml index fc4fc2e..88984cb 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.scalatest scalatest-maven-plugin maven-plugin - 2.0.0 + 3.0.0 ScalaTest Maven Plugin Integrates ScalaTest into Maven diff --git a/src/main/java/org/scalatest/tools/maven/TestMojo.java b/src/main/java/org/scalatest/tools/maven/TestMojo.java index 070a9ab..05d77f9 100644 --- a/src/main/java/org/scalatest/tools/maven/TestMojo.java +++ b/src/main/java/org/scalatest/tools/maven/TestMojo.java @@ -1,7 +1,12 @@ package org.scalatest.tools.maven; +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; + +import static java.util.Collections.newSetFromMap; import static org.scalatest.tools.maven.MojoUtils.*; import java.io.File; @@ -43,6 +48,12 @@ public class TestMojo extends AbstractScalaTestMojo { */ boolean testFailureIgnore; + /** + * Set to true to run the verify goal on failure + * @parameter default-value=false property="maven.test.failure.run.verify" + */ + boolean runVerifyOnFailure; + /** * Comma separated list of filereporters. A filereporter consists of an optional * configuration and a mandatory filename, separated by a whitespace. E.g all.txt,XE ignored_and_pending.txt @@ -104,7 +115,30 @@ public void execute() throws MojoExecutionException, MojoFailureException { getLog().info("Tests are skipped."); } else { if (!runScalaTest(configuration()) && !testFailureIgnore) { - throw new MojoFailureException("There are test failures"); + if (runVerifyOnFailure) { + try { + File newFile = new File( + reportsDirectory.getAbsolutePath() + "/scalatest-summary.txt"); + + if (newFile.createNewFile()) { + FileWriter fileWriter = + new FileWriter(newFile); + + BufferedWriter bufferedWriter = + new BufferedWriter(fileWriter); + bufferedWriter.write("failure"); + bufferedWriter.close(); + + } else { + throw new IOException("create new file failed"); + } + } catch (IOException ex) { + throw new MojoExecutionException( + "Failure to write to file, it might not exist: " + ex); + } + } else { + throw new MojoExecutionException("There are test failures"); + } } } } diff --git a/src/main/java/org/scalatest/tools/maven/VerifyMojo.java b/src/main/java/org/scalatest/tools/maven/VerifyMojo.java new file mode 100644 index 0000000..47d7f1a --- /dev/null +++ b/src/main/java/org/scalatest/tools/maven/VerifyMojo.java @@ -0,0 +1,50 @@ +package org.scalatest.tools.maven; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; + +/** + * A verification that all the tests passed. + * @phase verify + * @goal verify + */ +public class VerifyMojo extends AbstractScalaTestMojo { + + /** + * The summary file to read integration test results from. + * @parameter default-value="${project.build.directory}/surefire-reports/scalatest-summary.txt" property="scalatest.reportsDirectory" + */ + private File summaryFile; + + public void execute() throws MojoExecutionException { + try { + File reportFile = new File(summaryFile.getAbsolutePath()); + // FileReader reads text files in the default encoding. + FileReader fileReader = + new FileReader(reportFile); + + // Always wrap FileReader in BufferedReader. + BufferedReader bufferedReader = + new BufferedReader(fileReader); + if(bufferedReader.readLine().contentEquals("failure")) { + bufferedReader.close(); + throw new MojoExecutionException("There are test failures"); + } else { + bufferedReader.close(); + throw new MojoExecutionException("File has invalid content"); + } + } + catch(FileNotFoundException ex) { + throw new MojoExecutionException("Cannot find file: "+summaryFile.getAbsolutePath()); + } + catch(IOException ex) { + throw new MojoExecutionException("IOException: "+ ex.toString()); + } + //} + } +} diff --git a/src/site/apt/usage.apt b/src/site/apt/usage.apt index 0e178f7..c626ed5 100644 --- a/src/site/apt/usage.apt +++ b/src/site/apt/usage.apt @@ -115,6 +115,7 @@ Usage Documentation test + verify test-results @@ -138,6 +139,7 @@ Usage Documentation test + verify test-results @@ -150,6 +152,42 @@ Usage Documentation ++-- + +* Allowing the Post-Integration Phase to run + + In its original implementation, the plugin fails the build immediately if there is a test failure. + If you are using a dockerized test setup, this is bad behavior because your docker instances will + not have enough time to shutdown (https://github.com/scalatest/scalatest-maven-plugin/issues/45). + You can use these config values to stop this from happening by failing the build in the verification + stage rather than the testing phase. + ++-- + + + org.scalatest + scalatest-maven-plugin + 3.0.0 + + true + + + + test + integration-test + + test + + + + verify + + verify + + + + + +-- The "test" and "gui" goals support multiple Reporters of different types and formatting options for each @@ -173,4 +211,4 @@ Usage Documentation The "reporter" goal does not support this level of customization, however, because it doesn't really apply. Instead, this goal has a "fileReporterOptions" option that allows you to specify the formatting options to pass to the FileReporter. The plugin itself controls the name of that file, and the location of the file - is configured through the standard "reportOutputDirectory" as is convention in report goals of other plugins. \ No newline at end of file + is configured through the standard "reportOutputDirectory" as is convention in report goals of other plugins.