From 514c4bae5746e5aaf32f5fcded4a627cf604ddd8 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 5 May 2020 21:49:32 +0200 Subject: [PATCH] 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. --- app/test/processing/app/CommandLineTest.java | 68 +++++++++----------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/app/test/processing/app/CommandLineTest.java b/app/test/processing/app/CommandLineTest.java index 70e8c8fd8..0a7ea064b 100644 --- a/app/test/processing/app/CommandLineTest.java +++ b/app/test/processing/app/CommandLineTest.java @@ -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 args = new ArrayList(); + 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")