|
32 | 32 | import static org.junit.Assert.*; |
33 | 33 |
|
34 | 34 | import java.io.File; |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.List; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Arrays; |
35 | 39 |
|
36 | 40 | import org.apache.commons.compress.utils.IOUtils; |
37 | 41 | import org.fest.assertions.Assertions; |
@@ -72,88 +76,78 @@ public static void findBuildPaths() throws Exception { |
72 | 76 | System.out.println("found arduino: " + arduinoPath); |
73 | 77 | } |
74 | 78 |
|
| 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 | + |
75 | 97 | @Test |
76 | 98 | public void testCommandLineBuildWithRelativePath() throws Exception { |
77 | | - Runtime rt = Runtime.getRuntime(); |
78 | 99 | 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 | + }); |
85 | 104 | } |
86 | 105 |
|
87 | 106 | @Test |
88 | 107 | public void testCommandLinePreferencesSave() throws Exception { |
89 | | - Runtime rt = Runtime.getRuntime(); |
90 | 108 | File prefFile = File.createTempFile("test_pref", ".txt"); |
91 | 109 | prefFile.deleteOnExit(); |
92 | 110 |
|
93 | | - Process pr = rt.exec(new String[] { |
94 | | - arduinoPath.getAbsolutePath(), |
| 111 | + runArduino(true, true, null, new String[] { |
95 | 112 | "--save-prefs", |
96 | 113 | "--preferences-file", prefFile.getAbsolutePath(), |
97 | 114 | "--get-pref", // avoids starting the GUI |
98 | 115 | }); |
99 | | - IOUtils.copy(pr.getInputStream(), System.out); |
100 | | - IOUtils.copy(pr.getErrorStream(), System.out); |
101 | | - pr.waitFor(); |
102 | | - assertEquals(0, pr.exitValue()); |
103 | 116 |
|
104 | | - pr = rt.exec(new String[] { |
105 | | - arduinoPath.getAbsolutePath(), |
| 117 | + runArduino(true, true, null, new String[] { |
106 | 118 | "--pref", "test_pref=xxx", |
107 | 119 | "--preferences-file", prefFile.getAbsolutePath(), |
108 | 120 | }); |
109 | | - IOUtils.copy(pr.getInputStream(), System.out); |
110 | | - IOUtils.copy(pr.getErrorStream(), System.out); |
111 | | - pr.waitFor(); |
112 | | - assertEquals(0, pr.exitValue()); |
113 | 121 |
|
114 | 122 | PreferencesMap prefs = new PreferencesMap(prefFile); |
115 | 123 | assertNull("preference should not be saved", prefs.get("test_pref")); |
116 | 124 |
|
117 | | - pr = rt.exec(new String[] { |
118 | | - arduinoPath.getAbsolutePath(), |
| 125 | + runArduino(true, true, null, new String[] { |
119 | 126 | "--pref", "test_pref=xxx", |
120 | 127 | "--preferences-file", prefFile.getAbsolutePath(), |
121 | 128 | "--save-prefs", |
122 | 129 | }); |
123 | | - IOUtils.copy(pr.getInputStream(), System.out); |
124 | | - IOUtils.copy(pr.getErrorStream(), System.out); |
125 | | - pr.waitFor(); |
126 | | - assertEquals(0, pr.exitValue()); |
127 | 130 |
|
128 | 131 | prefs = new PreferencesMap(prefFile); |
129 | 132 | assertEquals("preference should be saved", "xxx", prefs.get("test_pref")); |
130 | 133 | } |
131 | 134 |
|
132 | 135 | @Test |
133 | 136 | 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[] { |
137 | 138 | "--version", |
138 | 139 | }); |
139 | | - pr.waitFor(); |
140 | 140 |
|
141 | | - Assertions.assertThat(pr.exitValue()) |
142 | | - .as("Process will finish with exit code 0 in --version") |
143 | | - .isEqualTo(0); |
144 | 141 | Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream()))) |
145 | 142 | .matches("Arduino: \\d+\\.\\d+\\.\\d+.*\r?\n"); |
146 | 143 | } |
147 | 144 |
|
148 | 145 | @Test |
149 | 146 | 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[] { |
153 | 148 | "--version", |
154 | 149 | "--verify", |
155 | 150 | }); |
156 | | - pr.waitFor(); |
157 | 151 |
|
158 | 152 | Assertions.assertThat(pr.exitValue()) |
159 | 153 | .as("Multiple Action will be rejected") |
|
0 commit comments