Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit e08abaf

Browse files
committed
Merge pull request #7263 from adobe/dangoor/7220-empty-prefs
Empty pref file is an empty object, not an error.
2 parents 28b51c9 + 8a33a5d commit e08abaf

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/preferences/PreferencesBase.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,15 @@ define(function (require, exports, module) {
175175

176176
self._lineEndings = FileUtils.sniffLineEndings(text);
177177

178-
try {
179-
result.resolve(JSON.parse(text));
180-
} catch (e) {
181-
result.reject(new ParsingError("Invalid JSON settings at " + path + "(" + e.toString() + ")"));
178+
// If the file is empty, turn it into an empty object
179+
if (/^\s*$/.test(text)) {
180+
result.resolve({});
181+
} else {
182+
try {
183+
result.resolve(JSON.parse(text));
184+
} catch (e) {
185+
result.reject(new ParsingError("Invalid JSON settings at " + path + "(" + e.toString() + ")"));
186+
}
182187
}
183188
});
184189
} else {

test/spec/PreferencesBase-test-files/empty.json

Whitespace-only changes.

test/spec/PreferencesBase-test.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,8 +1067,9 @@ define(function (require, exports, module) {
10671067
});
10681068

10691069
describe("File Storage", function () {
1070-
var settingsFile = FileSystem.getFileForPath(testPath + "/.brackets.json"),
1071-
newSettingsFile = FileSystem.getFileForPath(testPath + "/new.prefs"),
1070+
var settingsFile = FileSystem.getFileForPath(testPath + "/.brackets.json"),
1071+
newSettingsFile = FileSystem.getFileForPath(testPath + "/new.prefs"),
1072+
emptySettingsFile = FileSystem.getFileForPath(testPath + "/empty.json"),
10721073
filestorage,
10731074
originalText;
10741075

@@ -1207,6 +1208,18 @@ define(function (require, exports, module) {
12071208
}]);
12081209
});
12091210
});
1211+
1212+
it("is fine with empty preferences files", function () {
1213+
var filestorage = new PreferencesBase.FileStorage(emptySettingsFile.fullPath),
1214+
promise = filestorage.load();
1215+
1216+
waitsForDone(promise, "loading empty JSON file");
1217+
runs(function () {
1218+
promise.then(function (data) {
1219+
expect(data).toEqual({});
1220+
});
1221+
});
1222+
});
12101223
});
12111224
});
12121225
});

0 commit comments

Comments
 (0)