3232import static org .junit .Assert .assertEquals ;
3333import static org .junit .Assert .assertNull ;
3434
35+ import java .io .ByteArrayOutputStream ;
3536import java .io .File ;
3637import java .io .IOException ;
3738import java .io .InputStream ;
39+ import java .io .OutputStream ;
3840import java .util .List ;
3941import java .util .ArrayList ;
4042import java .util .Arrays ;
@@ -84,17 +86,17 @@ public static void findBuildPaths() throws Exception {
8486 System .out .println ("found arduino: " + arduinoPath );
8587 }
8688
87- private void consume (InputStream s ) {
89+ private void consume (InputStream s , OutputStream out ) {
8890 new Thread (() -> {
8991 try {
90- IOUtils .copy (s , System . out );
92+ IOUtils .copy (s , out );
9193 } catch (IOException e ) {
9294 e .printStackTrace ();
9395 }
9496 }).start ();
9597 }
9698
97- public Process runArduino (boolean output , boolean success , File wd , String [] extraArgs ) throws IOException , InterruptedException {
99+ public Process runArduino (OutputStream output , boolean success , File wd , String [] extraArgs ) throws IOException , InterruptedException {
98100 Runtime rt = Runtime .getRuntime ();
99101
100102 List <String > args = new ArrayList <>();
@@ -105,10 +107,8 @@ public Process runArduino(boolean output, boolean success, File wd, String[] ext
105107 System .out .println ("Running: " + String .join (" " , args ));
106108
107109 Process pr = rt .exec (args .toArray (new String [0 ]), null , wd );
108- if (output ) {
109- consume (pr .getInputStream ());
110- consume (pr .getErrorStream ());
111- }
110+ consume (pr .getInputStream (), output );
111+ consume (pr .getErrorStream (), System .err );
112112 pr .waitFor ();
113113 if (success )
114114 assertEquals (0 , pr .exitValue ());
@@ -118,7 +118,7 @@ public Process runArduino(boolean output, boolean success, File wd, String[] ext
118118 @ Test
119119 public void testCommandLineBuildWithRelativePath () throws Exception {
120120 File wd = new File (buildPath , "build/shared/examples/01.Basics/Blink/" );
121- runArduino (true , true , wd , new String [] {
121+ runArduino (System . out , true , wd , new String [] {
122122 "--board" , "arduino:avr:uno" ,
123123 "--verify" , "Blink.ino" ,
124124 });
@@ -129,21 +129,21 @@ public void testCommandLinePreferencesSave() throws Exception {
129129 File prefFile = File .createTempFile ("test_pref" , ".txt" );
130130 prefFile .deleteOnExit ();
131131
132- runArduino (true , true , null , new String [] {
132+ runArduino (System . out , true , null , new String [] {
133133 "--save-prefs" ,
134134 "--preferences-file" , prefFile .getAbsolutePath (),
135135 "--version" , // avoids starting the GUI
136136 });
137137
138- runArduino (true , true , null , new String [] {
138+ runArduino (System . out , true , null , new String [] {
139139 "--pref" , "test_pref=xxx" ,
140140 "--preferences-file" , prefFile .getAbsolutePath (),
141141 });
142142
143143 PreferencesMap prefs = new PreferencesMap (prefFile );
144144 assertNull ("preference should not be saved" , prefs .get ("test_pref" ));
145145
146- runArduino (true , true , null , new String [] {
146+ runArduino (System . out , true , null , new String [] {
147147 "--pref" , "test_pref=xxx" ,
148148 "--preferences-file" , prefFile .getAbsolutePath (),
149149 "--save-prefs" ,
@@ -155,17 +155,18 @@ public void testCommandLinePreferencesSave() throws Exception {
155155
156156 @ Test
157157 public void testCommandLineVersion () throws Exception {
158- Process pr = runArduino (false , true , null , new String [] {
158+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
159+ runArduino (out , true , null , new String [] {
159160 "--version" ,
160161 });
161162
162- Assertions .assertThat (new String ( IOUtils . toByteArray ( pr . getInputStream ()) ))
163- .matches ("Arduino: \\ d+\\ .\\ d+\\ .\\ d+.*\r ?\n " );
163+ Assertions .assertThat (out . toString ( ))
164+ .matches ("(?m) Arduino: \\ d+\\ .\\ d+\\ .\\ d+.*\r ?\n " );
164165 }
165166
166167 @ Test
167168 public void testCommandLineMultipleAction () throws Exception {
168- Process pr = runArduino (true , false , null , new String [] {
169+ Process pr = runArduino (System . out , false , null , new String [] {
169170 "--version" ,
170171 "--verify" ,
171172 });
0 commit comments