Skip to content

Commit

Permalink
Improve PantsRunnerTest setup error message.
Browse files Browse the repository at this point in the history
This is in support of debugging OSX CI failures under Jenkins but is
also generally more friendly.

Testing Done:
Verified the capture code worked with:
```diff
$ git diff -U1
diff --git a/tests/java/org/pantsbuild/tools/runner/PantsRunnerTest.java b/tests/java/org/pantsbuild/tools/runner/PantsRunnerTest.java
index 864f096..271c4ab 100644
--- a/tests/java/org/pantsbuild/tools/runner/PantsRunnerTest.java
+++ b/tests/java/org/pantsbuild/tools/runner/PantsRunnerTest.java
@@ -83,3 +83,3 @@ public class PantsRunnerTest {
             stderr.readAll()),
-        0,
+        42,
         result);
```

Yielding:
```
$ ./pants test tests/java/org/pantsbuild/tools/runner:jar
...

16:00:23 00:03     [junit]
16:00:23 00:03       [run]
                     OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
                     OpenJDK 64-Bit Server VM warning: ignoring option UseSplitVerifier; support was removed in 8.0
                     Auto-detected 8 processors, using -parallel-threads=8
                     F
                     Time: 3.73
                     There was 1 failure:
                     1) org.pantsbuild.tools.runner.PantsRunnerTest
                     java.lang.AssertionError: Problem running ./pants bundle testprojects/src/java/org/pantsbuild/testproject/runner:pants-runner-testproject - exited with 0:
                     STDOUT:

                     16:00:26 00:00 [main]
                                    (To run a reporting server: ./pants server)
                     16:00:26 00:00   [setup]
                     16:00:26 00:00     [parse]
                                    Executing tasks in goals: bootstrap -> imports -> unpack-jars -> deferred-sources -> gen -> jvm-platform-validate -> resolve -> resources -> compile -> binary -> apk -> sign -> bundle
                     16:00:26 00:00   [bootstrap]
                     16:00:26 00:00     [substitute-aliased-targets]
                     16:00:26 00:00     [jar-dependency-management]
                     16:00:26 00:00     [bootstrap-jvm-tools]
                     16:00:26 00:00     [provide-tools-jar]
...
                     16:00:27 00:01   [complete]
                                    SUCCESS

                     STDERR:
                      expected:<42> but was:<0>
                     	at org.junit.Assert.fail(Assert.java:88)
                     	at org.junit.Assert.failNotEquals(Assert.java:834)
                     	at org.junit.Assert.assertEquals(Assert.java:645)
                     	at org.pantsbuild.tools.runner.PantsRunnerTest.setUpClass(PantsRunnerTest.java:77)
...
```

CI went green here:
  http://jenkins.pantsbuild.org/job/pantsbuild/job/pants/job/PR-3616/10/

Bugs closed: 3615, 3616

Reviewed at https://rbcommons.com/s/twitter/r/4038/
  • Loading branch information
jsirois committed Jul 1, 2016
1 parent dedb99b commit 6ac25c8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
2 changes: 2 additions & 0 deletions tests/java/org/pantsbuild/tools/runner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ junit_tests(
name='jar',
sources=globs('*.java'),
dependencies=[
'3rdparty:guava',
'3rdparty:jsr305',
'3rdparty:junit',
'src/java/org/pantsbuild/tools/runner:runner-library',
],
Expand Down
65 changes: 61 additions & 4 deletions tests/java/org/pantsbuild/tools/runner/PantsRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@

package org.pantsbuild.tools.runner;

import java.io.*;
import java.lang.String;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.concurrent.atomic.AtomicReference;

import javax.annotation.Nullable;

import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.google.common.io.Closeables;

import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -28,9 +40,54 @@ public class PantsRunnerTest {
"testprojects.src.java.org.pantsbuild.testproject.runner.main-class-0.jar");
static final String MAIN_CLASS = "org.pantsbuild.testproject.runner.MainClass";

static class StreamReader extends Thread {
static StreamReader start(InputStream stream) {
StreamReader reader = new StreamReader(stream);
reader.start();
return reader;
}

private final Reader reader;
@Nullable private String capturedData;

private StreamReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream, Charsets.UTF_8));
}

@Override
public void run() {
try {
capturedData = CharStreams.toString(reader);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
Closeables.closeQuietly(reader);
}
}

@Nullable
String readAll() throws InterruptedException {
join();
return capturedData;
}
}

@BeforeClass
public static void setup() throws Exception {
assertEquals(0, Runtime.getRuntime().exec("./pants bundle " + TEST_PROJECT).waitFor());
public static void setUpClass() throws Exception {
String command = "./pants bundle " + TEST_PROJECT;
Process process = Runtime.getRuntime().exec(command);
StreamReader stdout = StreamReader.start(process.getInputStream());
StreamReader stderr = StreamReader.start(process.getErrorStream());
int result = process.waitFor();
assertEquals(
String.format(
"Problem running %s - exited with %d:\nSTDOUT:\n%s\n\nSTDERR:\n%s",
command,
result,
stdout.readAll(),
stderr.readAll()),
0,
result);
assertTrue(SYNTHETIC_JAR.exists());
assertTrue(MAIN_JAR.exists());
}
Expand Down

0 comments on commit 6ac25c8

Please sign in to comment.