Skip to content

Commit af8e1e6

Browse files
matthijskooijmancmaglie
authored andcommitted
Tests: Do not read system user's data
The tests would initialize Base, PreferencesData with default settings and/or run the arduino executable, without specifying any settings to use. In practice, this would read settings from e.g. `~/.arduino15`, load libraries from the user's sketchbook, etc. This is not a good idea, since this can be influence the test. For example, the presence of invalid libraries would cause extra output to be generated, which breaks the `--version` test. Or having the "Use external editor" setting set would break tests that try to edit a sketch. This commit fixes this. The core of this commit is in `AbstractWithPreferencesTest`, which sets up a clean settings dir (to replace `~/.arduino15`) with a sketchbook and `preferences.txt` file inside before every test (and removes it again after the test. For some tests, this is enough, but some tests create an instance of `Base`, which again initializes everything, including preferences, from the default location. To prevent that, `--preferences-file` is passed to the `Base` constructor, loading the previously set up preferences. This is handled by the new `AbstractWithPreferencesTest.createBase()` method. Furthermore, CommandLineTest calls the actual Arduino executable, which has the same problem. This is fixed by passing the same `--preferences-file` option on the commandline (generated by `AbstractWithPreferencesTest.getBaseArgs()`). This should prevent all tests from reading the the default settings files, fixing some tests on my system and even speeding up the tests somewhat (due less libraries and cores to load, probably).
1 parent 149aa52 commit af8e1e6

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

app/test/processing/app/AbstractGUITest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void startUpTheIDE() throws Exception {
6060
window = GuiActionRunner.execute(new GuiQuery<ArduinoFrameFixture>() {
6161
@Override
6262
protected ArduinoFrameFixture executeInEDT() throws Throwable {
63-
return new ArduinoFrameFixture(new Base(new String[0]).editors.get(0));
63+
return new ArduinoFrameFixture(createBase().editors.get(0));
6464
}
6565
});
6666
}

app/test/processing/app/AbstractWithPreferencesTest.java

+45-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package processing.app;
3131

32+
import static org.junit.Assert.assertEquals;
3233
import org.junit.Before;
3334
import org.junit.After;
3435

@@ -47,12 +48,30 @@ public abstract class AbstractWithPreferencesTest {
4748
* Subclasses can add files here in @Test or @Before functions.
4849
*/
4950
protected List<File> deleteAfter = new LinkedList<File>();
51+
protected File preferencesFile;
5052

5153
@Before
5254
public void init() throws Exception {
55+
File settingsDir = Files.createTempDirectory("arduino_test_settings").toFile();
56+
deleteAfter.add(settingsDir);
57+
58+
preferencesFile = new File(settingsDir, "preferences.txt");
59+
File sketchbookDir = new File(settingsDir, "sketchbook");
60+
sketchbookDir.mkdir();
61+
5362
BaseNoGui.initPlatform();
5463
BaseNoGui.getPlatform().init();
55-
PreferencesData.init(null);
64+
65+
PreferencesData.init(preferencesFile);
66+
// Do not read anything from e.g. ~/.arduino15
67+
PreferencesData.set("settings.path", settingsDir.toString());
68+
// Do not read or write the default ~/Arduino sketchbook
69+
PreferencesData.set("sketchbook.path", sketchbookDir.toString());
70+
// Write the defaults, with these changes to file. This allows them
71+
// to be reloaded when creating a Base instance (see getBaseArgs()
72+
// below).
73+
PreferencesData.save();
74+
5675
Theme.init();
5776

5877
BaseNoGui.initPackages();
@@ -61,6 +80,31 @@ public void init() throws Exception {
6180
deleteAfter.add(Base.untitledFolder);
6281
}
6382

83+
/**
84+
* Returns arguments to be passed to the Base constructor or on the
85+
* commandline to set up the created dummy environment.
86+
*/
87+
protected String[] getBaseArgs() {
88+
return new String[] {
89+
// Preferences are loaded (using --preferences-file) before
90+
// processing any other commandline options (e.g. --pref), so only
91+
// use --preferences-file here. Also, this does not affect the
92+
// "action" mode, for tests that require the GUI to be loaded.
93+
"--preferences-file", preferencesFile.toString(),
94+
};
95+
}
96+
97+
/**
98+
* Creates a new instance of Base. Always use this rather than calling
99+
* it directly, to ensure the right settings are used.
100+
*/
101+
protected Base createBase() throws Exception {
102+
Base base = new Base(getBaseArgs());
103+
// Doublecheck that the right preferencesFile was loaded
104+
assertEquals(preferencesFile, PreferencesData.preferencesFile);
105+
return base;
106+
}
107+
64108
@After
65109
public void cleanup() throws IOException {
66110
for (File f : deleteAfter)

app/test/processing/app/CommandLineTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@
4545
import processing.app.helpers.OSUtils;
4646
import processing.app.helpers.PreferencesMap;
4747

48-
public class CommandLineTest {
48+
/**
49+
* This extends AbstractWithPreferencesTest which initializes part of
50+
* the internal Arduino structures. Most of that is not required, but it
51+
* also conveniently sets up a settings directory and preferences file,
52+
* which we can use here (through getBaseArgs()).
53+
*/
54+
public class CommandLineTest extends AbstractWithPreferencesTest {
4955

5056
private static File buildPath;
5157
private static File arduinoPath;
@@ -81,6 +87,7 @@ public Process runArduino(boolean output, boolean success, File wd, String[] ext
8187

8288
List<String> args = new ArrayList<String>();
8389
args.add(arduinoPath.getAbsolutePath());
90+
args.addAll(Arrays.asList(getBaseArgs()));
8491
args.addAll(Arrays.asList(extraArgs));
8592

8693
System.out.println("Running: " + String.join(" ", args));

app/test/processing/app/DefaultTargetTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testDefaultTarget() throws Exception {
5757
PreferencesData.set("board", "unreal_board");
5858

5959
// should not raise an exception
60-
new Base(new String[0]);
60+
createBase();
6161

6262
// skip test if no target platforms are available
6363
Assume.assumeNotNull(BaseNoGui.getTargetPlatform());

0 commit comments

Comments
 (0)