From b9b2b4bca2df747dcf78f9ef436831016c568feb Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 27 Jan 2015 15:34:44 -0800 Subject: [PATCH] =?UTF-8?q?Adhere=20to=20the=20schemas=20when=20loading=20?= =?UTF-8?q?the=20user=E2=80=99s=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/config-spec.coffee | 20 ++++++++++++++++++++ src/config.coffee | 13 ++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/spec/config-spec.coffee b/spec/config-spec.coffee index cd5e7eaca..6ced5e34f 100644 --- a/spec/config-spec.coffee +++ b/spec/config-spec.coffee @@ -699,6 +699,26 @@ describe "Config", -> expect(atom.config.get("foo.bar")).toBe 'baz' expect(atom.config.get("foo.bar", scope: ['.source.ruby'])).toBe 'more-specific' + describe "when the config file does not conform to the schema", -> + beforeEach -> + fs.writeFileSync atom.config.configFilePath, """ + '*': + foo: + bar: 'omg' + int: 'baz' + '.source.ruby': + foo: + bar: 'scoped' + int: 'nope' + """ + + it "validates and does not load the incorrect values", -> + atom.config.loadUserConfig() + expect(atom.config.get("foo.int")).toBe 12 + expect(atom.config.get("foo.bar")).toBe 'omg' + expect(atom.config.get("foo.int", scope: ['.source.ruby'])).toBe 12 + expect(atom.config.get("foo.bar", scope: ['.source.ruby'])).toBe 'scoped' + describe "when the config file contains valid cson", -> beforeEach -> fs.writeFileSync(atom.config.configFilePath, "foo: bar: 'baz'") diff --git a/src/config.coffee b/src/config.coffee index 1742cbd9f..83f33354c 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -1032,8 +1032,19 @@ class Config resetUserScopedSettings: (newScopedSettings) -> source = @getUserConfigPath() + priority = @priorityForSource(source) @scopedSettingsStore.removePropertiesForSource(source) - @scopedSettingsStore.addProperties(source, newScopedSettings, priority: @priorityForSource(source)) + + for scopeSelector, settings of newScopedSettings + validatedSettings = {} + try + settings = withoutEmptyObjects(@makeValueConformToSchema(null, settings)) + validatedSettings[scopeSelector] = settings + catch e + ; + + @scopedSettingsStore.addProperties(source, validatedSettings, {priority}) if validatedSettings[scopeSelector] + @emitChangeEvent() addScopedSettings: (source, selector, value, options) ->