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

Commit

Permalink
Fix for #7220: empty pref file is an empty object, not an error.
Browse files Browse the repository at this point in the history
This also fixes #7229 which was duped to #7220.
  • Loading branch information
dangoor committed Mar 20, 2014
1 parent ee8d3bf commit 4c737e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/preferences/PreferencesBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,15 @@ define(function (require, exports, module) {

self._lineEndings = FileUtils.sniffLineEndings(text);

try {
result.resolve(JSON.parse(text));
} catch (e) {
result.reject(new ParsingError("Invalid JSON settings at " + path + "(" + e.toString() + ")"));
// If the file is empty, turn it into an empty object
if (/^\w*$/.test(text)) {
result.resolve({});
} else {
try {
result.resolve(JSON.parse(text));
} catch (e) {
result.reject(new ParsingError("Invalid JSON settings at " + path + "(" + e.toString() + ")"));
}
}
});
} else {
Expand Down
Empty file.
20 changes: 18 additions & 2 deletions test/spec/PreferencesBase-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1067,8 +1067,9 @@ define(function (require, exports, module) {
});

describe("File Storage", function () {
var settingsFile = FileSystem.getFileForPath(testPath + "/.brackets.json"),
newSettingsFile = FileSystem.getFileForPath(testPath + "/new.prefs"),
var settingsFile = FileSystem.getFileForPath(testPath + "/.brackets.json"),
newSettingsFile = FileSystem.getFileForPath(testPath + "/new.prefs"),
emptySettingsFile = FileSystem.getFileForPath(testPath + "/empty.json"),
filestorage,
originalText;

Expand Down Expand Up @@ -1207,6 +1208,21 @@ define(function (require, exports, module) {
}]);
});
});

it("is fine with empty preferences files", function () {
var filestorage = new PreferencesBase.FileStorage(emptySettingsFile.fullPath),
promise = filestorage.load();

waitsForDone(promise, "loading empty JSON file");
runs(function () {
promise.then(function (data) {
expect(data).toEqual({});
})
.fail(function (error) {
expect("There should have been no error").toEqual("");
});
});
});
});
});
});

0 comments on commit 4c737e3

Please sign in to comment.