From 79094ee889c52385ea0c2e847ddb5fdb271b0544 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 27 Jan 2015 15:09:19 -0800 Subject: [PATCH] Reset all user config values when the schema changes. --- spec/config-spec.coffee | 38 ++++++++++++++++++++++++++++++++++++++ src/config.coffee | 15 +++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/spec/config-spec.coffee b/spec/config-spec.coffee index 9a570fda4..cd5e7eaca 100644 --- a/spec/config-spec.coffee +++ b/spec/config-spec.coffee @@ -1106,6 +1106,44 @@ describe "Config", -> expect(atom.config.get('foo.bar.str', scope: ['.source.js'])).toBe 'omg' expect(atom.config.get('foo.bar.str', scope: ['.source.coffee'])).toBe 'ok' + describe 'when a schema is added after config values have been set', -> + schema = null + beforeEach -> + schema = + type: 'object' + properties: + int: + type: 'integer' + default: 2 + + it 'the values set respect the new schema', -> + expect(atom.config.set('foo.bar.int', 'nope')).toBe true + expect(atom.config.set('foo.bar.int', 'notanint', scopeSelector: '.source.js')).toBe true + expect(atom.config.set('foo.bar.int', 23, scopeSelector: '.source.coffee')).toBe true + expect(atom.config.get('foo.bar.int')).toBe 'nope' + expect(atom.config.get('foo.bar.int', scope: ['.source.js'])).toBe 'notanint' + expect(atom.config.get('foo.bar.int', scope: ['.source.coffee'])).toBe 23 + + atom.config.setSchema('foo.bar', schema) + + expect(atom.config.get('foo.bar.int')).toBe 2 + expect(atom.config.get('foo.bar.int', scope: ['.source.js'])).toBe 2 + expect(atom.config.get('foo.bar.int', scope: ['.source.coffee'])).toBe 23 + + it 'doesnt mess it up', -> + expect(atom.config.set('foo.bar.int', 10)).toBe true + expect(atom.config.set('foo.bar.int', 15, scopeSelector: '.source.js')).toBe true + expect(atom.config.set('foo.bar.int', 23, scopeSelector: '.source.coffee')).toBe true + expect(atom.config.get('foo.bar.int')).toBe 10 + expect(atom.config.get('foo.bar.int', scope: ['.source.js'])).toBe 15 + expect(atom.config.get('foo.bar.int', scope: ['.source.coffee'])).toBe 23 + + atom.config.setSchema('foo.bar', schema) + + expect(atom.config.get('foo.bar.int')).toBe 10 + expect(atom.config.get('foo.bar.int', scope: ['.source.js'])).toBe 15 + expect(atom.config.get('foo.bar.int', scope: ['.source.coffee'])).toBe 23 + describe 'when the value has an "integer" type', -> beforeEach -> schema = diff --git a/src/config.coffee b/src/config.coffee index 6e6409309..1742cbd9f 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -797,6 +797,7 @@ class Config _.extend rootSchema, schema @setDefaults(keyPath, @extractDefaultsFromSchema(schema)) @setScopedDefaultsFromSchema(keyPath, schema) + @resetForSchemaChange() load: -> @initializeConfigDirectory() @@ -1002,6 +1003,20 @@ class Config value = @constructor.executeSchemaEnforcers(keyPath, value, schema) if schema = @getSchema(keyPath) value + # When the schema is changed / added, there may be values set in the config + # that do not conform to the config. This will reset make them conform. + resetForSchemaChange: (source=@getUserConfigPath()) -> + settings = @settings + @settings = null + @set(null, settings, {save: false}) + + priority = @priorityForSource(source) + selectorsAndSettings = @scopedSettingsStore.propertiesForSource(source) + @scopedSettingsStore.removePropertiesForSource(source) + for scopeSelector, settings of selectorsAndSettings + @set(null, settings, {scopeSelector, source, priority, save: false}) if settings? + return + ### Section: Private Scoped Settings ###