diff --git a/src/it/casperjs-runner/integration-test/invoker.properties b/src/it/casperjs-runner/integration-test/invoker.properties new file mode 100644 index 0000000..58e18a9 --- /dev/null +++ b/src/it/casperjs-runner/integration-test/invoker.properties @@ -0,0 +1,3 @@ +invoker.goals=verify +invoker.buildResult=failure +invoker.failureBehavior=fail-at-end \ No newline at end of file diff --git a/src/it/casperjs-runner/integration-test/pom.xml b/src/it/casperjs-runner/integration-test/pom.xml new file mode 100644 index 0000000..d6a582c --- /dev/null +++ b/src/it/casperjs-runner/integration-test/pom.xml @@ -0,0 +1,61 @@ + + + + + 4.0.0 + + com.github.casperjs + integration-test + 1.0 + pom + + CasperJS Runner :: integration-test + + no-url + + + UTF-8 + true + + + + + + + + com.github.casperjs + casperjs-runner-maven-plugin + @pom.version@ + + + + + + + com.github.casperjs + casperjs-runner-maven-plugin + + + integration-test + + integration-test + + + + verify + + verify + + + + + true + + + + + + + diff --git a/src/it/casperjs-runner/integration-test/postbuild.groovy b/src/it/casperjs-runner/integration-test/postbuild.groovy new file mode 100644 index 0000000..8df0feb --- /dev/null +++ b/src/it/casperjs-runner/integration-test/postbuild.groovy @@ -0,0 +1,13 @@ +file = new File(basedir, 'build.log'); +assert file.exists(); +assert file.text.contains('casperjs-runner-maven-plugin'); +assert file.text.contains('Execution of test test.js'); +assert file.text.contains('Test \'test.js\' has failure'); +assert file.text.contains('Execution of test test.coffee'); +assert file.text.contains('Test \'test.coffee\' has failure'); +assert file.text.contains('FAIL 2 tests executed'); +assert file.text.contains('1 passed, 1 failed'); +assert file.text.contains('Tests run: 2, Success: 0 Failures: 2. Time elapsed:'); +assert file.text.contains('Integration test verification error'); + +return true; \ No newline at end of file diff --git a/src/it/casperjs-runner/integration-test/src/test/casperjs/test.coffee b/src/it/casperjs-runner/integration-test/src/test/casperjs/test.coffee new file mode 100644 index 0000000..639e878 --- /dev/null +++ b/src/it/casperjs-runner/integration-test/src/test/casperjs/test.coffee @@ -0,0 +1,4 @@ +casper.test.begin 'Fake test', 2, (test) -> + test.assert true, 'true is so true' + test.assert false, 'false is so wrong' + test.done() diff --git a/src/it/casperjs-runner/integration-test/src/test/casperjs/test.js b/src/it/casperjs-runner/integration-test/src/test/casperjs/test.js new file mode 100644 index 0000000..0869ffa --- /dev/null +++ b/src/it/casperjs-runner/integration-test/src/test/casperjs/test.js @@ -0,0 +1,5 @@ +casper.test.begin('Fake test', 2, function(test) { + test.assert(true, 'true is so true'); + test.assert(false, 'false is so wrong'); + test.done(); +}); \ No newline at end of file diff --git a/src/main/java/com/github/casperjs/casperjsrunner/CasperJSRunnerMojo.java b/src/main/java/com/github/casperjs/casperjsrunner/AbstractCasperJSRunnerMojo.java similarity index 95% rename from src/main/java/com/github/casperjs/casperjsrunner/CasperJSRunnerMojo.java rename to src/main/java/com/github/casperjs/casperjsrunner/AbstractCasperJSRunnerMojo.java index b982776..b7836a3 100644 --- a/src/main/java/com/github/casperjs/casperjsrunner/CasperJSRunnerMojo.java +++ b/src/main/java/com/github/casperjs/casperjsrunner/AbstractCasperJSRunnerMojo.java @@ -16,8 +16,6 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.toolchain.ToolchainManager; @@ -31,10 +29,10 @@ * Runs JavaScript and/or CoffeScript test files on CasperJS instance * * @author Romain Linsolas + * @author Benoit Guerin * @since 1.0.0 */ -@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST, threadSafe = true) -public class CasperJSRunnerMojo extends AbstractMojo { +public abstract class AbstractCasperJSRunnerMojo extends AbstractMojo { // Parameters for the plugin @@ -98,14 +96,6 @@ public class CasperJSRunnerMojo extends AbstractMojo { @Parameter private List testsExcludes; - /** - * Do we ignore the tests failures. If yes, the plugin will not fail at the end if there was tests failures. - * - * @since 1.0.0 - */ - @Parameter(property = "casperjs.ignoreTestFailures", defaultValue = "${maven.test.failure.ignore}") - private boolean ignoreTestFailures = false; - /** * Set the plugin to be verbose during its execution. * @@ -260,7 +250,7 @@ public class CasperJSRunnerMojo extends AbstractMojo { /** * The directory where output files will be stored - * + * * @since 1.0.0 */ @Parameter(defaultValue = "${project.build.directory}/casperjs") @@ -268,7 +258,7 @@ public class CasperJSRunnerMojo extends AbstractMojo { /** * The current maven session, used by the ToolChainManager - * + * * @since 1.0.0 */ @Parameter(defaultValue = "${session}") @@ -311,9 +301,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { final Collection scripts = findScripts(); final Result globalResult = executeScripts(scripts); getLogger().info(globalResult.print()); - if (!ignoreTestFailures && globalResult.getFailures() > 0) { - throw new MojoFailureException("There are " + globalResult.getFailures() + " tests failures"); - } + afterTestExecution(globalResult); } private void init() throws MojoFailureException { @@ -456,4 +444,7 @@ private int executeScript(final File f) { return executeCommand(cmdLine, environmentVariables, verbose); } + protected void afterTestExecution(final Result globalResult) throws MojoFailureException, MojoExecutionException { + } + } diff --git a/src/main/java/com/github/casperjs/casperjsrunner/CasperJSRunnerIntegrationTestMojo.java b/src/main/java/com/github/casperjs/casperjsrunner/CasperJSRunnerIntegrationTestMojo.java new file mode 100644 index 0000000..fcb8dca --- /dev/null +++ b/src/main/java/com/github/casperjs/casperjsrunner/CasperJSRunnerIntegrationTestMojo.java @@ -0,0 +1,72 @@ +package com.github.casperjs.casperjsrunner; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +/** + * Runs JavaScript and/or CoffeScript test files on CasperJS instance, but in "integration-test" mode. IE does not fails the build if there are test + * errors, letting the verify Mojo doing it. + * + * @author Vilmos Nagy (vilmos dot nagy at outlook dot com) + */ +@Mojo(name = "integration-test", defaultPhase = LifecyclePhase.INTEGRATION_TEST) +public class CasperJSRunnerIntegrationTestMojo extends AbstractCasperJSRunnerMojo { + + /** + * A file in which the count of failed tests will be written. We'll check this file in the verify phase to fail the build, if the testFailures + * ignored during the test mojo. + */ + @Parameter(property = "casperjs.testFailure.countFile", defaultValue = "${project.build.directory}/casperjsFailureCount") + private File testFailureCountFile; + + @Override + protected void afterTestExecution(final Result globalResult) throws MojoFailureException, MojoExecutionException { + writeFailedTestCount(globalResult); + } + + private void writeFailedTestCount(final Result globalResult) throws MojoExecutionException { + try { + tryToWriteFailedTestCount(globalResult); + } catch (final IOException e) { + throw new MojoExecutionException("Could not write the failed tests' count to disk.", e); + } + } + + private void tryToWriteFailedTestCount(final Result globalResult) throws IOException { + createParentDirectoryIfNecessary(testFailureCountFile); + writeFailedTestCountToExistingDir(globalResult); + } + + private void writeFailedTestCountToExistingDir(final Result globalResult) throws IOException { + BufferedWriter bufferedWriter = null; + try { + bufferedWriter = new BufferedWriter(new FileWriter(testFailureCountFile, false)); + bufferedWriter.write(globalResult.getFailures() + "\n"); + } finally { + if (bufferedWriter != null) { + try { + bufferedWriter.close(); + } catch (final Exception ignored) { + } + } + } + } + + private void createParentDirectoryIfNecessary(final File destinationFile) throws IOException { + final File parentDir = destinationFile.getParentFile(); + if (!parentDir.exists()) { + final boolean success = parentDir.mkdirs(); + if (!success) { + throw new IOException("Cannot create directory for output file: " + destinationFile.toString()); + } + } + } +} diff --git a/src/main/java/com/github/casperjs/casperjsrunner/CasperJSRunnerTestMojo.java b/src/main/java/com/github/casperjs/casperjsrunner/CasperJSRunnerTestMojo.java new file mode 100644 index 0000000..b838920 --- /dev/null +++ b/src/main/java/com/github/casperjs/casperjsrunner/CasperJSRunnerTestMojo.java @@ -0,0 +1,31 @@ +package com.github.casperjs.casperjsrunner; + +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; + +/** + * Runs JavaScript and/or CoffeScript test files on CasperJS instance + * + * @author Benoit Guerin + * @author Vilmos Nagy (vilmos dot nagy at outlook dot com) + */ +@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST, threadSafe = true) +public class CasperJSRunnerTestMojo extends AbstractCasperJSRunnerMojo { + + /** + * Do we ignore the tests failures. If yes, the plugin will not fail at the end if there was tests failures. + * + * @since 1.0.0 + */ + @Parameter(property = "casperjs.ignoreTestFailures", defaultValue = "${maven.test.failure.ignore}") + private boolean ignoreTestFailures = false; + + @Override + protected void afterTestExecution(final Result globalResult) throws MojoFailureException { + if (!ignoreTestFailures && globalResult.getFailures() > 0) { + throw new MojoFailureException("There are " + globalResult.getFailures() + " tests failures"); + } + } +} diff --git a/src/main/java/com/github/casperjs/casperjsrunner/CasperJSVerifyMojo.java b/src/main/java/com/github/casperjs/casperjsrunner/CasperJSVerifyMojo.java new file mode 100644 index 0000000..dac18f1 --- /dev/null +++ b/src/main/java/com/github/casperjs/casperjsrunner/CasperJSVerifyMojo.java @@ -0,0 +1,67 @@ +package com.github.casperjs.casperjsrunner; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * Verifies that there was no test failure during the integration-test Mojo. + * + * @author Vilmos Nagy (vilmos dot nagy at outlook dot com) + */ +@Mojo(name = "verify", defaultPhase = LifecyclePhase.VERIFY) +public class CasperJSVerifyMojo extends AbstractMojo { + + /** + * A file in which the count of failed tests will be written. We'll check this file in the verify phase to fail the build, if the testFailures + * ignored during the test mojo. + */ + @Parameter(property = "casperjs.testFailure.countFile", defaultValue = "${project.build.directory}/casperjsFailureCount") + private File testFailureCountFile; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + if (!testFailureCountFile.exists()) { + throw new MojoFailureException( + "The testFailureCountFile " + testFailureCountFile.getAbsolutePath() + " doesn't exists. Run tests before the verify phase!"); + } + + final String firstLine = readFirstLineOfFile(); + final Integer failedTestCount = Integer.valueOf(firstLine); + if (failedTestCount > 0) { + throw new MojoFailureException("Integration test verification error: There are " + failedTestCount + " tests failures"); + } + } + + private String readFirstLineOfFile() throws MojoExecutionException { + try { + return tryToReadFirstLineOfFile(); + } catch (final IOException e) { + throw new MojoExecutionException("", e); + } + } + + private String tryToReadFirstLineOfFile() throws IOException { + BufferedReader bufferedReader = null; + try { + bufferedReader = new BufferedReader(new FileReader(testFailureCountFile)); + return bufferedReader.readLine(); + } finally { + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (final Exception ignored) { + } + } + } + } + +}