Skip to content

Commit 4b65f2c

Browse files
committed
keep a failed first-run save a warning, not a fatal error
loadUserPrefs() saved preferences.txt itself on a first run or after a 3.x sketchbook migration, so a failed save surfaced through the app's 'Error reading preferences' handler, which exits. On main that case is only a 'Could not save' warning. loadUserPrefs() now only reads and reports whether a save is needed. The standalone init() and the app's init() each do the save, so the app goes through its own save() and shows the warning again.
1 parent dd9b4e8 commit 4b65f2c

3 files changed

Lines changed: 59 additions & 11 deletions

File tree

‎app/src/processing/app/Preferences.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ static public void init() {
6060
}
6161

6262
// next load user preferences file over the defaults
63+
boolean needsSave = false;
6364
try {
64-
processing.utils.Preferences.loadUserPrefs(Base.getSettingsFolder());
65+
needsSave =
66+
processing.utils.Preferences.loadUserPrefs(Base.getSettingsFolder());
6567
} catch (IOException ex) {
6668
Messages.showError("Error reading preferences",
6769
"Error reading the preferences file. " +
@@ -70,6 +72,12 @@ static public void init() {
7072
" and restart Processing.", ex);
7173
}
7274

75+
if (needsSave) {
76+
// create a new preferences file if none exists
77+
// saves the defaults out to the file
78+
save();
79+
}
80+
7381
PApplet.useNativeSelect =
7482
Preferences.getBoolean("chooser.files.native"); //$NON-NLS-1$
7583

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ static public void init() throws IOException {
138138
*/
139139
static public void init(File settingsFolder) throws IOException {
140140
loadDefaults();
141-
loadUserPrefs(settingsFolder);
141+
if (loadUserPrefs(settingsFolder)) {
142+
// create a new preferences file if none exists
143+
// saves the defaults out to the file
144+
save();
145+
}
142146
}
143147

144148

@@ -167,13 +171,16 @@ static public void loadDefaults() throws IOException {
167171

168172

169173
/**
170-
* Second init stage: read the user's preferences.txt over the defaults,
171-
* and write the file out if it did not exist yet (or a 3.x sketchbook
172-
* location was migrated).
174+
* Second init stage: read the user's preferences.txt over the defaults.
175+
* Does not write anything. Saving is left to the caller so that a failed
176+
* read and a failed save stay separate errors: the app treats the first
177+
* as fatal and the second as a warning.
173178
* @param settingsFolder folder containing preferences.txt, or null to
174179
* resolve the standard location for this platform
180+
* @return true if the preferences should be saved right away, because
181+
* the file did not exist yet or a 3.x sketchbook location was migrated
175182
*/
176-
static public void loadUserPrefs(File settingsFolder) throws IOException {
183+
static public boolean loadUserPrefs(File settingsFolder) throws IOException {
177184
preferencesFile = resolvePreferencesFile(settingsFolder);
178185
boolean firstRun = !preferencesFile.exists();
179186
if (!firstRun) {
@@ -185,11 +192,7 @@ static public void loadUserPrefs(File settingsFolder) throws IOException {
185192
}
186193
}
187194

188-
if (checkSketchbookPref() || firstRun) {
189-
// create a new preferences file if none exists
190-
// saves the defaults out to the file
191-
save();
192-
}
195+
return checkSketchbookPref() || firstRun;
193196
}
194197

195198

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,43 @@ public void testUnreadablePreferencesFileThrows() throws IOException {
187187
}
188188

189189

190+
/**
191+
* Loading the user prefs never writes: on a first run it only reports
192+
* that a save is needed. A settings folder that can't be written to
193+
* therefore fails in save(), not in the load, and the preferences
194+
* stay usable from the defaults.
195+
*/
196+
@Test
197+
public void testLoadUserPrefsDoesNotSave() throws IOException {
198+
// a regular file where the parent folder should be, so the settings
199+
// folder can never be created (works on every platform, and as root)
200+
var notAFolder = new File(settingsFolder, "not-a-folder");
201+
Files.writeString(notAFolder.toPath(), "");
202+
var unwritable = new File(notAFolder, "settings");
203+
204+
Preferences.loadDefaults();
205+
assertTrue(Preferences.loadUserPrefs(unwritable));
206+
assertFalse(unwritable.exists());
207+
assertNotNull(Preferences.get("editor.tabs.size"));
208+
209+
assertThrows(IOException.class, Preferences::save);
210+
}
211+
212+
213+
/**
214+
* An existing preferences.txt with nothing to migrate needs no save.
215+
*/
216+
@Test
217+
public void testLoadUserPrefsReportsNoSaveNeeded() throws IOException {
218+
var file = new File(settingsFolder, "preferences.txt");
219+
Files.writeString(file.toPath(),
220+
"sketchbook.path.four=/tmp/sketchbook\n");
221+
222+
Preferences.loadDefaults();
223+
assertFalse(Preferences.loadUserPrefs(settingsFolder));
224+
}
225+
226+
190227
/**
191228
* preferences.txt is written with its keys sorted.
192229
*/

0 commit comments

Comments
 (0)