Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'casperjs/master'
Browse files Browse the repository at this point in the history
Conflicts:
	src/main/java/com/github/casperjs/casperjsrunner/AbstractCasperJSRunnerMojo.java
  • Loading branch information
Benoit Guerin committed Jan 3, 2017
2 parents 1c3c590 + f6e5b5a commit db9c5c7
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/it/casperjs-runner/integration-test/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.goals=verify
invoker.buildResult=failure
invoker.failureBehavior=fail-at-end
61 changes: 61 additions & 0 deletions src/it/casperjs-runner/integration-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.github.casperjs</groupId>
<artifactId>integration-test</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<name>CasperJS Runner :: integration-test</name>

<url>no-url</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<casperjs.verbose>true</casperjs.verbose>
</properties>

<build>

<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.casperjs</groupId>
<artifactId>casperjs-runner-maven-plugin</artifactId>
<version>@pom.version@</version>
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<groupId>com.github.casperjs</groupId>
<artifactId>casperjs-runner-maven-plugin</artifactId>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreTestFailures>true</ignoreTestFailures>
</configuration>
</plugin>
</plugins>

</build>

</project>
13 changes: 13 additions & 0 deletions src/it/casperjs-runner/integration-test/postbuild.groovy
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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();
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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

Expand Down Expand Up @@ -98,14 +96,6 @@ public class CasperJSRunnerMojo extends AbstractMojo {
@Parameter
private List<String> 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.
*
Expand Down Expand Up @@ -260,15 +250,15 @@ public class CasperJSRunnerMojo extends AbstractMojo {

/**
* The directory where output files will be stored
*
*
* @since 1.0.0
*/
@Parameter(defaultValue = "${project.build.directory}/casperjs")
private File targetDir;

/**
* The current maven session, used by the ToolChainManager
*
*
* @since 1.0.0
*/
@Parameter(defaultValue = "${session}")
Expand Down Expand Up @@ -311,9 +301,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
final Collection<String> 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 {
Expand Down Expand Up @@ -456,4 +444,7 @@ private int executeScript(final File f) {
return executeCommand(cmdLine, environmentVariables, verbose);
}

protected void afterTestExecution(final Result globalResult) throws MojoFailureException, MojoExecutionException {
}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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) {
}
}
}
}

}

0 comments on commit db9c5c7

Please sign in to comment.