Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verify stage #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>2.0.0</version>
<version>3.0.0</version>
<name>ScalaTest Maven Plugin</name>
<description>Integrates ScalaTest into Maven</description>

Expand Down
36 changes: 35 additions & 1 deletion src/main/java/org/scalatest/tools/maven/TestMojo.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 <code>all.txt,XE ignored_and_pending.txt</code>
Expand Down Expand Up @@ -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");
}
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/org/scalatest/tools/maven/VerifyMojo.java
Original file line number Diff line number Diff line change
@@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nab0310 It seems like we'll always fail in this verify stage, is it intended?

}
}
catch(FileNotFoundException ex) {
throw new MojoExecutionException("Cannot find file: "+summaryFile.getAbsolutePath());
}
catch(IOException ex) {
throw new MojoExecutionException("IOException: "+ ex.toString());
}
//}
}
}
40 changes: 39 additions & 1 deletion src/site/apt/usage.apt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Usage Documentation
<execution>
<goals>
<goal>test</goal>
<goal>verify</goal>
</goals>
<configuration>
<xmlreports>test-results</xmlreports>
Expand All @@ -138,6 +139,7 @@ Usage Documentation
<execution>
<goals>
<goal>test</goal>
<goal>verify</goal>
</goals>
<configuration>
<xmlreports>test-results</xmlreports>
Expand All @@ -150,6 +152,42 @@ Usage Documentation
</build>
</profile>
</profiles>
+--

* 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.

+--

<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<runVerifyOnFailure>true</runVerifyOnFailure>
</configuration>
<executions>
<execution>
<id>test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

+--

The "test" and "gui" goals support multiple Reporters of different types and formatting options for each
Expand All @@ -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.
is configured through the standard "reportOutputDirectory" as is convention in report goals of other plugins.