diff --git a/spec/app/config-spec.coffee b/spec/app/config-spec.coffee index 63dfac4ae..b42f140f5 100644 --- a/spec/app/config-spec.coffee +++ b/spec/app/config-spec.coffee @@ -1,3 +1,5 @@ +fs = require 'fs' + describe "Config", -> describe ".get(keyPath) and .set(keyPath, value)", -> it "allows a key path's value to be read and written", -> @@ -15,14 +17,33 @@ describe "Config", -> expect(config.save).toHaveBeenCalled() expect(observeHandler).toHaveBeenCalledWith 42 + describe ".save()", -> + beforeEach -> + spyOn(fs, 'write') + jasmine.unspy config, 'save' + + it "writes any non-default properties to the config.json in the user's .atom directory", -> + config.set("a.b.c", 1) + config.set("a.b.d", 2) + config.set("x.y.z", 3) + config.setDefaults("a.b", e: 4, f: 5) + + config.save() + + writtenConfig = JSON.parse(fs.write.argsForCall[0][1]) + expect(writtenConfig).toEqual config.settings + describe ".setDefaults(keyPath, defaults)", -> it "assigns any previously-unassigned keys to the object at the key path", -> config.set("foo.bar.baz", a: 1) config.setDefaults("foo.bar.baz", a: 2, b: 3, c: 4) - expect(config.get("foo.bar.baz")).toEqual(a: 1, b: 3, c: 4) + expect(config.get("foo.bar.baz.a")).toBe 1 + expect(config.get("foo.bar.baz.b")).toBe 3 + expect(config.get("foo.bar.baz.c")).toBe 4 config.setDefaults("foo.quux", x: 0, y: 1) - expect(config.get("foo.quux")).toEqual(x: 0, y: 1) + expect(config.get("foo.quux.x")).toBe 0 + expect(config.get("foo.quux.y")).toBe 1 describe ".update()", -> it "updates observers if a value is mutated without the use of .set", -> diff --git a/src/app/config.coffee b/src/app/config.coffee index fca996949..d006e7540 100644 --- a/src/app/config.coffee +++ b/src/app/config.coffee @@ -22,12 +22,14 @@ class Config configDirPath: configDirPath themeDirPaths: [userThemesDirPath, bundledThemesDirPath] packageDirPaths: [userPackagesDirPath, bundledVendorPackagesDirPath, bundledPackagesDirPath] + defaultSettings: null settings: null constructor: -> - @settings = + @defaultSettings = core: _.clone(require('root-view').configDefaults) editor: _.clone(require('editor').configDefaults) + @settings = {} load: -> @loadUserConfig() @@ -41,7 +43,8 @@ class Config _.extend(@settings, userConfig) get: (keyPath) -> - _.valueForKeyPath(@settings, keyPath) + _.valueForKeyPath(@settings, keyPath) ? + _.valueForKeyPath(@defaultSettings, keyPath) set: (keyPath, value) -> keys = keyPath.split('.') @@ -57,12 +60,12 @@ class Config setDefaults: (keyPath, defaults) -> keys = keyPath.split('.') - hash = @settings + hash = @defaultSettings for key in keys hash[key] ?= {} hash = hash[key] - _.defaults hash, defaults + _.extend hash, defaults @update() observe: (keyPath, callback) ->