1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-20 14:54:31 +01:00

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.
This commit is contained in:
Matthijs Kooijman 2020-05-05 21:49:32 +02:00 committed by Cristian Maglie
parent fc0478eaa9
commit 514c4bae57

View File

@ -32,6 +32,10 @@ package processing.app;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import org.apache.commons.compress.utils.IOUtils;
import org.fest.assertions.Assertions;
@ -72,58 +76,57 @@ public class CommandLineTest {
System.out.println("found arduino: " + arduinoPath);
}
public Process runArduino(boolean output, boolean success, File wd, String[] extraArgs) throws IOException, InterruptedException {
Runtime rt = Runtime.getRuntime();
List<String> args = new ArrayList<String>();
args.add(arduinoPath.getAbsolutePath());
args.addAll(Arrays.asList(extraArgs));
Process pr = rt.exec(args.toArray(new String[0]), null, wd);
if (output) {
IOUtils.copy(pr.getInputStream(), System.out);
IOUtils.copy(pr.getErrorStream(), System.out);
}
pr.waitFor();
if (success)
assertEquals(0, pr.exitValue());
return pr;
}
@Test
public void testCommandLineBuildWithRelativePath() throws Exception {
Runtime rt = Runtime.getRuntime();
File wd = new File(buildPath, "build/shared/examples/01.Basics/Blink/");
Process pr = rt
.exec(arduinoPath + " --board arduino:avr:uno --verify Blink.ino", null,
wd);
IOUtils.copy(pr.getInputStream(), System.out);
pr.waitFor();
assertEquals(0, pr.exitValue());
runArduino(true, true, wd, new String[] {
"--board", "arduino:avr:uno",
"--verify", "Blink.ino",
});
}
@Test
public void testCommandLinePreferencesSave() throws Exception {
Runtime rt = Runtime.getRuntime();
File prefFile = File.createTempFile("test_pref", ".txt");
prefFile.deleteOnExit();
Process pr = rt.exec(new String[] {
arduinoPath.getAbsolutePath(),
runArduino(true, true, null, new String[] {
"--save-prefs",
"--preferences-file", prefFile.getAbsolutePath(),
"--get-pref", // avoids starting the GUI
});
IOUtils.copy(pr.getInputStream(), System.out);
IOUtils.copy(pr.getErrorStream(), System.out);
pr.waitFor();
assertEquals(0, pr.exitValue());
pr = rt.exec(new String[] {
arduinoPath.getAbsolutePath(),
runArduino(true, true, null, new String[] {
"--pref", "test_pref=xxx",
"--preferences-file", prefFile.getAbsolutePath(),
});
IOUtils.copy(pr.getInputStream(), System.out);
IOUtils.copy(pr.getErrorStream(), System.out);
pr.waitFor();
assertEquals(0, pr.exitValue());
PreferencesMap prefs = new PreferencesMap(prefFile);
assertNull("preference should not be saved", prefs.get("test_pref"));
pr = rt.exec(new String[] {
arduinoPath.getAbsolutePath(),
runArduino(true, true, null, new String[] {
"--pref", "test_pref=xxx",
"--preferences-file", prefFile.getAbsolutePath(),
"--save-prefs",
});
IOUtils.copy(pr.getInputStream(), System.out);
IOUtils.copy(pr.getErrorStream(), System.out);
pr.waitFor();
assertEquals(0, pr.exitValue());
prefs = new PreferencesMap(prefFile);
assertEquals("preference should be saved", "xxx", prefs.get("test_pref"));
@ -131,29 +134,20 @@ public class CommandLineTest {
@Test
public void testCommandLineVersion() throws Exception {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(new String[]{
arduinoPath.getAbsolutePath(),
Process pr = runArduino(false, true, null, new String[] {
"--version",
});
pr.waitFor();
Assertions.assertThat(pr.exitValue())
.as("Process will finish with exit code 0 in --version")
.isEqualTo(0);
Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream())))
.matches("Arduino: \\d+\\.\\d+\\.\\d+.*\r?\n");
}
@Test
public void testCommandLineMultipleAction() throws Exception {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(new String[]{
arduinoPath.getAbsolutePath(),
Process pr = runArduino(true, false, null, new String[] {
"--version",
"--verify",
});
pr.waitFor();
Assertions.assertThat(pr.exitValue())
.as("Multiple Action will be rejected")