From 4c737e3ba2db8400861aa74a7fc92e222497ba33 Mon Sep 17 00:00:00 2001 From: Kevin Dangoor Date: Thu, 20 Mar 2014 11:12:10 -0400 Subject: [PATCH] Fix for #7220: empty pref file is an empty object, not an error. This also fixes #7229 which was duped to #7220. --- src/preferences/PreferencesBase.js | 13 ++++++++---- .../PreferencesBase-test-files/empty.json | 0 test/spec/PreferencesBase-test.js | 20 +++++++++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 test/spec/PreferencesBase-test-files/empty.json diff --git a/src/preferences/PreferencesBase.js b/src/preferences/PreferencesBase.js index fe1e82b2c45..cb3b8f16754 100644 --- a/src/preferences/PreferencesBase.js +++ b/src/preferences/PreferencesBase.js @@ -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 { diff --git a/test/spec/PreferencesBase-test-files/empty.json b/test/spec/PreferencesBase-test-files/empty.json new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/spec/PreferencesBase-test.js b/test/spec/PreferencesBase-test.js index 209f9186c6e..fbc46acab1c 100644 --- a/test/spec/PreferencesBase-test.js +++ b/test/spec/PreferencesBase-test.js @@ -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; @@ -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(""); + }); + }); + }); }); }); });