Skip to content

Commit 514c4ba

Browse files
matthijskooijmancmaglie
authored andcommitted
CommandLineTest: Add runArduino helper
This abstracts some the common code (running Arduino, copying output, waiting for completion, checking result) from all testcases into a single method. This simplifies each testcase, but also prepares for adding more common arguments to all runs in a subsequent commit.
1 parent fc0478e commit 514c4ba

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

app/test/processing/app/CommandLineTest.java

+31-37
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
import static org.junit.Assert.*;
3333

3434
import java.io.File;
35+
import java.io.IOException;
36+
import java.util.List;
37+
import java.util.ArrayList;
38+
import java.util.Arrays;
3539

3640
import org.apache.commons.compress.utils.IOUtils;
3741
import org.fest.assertions.Assertions;
@@ -72,88 +76,78 @@ public static void findBuildPaths() throws Exception {
7276
System.out.println("found arduino: " + arduinoPath);
7377
}
7478

79+
public Process runArduino(boolean output, boolean success, File wd, String[] extraArgs) throws IOException, InterruptedException {
80+
Runtime rt = Runtime.getRuntime();
81+
82+
List<String> args = new ArrayList<String>();
83+
args.add(arduinoPath.getAbsolutePath());
84+
args.addAll(Arrays.asList(extraArgs));
85+
86+
Process pr = rt.exec(args.toArray(new String[0]), null, wd);
87+
if (output) {
88+
IOUtils.copy(pr.getInputStream(), System.out);
89+
IOUtils.copy(pr.getErrorStream(), System.out);
90+
}
91+
pr.waitFor();
92+
if (success)
93+
assertEquals(0, pr.exitValue());
94+
return pr;
95+
}
96+
7597
@Test
7698
public void testCommandLineBuildWithRelativePath() throws Exception {
77-
Runtime rt = Runtime.getRuntime();
7899
File wd = new File(buildPath, "build/shared/examples/01.Basics/Blink/");
79-
Process pr = rt
80-
.exec(arduinoPath + " --board arduino:avr:uno --verify Blink.ino", null,
81-
wd);
82-
IOUtils.copy(pr.getInputStream(), System.out);
83-
pr.waitFor();
84-
assertEquals(0, pr.exitValue());
100+
runArduino(true, true, wd, new String[] {
101+
"--board", "arduino:avr:uno",
102+
"--verify", "Blink.ino",
103+
});
85104
}
86105

87106
@Test
88107
public void testCommandLinePreferencesSave() throws Exception {
89-
Runtime rt = Runtime.getRuntime();
90108
File prefFile = File.createTempFile("test_pref", ".txt");
91109
prefFile.deleteOnExit();
92110

93-
Process pr = rt.exec(new String[] {
94-
arduinoPath.getAbsolutePath(),
111+
runArduino(true, true, null, new String[] {
95112
"--save-prefs",
96113
"--preferences-file", prefFile.getAbsolutePath(),
97114
"--get-pref", // avoids starting the GUI
98115
});
99-
IOUtils.copy(pr.getInputStream(), System.out);
100-
IOUtils.copy(pr.getErrorStream(), System.out);
101-
pr.waitFor();
102-
assertEquals(0, pr.exitValue());
103116

104-
pr = rt.exec(new String[] {
105-
arduinoPath.getAbsolutePath(),
117+
runArduino(true, true, null, new String[] {
106118
"--pref", "test_pref=xxx",
107119
"--preferences-file", prefFile.getAbsolutePath(),
108120
});
109-
IOUtils.copy(pr.getInputStream(), System.out);
110-
IOUtils.copy(pr.getErrorStream(), System.out);
111-
pr.waitFor();
112-
assertEquals(0, pr.exitValue());
113121

114122
PreferencesMap prefs = new PreferencesMap(prefFile);
115123
assertNull("preference should not be saved", prefs.get("test_pref"));
116124

117-
pr = rt.exec(new String[] {
118-
arduinoPath.getAbsolutePath(),
125+
runArduino(true, true, null, new String[] {
119126
"--pref", "test_pref=xxx",
120127
"--preferences-file", prefFile.getAbsolutePath(),
121128
"--save-prefs",
122129
});
123-
IOUtils.copy(pr.getInputStream(), System.out);
124-
IOUtils.copy(pr.getErrorStream(), System.out);
125-
pr.waitFor();
126-
assertEquals(0, pr.exitValue());
127130

128131
prefs = new PreferencesMap(prefFile);
129132
assertEquals("preference should be saved", "xxx", prefs.get("test_pref"));
130133
}
131134

132135
@Test
133136
public void testCommandLineVersion() throws Exception {
134-
Runtime rt = Runtime.getRuntime();
135-
Process pr = rt.exec(new String[]{
136-
arduinoPath.getAbsolutePath(),
137+
Process pr = runArduino(false, true, null, new String[] {
137138
"--version",
138139
});
139-
pr.waitFor();
140140

141-
Assertions.assertThat(pr.exitValue())
142-
.as("Process will finish with exit code 0 in --version")
143-
.isEqualTo(0);
144141
Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream())))
145142
.matches("Arduino: \\d+\\.\\d+\\.\\d+.*\r?\n");
146143
}
147144

148145
@Test
149146
public void testCommandLineMultipleAction() throws Exception {
150-
Runtime rt = Runtime.getRuntime();
151-
Process pr = rt.exec(new String[]{
152-
arduinoPath.getAbsolutePath(),
147+
Process pr = runArduino(true, false, null, new String[] {
153148
"--version",
154149
"--verify",
155150
});
156-
pr.waitFor();
157151

158152
Assertions.assertThat(pr.exitValue())
159153
.as("Multiple Action will be rejected")

0 commit comments

Comments
 (0)