diff --git a/app/test/processing/app/AbstractGUITest.java b/app/test/processing/app/AbstractGUITest.java index 913508856..37f0ebefe 100644 --- a/app/test/processing/app/AbstractGUITest.java +++ b/app/test/processing/app/AbstractGUITest.java @@ -60,7 +60,7 @@ public abstract class AbstractGUITest extends AbstractWithPreferencesTest { window = GuiActionRunner.execute(new GuiQuery() { @Override protected ArduinoFrameFixture executeInEDT() throws Throwable { - return new ArduinoFrameFixture(new Base(new String[0]).editors.get(0)); + return new ArduinoFrameFixture(createBase().editors.get(0)); } }); } diff --git a/app/test/processing/app/AbstractWithPreferencesTest.java b/app/test/processing/app/AbstractWithPreferencesTest.java index 10a690393..0045ee5fc 100644 --- a/app/test/processing/app/AbstractWithPreferencesTest.java +++ b/app/test/processing/app/AbstractWithPreferencesTest.java @@ -29,6 +29,7 @@ package processing.app; +import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.After; @@ -47,12 +48,30 @@ public abstract class AbstractWithPreferencesTest { * Subclasses can add files here in @Test or @Before functions. */ protected List deleteAfter = new LinkedList(); + protected File preferencesFile; @Before public void init() throws Exception { + File settingsDir = Files.createTempDirectory("arduino_test_settings").toFile(); + deleteAfter.add(settingsDir); + + preferencesFile = new File(settingsDir, "preferences.txt"); + File sketchbookDir = new File(settingsDir, "sketchbook"); + sketchbookDir.mkdir(); + BaseNoGui.initPlatform(); BaseNoGui.getPlatform().init(); - PreferencesData.init(null); + + PreferencesData.init(preferencesFile); + // Do not read anything from e.g. ~/.arduino15 + PreferencesData.set("settings.path", settingsDir.toString()); + // Do not read or write the default ~/Arduino sketchbook + PreferencesData.set("sketchbook.path", sketchbookDir.toString()); + // Write the defaults, with these changes to file. This allows them + // to be reloaded when creating a Base instance (see getBaseArgs() + // below). + PreferencesData.save(); + Theme.init(); BaseNoGui.initPackages(); @@ -61,6 +80,31 @@ public abstract class AbstractWithPreferencesTest { deleteAfter.add(Base.untitledFolder); } + /** + * Returns arguments to be passed to the Base constructor or on the + * commandline to set up the created dummy environment. + */ + protected String[] getBaseArgs() { + return new String[] { + // Preferences are loaded (using --preferences-file) before + // processing any other commandline options (e.g. --pref), so only + // use --preferences-file here. Also, this does not affect the + // "action" mode, for tests that require the GUI to be loaded. + "--preferences-file", preferencesFile.toString(), + }; + } + + /** + * Creates a new instance of Base. Always use this rather than calling + * it directly, to ensure the right settings are used. + */ + protected Base createBase() throws Exception { + Base base = new Base(getBaseArgs()); + // Doublecheck that the right preferencesFile was loaded + assertEquals(preferencesFile, PreferencesData.preferencesFile); + return base; + } + @After public void cleanup() throws IOException { for (File f : deleteAfter) diff --git a/app/test/processing/app/CommandLineTest.java b/app/test/processing/app/CommandLineTest.java index ddbc3bada..1db6afe12 100644 --- a/app/test/processing/app/CommandLineTest.java +++ b/app/test/processing/app/CommandLineTest.java @@ -45,7 +45,13 @@ import org.junit.Test; import processing.app.helpers.OSUtils; import processing.app.helpers.PreferencesMap; -public class CommandLineTest { +/** + * This extends AbstractWithPreferencesTest which initializes part of + * the internal Arduino structures. Most of that is not required, but it + * also conveniently sets up a settings directory and preferences file, + * which we can use here (through getBaseArgs()). + */ +public class CommandLineTest extends AbstractWithPreferencesTest { private static File buildPath; private static File arduinoPath; @@ -81,6 +87,7 @@ public class CommandLineTest { List args = new ArrayList(); args.add(arduinoPath.getAbsolutePath()); + args.addAll(Arrays.asList(getBaseArgs())); args.addAll(Arrays.asList(extraArgs)); System.out.println("Running: " + String.join(" ", args)); diff --git a/app/test/processing/app/DefaultTargetTest.java b/app/test/processing/app/DefaultTargetTest.java index 37819c84c..24767bee3 100644 --- a/app/test/processing/app/DefaultTargetTest.java +++ b/app/test/processing/app/DefaultTargetTest.java @@ -57,7 +57,7 @@ public class DefaultTargetTest extends AbstractWithPreferencesTest { PreferencesData.set("board", "unreal_board"); // should not raise an exception - new Base(new String[0]); + createBase(); // skip test if no target platforms are available Assume.assumeNotNull(BaseNoGui.getTargetPlatform());