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

Commit

Permalink
Merge pull request #18 from netceler/master
Browse files Browse the repository at this point in the history
New option to save log of each CasperJS invocation
  • Loading branch information
bguerin authored Jan 3, 2017
2 parents b63fc08 + 4d909fc commit 81d9ab7
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/it/casperjs-runner/logs/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals=test
invoker.failureBehavior=fail-at-end
55 changes: 55 additions & 0 deletions src/it/casperjs-runner/logs/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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>logs</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<name>CasperJS Runner :: Logs</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>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<enableLogReports>true</enableLogReports>
</configuration>
</plugin>
</plugins>

</build>

</project>
26 changes: 26 additions & 0 deletions src/it/casperjs-runner/logs/postbuild.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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('Execution of test test.coffee');
assert file.text.contains('PASS 2 tests executed');
assert file.text.contains('2 passed, 0 failed');
assert file.text.contains('Tests run: 4, Success: 4 Failures: 0. Time elapsed:');

file = new File(basedir, 'target/casperjs-reports/test_coffee.txt');
assert file.exists();
assert file.text.contains('Fake coffee test');

file = new File(basedir, 'target/casperjs-reports/test_js.txt');
assert file.exists();
assert file.text.contains('Fake js test');

file = new File(basedir, 'target/casperjs-reports/subdir_test_coffee.txt');
assert file.exists();
assert file.text.contains('Subdir fake coffee test');

file = new File(basedir, 'target/casperjs-reports/subdir_test_js.txt');
assert file.exists();
assert file.text.contains('Subdir fake js test');

return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
casper.test.begin 'Subdir fake coffee test', 2, (test) ->
test.assert true, 'true is so true'
test.assertNot false, 'false is so wrong'
test.done()
5 changes: 5 additions & 0 deletions src/it/casperjs-runner/logs/src/test/casperjs/subdir/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
casper.test.begin('Subdir fake js test', 2, function(test) {
test.assert(true, 'true is so true');
test.assertNot(false, 'false is so wrong');
test.done();
});
4 changes: 4 additions & 0 deletions src/it/casperjs-runner/logs/src/test/casperjs/test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
casper.test.begin 'Fake coffee test', 2, (test) ->
test.assert true, 'true is so true'
test.assertNot false, 'false is so wrong'
test.done()
5 changes: 5 additions & 0 deletions src/it/casperjs-runner/logs/src/test/casperjs/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
casper.test.begin('Fake js test', 2, function(test) {
test.assert(true, 'true is so true');
test.assertNot(false, 'false is so wrong');
test.done();
});
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ public abstract class AbstractCasperJSRunnerMojo extends AbstractMojo {
@Parameter
private List<String> includesPatterns;

/**
* Should CasperJS generates log reports. If <code>true</code> (which is the default), a log file will be generated for each test, containing the
* output of the CasperJS run. These reports will be generated in the
* <code>reportsDirectory<code> directory, with a name of <code>test filename&gt;.txt</code>.
*
* @since 1.0.5
*/
@Parameter(property = "casperjs.enableLogReports", defaultValue = "true")
private boolean enableLogReports;

/**
* Should CasperJS generates XML reports, through the <code>--xunit=[filename]</code> option. If <code>true</code>, such reports will be generated
* in the <code>reportsDirectory<code> directory,
Expand Down Expand Up @@ -344,8 +354,8 @@ private void init() throws MojoFailureException {
}
}

if (enableXmlReports) {
getLogger().debug("creating directories to hold xunit file(s)");
if (enableXmlReports || enableLogReports) {
getLogger().debug("creating directories to hold log/xunit file(s)");
reportsDir.mkdirs();
}
}
Expand Down Expand Up @@ -441,7 +451,12 @@ private int executeScript(final File f) {
cmdLine.addArgument(quote(argument), false);
}
}
return executeCommand(cmdLine, environmentVariables, verbose);
File logFile = null;
if (enableLogReports) {
logFile = new File(reportsDir, buildName(scriptsDir, f) + ".txt");
}

return executeCommand(cmdLine, environmentVariables, verbose, logFile);
}

protected void afterTestExecution(final Result globalResult) throws MojoFailureException, MojoExecutionException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.casperjs.casperjsrunner;

import static com.google.common.collect.Lists.newArrayList;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class CloneOutputStream extends OutputStream {

private List<OutputStream> streams;

public CloneOutputStream(final OutputStream... streams) {
this.streams = newArrayList(streams);
}

@Override
public void write(final int b) throws IOException {
for (final OutputStream stream : streams) {
stream.write(b);
}
}

@Override
public void write(final byte[] b) throws IOException {
for (final OutputStream stream : streams) {
stream.write(b);
}
}

@Override
public void write(final byte[] b, final int off, final int len) throws IOException {
for (final OutputStream stream : streams) {
stream.write(b, off, len);
}
}

@Override
public void flush() throws IOException {
for (final OutputStream stream : streams) {
stream.flush();
}
}

@Override
public void close() throws IOException {
for (final OutputStream stream : streams) {
stream.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,38 @@

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

public class CommandExecutor {

public static int executeCommand(final CommandLine line, final Map<String, String> environmentVariables, final boolean verbose) {
public static int executeCommand(final CommandLine line, final Map<String, String> environmentVariables, final boolean verbose,
final File logFile) {
getLogger().debug("Execute CasperJS command [" + line + "], with env: " + environmentVariables);
FileOutputStream fos = null;
try {
final DefaultExecutor executor = new DefaultExecutor();
if (logFile != null) {
if (verbose) {
getLogger().info("Will duplicate output to: " + logFile.getAbsolutePath());
}
fos = new FileOutputStream(logFile);
executor.setStreamHandler(new PumpStreamHandler(new CloneOutputStream(System.out, fos)));
}
executor.setExitValues(new int[] { 0, 1 });
return executor.execute(line, environmentVariables);
} catch (final IOException e) {
if (fos != null) {
try {
fos.close();
} catch (final IOException ioex) {
// ignore
}
}
if (verbose) {
getLogger().error("Could not run CasperJS command", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.github.casperjs.casperjsrunner;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.IOException;
import java.io.OutputStream;

@RunWith(MockitoJUnitRunner.class)
public class CloneOutputStreamTest {

@Mock
private OutputStream out1;

@Mock
private OutputStream out2;

private CloneOutputStream stream;

@Before
public void initializeStream() {
stream = new CloneOutputStream(out1, out2);
}

@Test
public void testWriteInt() throws IOException {
final int b = 5;

stream.write(b);

verify(out1).write(b);
verify(out2).write(b);
verifyNoMoreInteractions(out1, out2);
}

@Test
public void testWriteByteArray() throws IOException {
final byte[] b = new byte[] { 1, 2, 3 };

stream.write(b);

verify(out1).write(b);
verify(out2).write(b);
verifyNoMoreInteractions(out1, out2);
}

@Test
public void testWriteByteArrayIntInt() throws IOException {
final byte[] b = new byte[] { -1, -2, -3 };
final int off = 4;
final int len = 3;

stream.write(b, off, len);

verify(out1).write(b, off, len);
verify(out2).write(b, off, len);
verifyNoMoreInteractions(out1, out2);
}

@Test
public void testFlush() throws IOException {
stream.flush();

verify(out1).flush();
verify(out2).flush();
verifyNoMoreInteractions(out1, out2);
}

@Test
public void testClose() throws IOException {
stream.close();

verify(out1).close();
verify(out2).close();
verifyNoMoreInteractions(out1, out2);
}

}

0 comments on commit 81d9ab7

Please sign in to comment.