Skip to content

Commit 3ae7e75

Browse files
Redirect warnings to stderr
1 parent a97d44d commit 3ae7e75

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

src/OutputParser.groovy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
class OutputParser {
3+
public def out
4+
public def err
5+
6+
OutputParser(out, err) {
7+
this.out = new OutStreamProcessor(out);
8+
this.err = err
9+
}
10+
11+
class OutStreamProcessor extends PrintStream {
12+
public OutStreamProcessor(out) {
13+
super(out);
14+
}
15+
16+
public void print(String line) {
17+
if(isJson(line)) {
18+
super.print(line)
19+
} else {
20+
this.err.print(line)
21+
}
22+
}
23+
24+
boolean isJson(txt) {
25+
return txt.startsWith("{");
26+
}
27+
}
28+
}

src/main.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import groovy.util.*
22

33
class Main {
44
static def execute(command) {
5+
OutputParser parser = new OutputParser(System.out, System.err)
56
ProcessBuilder builder = new ProcessBuilder(command.split(' '))
67
def env = builder.environment()
78
env.put("JAVA_OPTS", "-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=30")
89
Process process = builder.start()
9-
process.consumeProcessOutput(System.out, System.err)
10+
process.consumeProcessOutput(parser.out, parser.err)
1011
process.waitFor()
1112
System.exit(process.exitValue())
1213
}

test/OutputParserTest.groovy

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import static org.junit.Assert.*
2+
import org.junit.*
3+
4+
class OutputParserTest {
5+
def out
6+
def err
7+
def parser
8+
9+
@Before
10+
public void setup() {
11+
out = new ByteArrayOutputStream()
12+
err = new ByteArrayOutputStream()
13+
14+
def outStream = new PrintStream(out)
15+
def errStream = new PrintStream(err)
16+
17+
parser = new OutputParser(outStream, errStream)
18+
}
19+
20+
@Test
21+
public void redirectNonJsonInput() {
22+
parser.out.print("This is a warning")
23+
assert out.toString("UTF-8").isEmpty()
24+
assert "This is a warning", err.toString("UTF-8")
25+
}
26+
27+
@Test
28+
public void printJsonLines() {
29+
parser.out.print("{}")
30+
assert err.toString("UTF-8").isEmpty()
31+
assert "{}", out.toString("UTF-8")
32+
}
33+
}
34+

test/SanityCheckTest.groovy

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,29 @@ import groovy.util.FileNameFinder
44
import static org.junit.Assert.*
55
import org.junit.*
66

7+
class CustomIO {
8+
def byteStream
9+
def printStream
10+
11+
CustomIO() {
12+
byteStream = new ByteArrayOutputStream()
13+
printStream = new PrintStream(byteStream)
14+
}
15+
16+
public String toString() {
17+
return byteStream.toString("UTF-8")
18+
}
19+
}
20+
721
class SanityCheckTest {
822
def execute(command) {
923
def proc = command.execute()
10-
def out = new StringBuffer()
11-
def err = new StringBuffer()
24+
def outIO = new CustomIO()
25+
def errIO = new CustomIO()
1226

13-
proc.waitForProcessOutput(out, err)
27+
proc.waitForProcessOutput(outIO.printStream, errIO.printStream)
1428

15-
return [proc, out, err]
29+
return [proc, outIO, errIO]
1630
}
1731

1832
@Test

0 commit comments

Comments
 (0)