Skip to content

Commit d041db5

Browse files
committed
create the settings folder on save when missing
found while smoke-testing the utils jar standalone: the PDE and the unit tests both pre-create the settings folder, but a standalone caller passing a new location would fail on the first save.
1 parent c6913d2 commit d041db5

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

app/utils/src/main/java/processing/utils/Preferences.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ static public void save() throws IOException {
324324
// prefs for the open sketch before init() has been called.
325325
if (preferencesFile != null) {
326326
File dir = preferencesFile.getParentFile();
327+
// standalone callers may pass a settings folder that doesn't exist yet
328+
if (!dir.exists() && !dir.mkdirs()) {
329+
throw new IOException("Could not create " + dir);
330+
}
327331
File preferencesTemp = File.createTempFile("preferences", ".txt", dir);
328332
if (!preferencesTemp.setWritable(true, false)) {
329333
throw new IOException("Could not set " + preferencesTemp + " writable");

app/utils/src/test/java/processing/utils/PreferencesTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@ public void testSaveWritesSortedKeys() throws IOException {
211211
}
212212

213213

214+
/**
215+
* A settings folder that doesn't exist yet is created on first save,
216+
* so standalone callers can point at any location.
217+
*/
218+
@Test
219+
public void testInitCreatesMissingSettingsFolder() throws IOException {
220+
var missing = new File(settingsFolder, "nested/settings");
221+
assertFalse(missing.exists());
222+
223+
Preferences.init(missing);
224+
assertTrue(new File(missing, "preferences.txt").exists());
225+
}
226+
227+
214228
/**
215229
* A 3.x sketchbook location is migrated to the 4.0 key on init,
216230
* and the migrated preference is written back out right away.

0 commit comments

Comments
 (0)