Merge pull request #9131 from atom/mb-fix-config-unset

Fix setting default values in config
This commit is contained in:
Max Brunsfeld 2015-10-14 09:51:53 -07:00
commit 395ffde7a1
3 changed files with 42 additions and 17 deletions

View File

@ -31,7 +31,7 @@
"jasmine-json": "~0.0",
"jasmine-tagged": "^1.1.4",
"jquery": "^2.1.1",
"key-path-helpers": "^0.3.0",
"key-path-helpers": "^0.4.0",
"less-cache": "0.22",
"marked": "^0.3.4",
"normalize-package-data": "^2.0.0",

View File

@ -153,13 +153,27 @@ describe "Config", ->
describe "when the value equals the default value", ->
it "does not store the value in the user's config", ->
atom.config.setDefaults "foo",
same: 1
changes: 1
sameArray: [1, 2, 3]
sameObject: {a: 1, b: 2}
null: null
undefined: undefined
atom.config.setSchema "foo",
type: 'object'
properties:
same:
type: 'number'
default: 1
changes:
type: 'number'
default: 1
sameArray:
type: 'array'
default: [1, 2, 3]
sameObject:
type: 'object'
default: {a: 1, b: 2}
null:
type: '*'
default: null
undefined:
type: '*'
default: undefined
expect(atom.config.settings.foo).toBeUndefined()
atom.config.set('foo.same', 1)
@ -169,11 +183,15 @@ describe "Config", ->
atom.config.set('foo.undefined', null)
atom.config.set('foo.sameObject', {b: 2, a: 1})
expect(atom.config.get("foo.same", sources: [atom.config.getUserConfigPath()])).toBeUndefined()
userConfigPath = atom.config.getUserConfigPath()
expect(atom.config.get("foo.same", sources: [userConfigPath])).toBeUndefined()
expect(atom.config.get("foo.changes")).toBe 2
expect(atom.config.get("foo.changes", sources: [userConfigPath])).toBe 2
expect(atom.config.get("foo.changes", sources: [atom.config.getUserConfigPath()])).toBe 2
atom.config.set('foo.changes', 1)
expect(atom.config.get("foo.changes", sources: [atom.config.getUserConfigPath()])).toBeUndefined()
expect(atom.config.get("foo.changes", sources: [userConfigPath])).toBeUndefined()
describe "when a 'scopeSelector' is given", ->
it "sets the value and overrides the others", ->

View File

@ -5,7 +5,10 @@ CSON = require 'season'
path = require 'path'
async = require 'async'
pathWatcher = require 'pathwatcher'
{pushKeyPath, splitKeyPath, getValueAtKeyPath, setValueAtKeyPath} = require 'key-path-helpers'
{
getValueAtKeyPath, setValueAtKeyPath, deleteValueAtKeyPath,
pushKeyPath, splitKeyPath,
} = require 'key-path-helpers'
Color = require './color'
ScopedPropertyStore = require 'scoped-property-store'
@ -832,12 +835,16 @@ class Config
setRawValue: (keyPath, value) ->
defaultValue = getValueAtKeyPath(@defaultSettings, keyPath)
value = undefined if _.isEqual(defaultValue, value)
if keyPath?
setValueAtKeyPath(@settings, keyPath, value)
if _.isEqual(defaultValue, value)
if keyPath?
deleteValueAtKeyPath(@settings, keyPath)
else
@settings = null
else
@settings = value
if keyPath?
setValueAtKeyPath(@settings, keyPath, value)
else
@settings = value
@emitChangeEvent()
observeKeyPath: (keyPath, options, callback) ->