32
32
import static org .junit .Assert .assertEquals ;
33
33
import static org .junit .Assert .assertNull ;
34
34
35
+ import java .io .ByteArrayOutputStream ;
35
36
import java .io .File ;
36
37
import java .io .IOException ;
37
38
import java .io .InputStream ;
39
+ import java .io .OutputStream ;
38
40
import java .util .List ;
39
41
import java .util .ArrayList ;
40
42
import java .util .Arrays ;
@@ -84,17 +86,17 @@ public static void findBuildPaths() throws Exception {
84
86
System .out .println ("found arduino: " + arduinoPath );
85
87
}
86
88
87
- private void consume (InputStream s ) {
89
+ private void consume (InputStream s , OutputStream out ) {
88
90
new Thread (() -> {
89
91
try {
90
- IOUtils .copy (s , System . out );
92
+ IOUtils .copy (s , out );
91
93
} catch (IOException e ) {
92
94
e .printStackTrace ();
93
95
}
94
96
}).start ();
95
97
}
96
98
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 {
98
100
Runtime rt = Runtime .getRuntime ();
99
101
100
102
List <String > args = new ArrayList <>();
@@ -105,10 +107,8 @@ public Process runArduino(boolean output, boolean success, File wd, String[] ext
105
107
System .out .println ("Running: " + String .join (" " , args ));
106
108
107
109
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 );
112
112
pr .waitFor ();
113
113
if (success )
114
114
assertEquals (0 , pr .exitValue ());
@@ -118,7 +118,7 @@ public Process runArduino(boolean output, boolean success, File wd, String[] ext
118
118
@ Test
119
119
public void testCommandLineBuildWithRelativePath () throws Exception {
120
120
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 [] {
122
122
"--board" , "arduino:avr:uno" ,
123
123
"--verify" , "Blink.ino" ,
124
124
});
@@ -129,21 +129,21 @@ public void testCommandLinePreferencesSave() throws Exception {
129
129
File prefFile = File .createTempFile ("test_pref" , ".txt" );
130
130
prefFile .deleteOnExit ();
131
131
132
- runArduino (true , true , null , new String [] {
132
+ runArduino (System . out , true , null , new String [] {
133
133
"--save-prefs" ,
134
134
"--preferences-file" , prefFile .getAbsolutePath (),
135
135
"--version" , // avoids starting the GUI
136
136
});
137
137
138
- runArduino (true , true , null , new String [] {
138
+ runArduino (System . out , true , null , new String [] {
139
139
"--pref" , "test_pref=xxx" ,
140
140
"--preferences-file" , prefFile .getAbsolutePath (),
141
141
});
142
142
143
143
PreferencesMap prefs = new PreferencesMap (prefFile );
144
144
assertNull ("preference should not be saved" , prefs .get ("test_pref" ));
145
145
146
- runArduino (true , true , null , new String [] {
146
+ runArduino (System . out , true , null , new String [] {
147
147
"--pref" , "test_pref=xxx" ,
148
148
"--preferences-file" , prefFile .getAbsolutePath (),
149
149
"--save-prefs" ,
@@ -155,17 +155,18 @@ public void testCommandLinePreferencesSave() throws Exception {
155
155
156
156
@ Test
157
157
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 [] {
159
160
"--version" ,
160
161
});
161
162
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 " );
164
165
}
165
166
166
167
@ Test
167
168
public void testCommandLineMultipleAction () throws Exception {
168
- Process pr = runArduino (true , false , null , new String [] {
169
+ Process pr = runArduino (System . out , false , null , new String [] {
169
170
"--version" ,
170
171
"--verify" ,
171
172
});
0 commit comments