1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-21 15:54:39 +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 static org.junit.Assert.*;
import java.io.File; 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.apache.commons.compress.utils.IOUtils;
import org.fest.assertions.Assertions; import org.fest.assertions.Assertions;
@ -72,58 +76,57 @@ public class CommandLineTest {
System.out.println("found arduino: " + arduinoPath); 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 @Test
public void testCommandLineBuildWithRelativePath() throws Exception { public void testCommandLineBuildWithRelativePath() throws Exception {
Runtime rt = Runtime.getRuntime();
File wd = new File(buildPath, "build/shared/examples/01.Basics/Blink/"); File wd = new File(buildPath, "build/shared/examples/01.Basics/Blink/");
Process pr = rt runArduino(true, true, wd, new String[] {
.exec(arduinoPath + " --board arduino:avr:uno --verify Blink.ino", null, "--board", "arduino:avr:uno",
wd); "--verify", "Blink.ino",
IOUtils.copy(pr.getInputStream(), System.out); });
pr.waitFor();
assertEquals(0, pr.exitValue());
} }
@Test @Test
public void testCommandLinePreferencesSave() throws Exception { public void testCommandLinePreferencesSave() throws Exception {
Runtime rt = Runtime.getRuntime();
File prefFile = File.createTempFile("test_pref", ".txt"); File prefFile = File.createTempFile("test_pref", ".txt");
prefFile.deleteOnExit(); prefFile.deleteOnExit();
Process pr = rt.exec(new String[] { runArduino(true, true, null, new String[] {
arduinoPath.getAbsolutePath(),
"--save-prefs", "--save-prefs",
"--preferences-file", prefFile.getAbsolutePath(), "--preferences-file", prefFile.getAbsolutePath(),
"--get-pref", // avoids starting the GUI "--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[] { runArduino(true, true, null, new String[] {
arduinoPath.getAbsolutePath(),
"--pref", "test_pref=xxx", "--pref", "test_pref=xxx",
"--preferences-file", prefFile.getAbsolutePath(), "--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); PreferencesMap prefs = new PreferencesMap(prefFile);
assertNull("preference should not be saved", prefs.get("test_pref")); assertNull("preference should not be saved", prefs.get("test_pref"));
pr = rt.exec(new String[] { runArduino(true, true, null, new String[] {
arduinoPath.getAbsolutePath(),
"--pref", "test_pref=xxx", "--pref", "test_pref=xxx",
"--preferences-file", prefFile.getAbsolutePath(), "--preferences-file", prefFile.getAbsolutePath(),
"--save-prefs", "--save-prefs",
}); });
IOUtils.copy(pr.getInputStream(), System.out);
IOUtils.copy(pr.getErrorStream(), System.out);
pr.waitFor();
assertEquals(0, pr.exitValue());
prefs = new PreferencesMap(prefFile); prefs = new PreferencesMap(prefFile);
assertEquals("preference should be saved", "xxx", prefs.get("test_pref")); assertEquals("preference should be saved", "xxx", prefs.get("test_pref"));
@ -131,29 +134,20 @@ public class CommandLineTest {
@Test @Test
public void testCommandLineVersion() throws Exception { public void testCommandLineVersion() throws Exception {
Runtime rt = Runtime.getRuntime(); Process pr = runArduino(false, true, null, new String[] {
Process pr = rt.exec(new String[]{
arduinoPath.getAbsolutePath(),
"--version", "--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()))) Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream())))
.matches("Arduino: \\d+\\.\\d+\\.\\d+.*\r?\n"); .matches("Arduino: \\d+\\.\\d+\\.\\d+.*\r?\n");
} }
@Test @Test
public void testCommandLineMultipleAction() throws Exception { public void testCommandLineMultipleAction() throws Exception {
Runtime rt = Runtime.getRuntime(); Process pr = runArduino(true, false, null, new String[] {
Process pr = rt.exec(new String[]{
arduinoPath.getAbsolutePath(),
"--version", "--version",
"--verify", "--verify",
}); });
pr.waitFor();
Assertions.assertThat(pr.exitValue()) Assertions.assertThat(pr.exitValue())
.as("Multiple Action will be rejected") .as("Multiple Action will be rejected")