File tree Expand file tree Collapse file tree 4 files changed +82
-5
lines changed Expand file tree Collapse file tree 4 files changed +82
-5
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -2,11 +2,12 @@ import groovy.util.*
2
2
3
3
class Main {
4
4
static def execute (command ) {
5
+ OutputParser parser = new OutputParser (System . out, System . err)
5
6
ProcessBuilder builder = new ProcessBuilder (command. split(' ' ))
6
7
def env = builder. environment()
7
8
env. put(" JAVA_OPTS" , " -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=30" )
8
9
Process process = builder. start()
9
- process. consumeProcessOutput(System . out, System . err)
10
+ process. consumeProcessOutput(parser . out, parser . err)
10
11
process. waitFor()
11
12
System . exit(process. exitValue())
12
13
}
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change @@ -4,15 +4,29 @@ import groovy.util.FileNameFinder
4
4
import static org.junit.Assert.*
5
5
import org.junit.*
6
6
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
+
7
21
class SanityCheckTest {
8
22
def execute (command ) {
9
23
def proc = command. execute()
10
- def out = new StringBuffer ()
11
- def err = new StringBuffer ()
24
+ def outIO = new CustomIO ()
25
+ def errIO = new CustomIO ()
12
26
13
- proc. waitForProcessOutput(out, err )
27
+ proc. waitForProcessOutput(outIO . printStream, errIO . printStream )
14
28
15
- return [proc, out, err ]
29
+ return [proc, outIO, errIO ]
16
30
}
17
31
18
32
@Test
You can’t perform that action at this time.
0 commit comments