Warn when loading bogus values from the user's config

This commit is contained in:
Ben Ogle
2014-09-24 17:13:01 -07:00
parent aa5b0ce41f
commit 0fc773c1fc
2 changed files with 52 additions and 1 deletions

View File

@@ -360,6 +360,38 @@ describe "Config", ->
expect(fs.existsSync(atom.config.configFilePath)).toBe true
expect(CSON.readFileSync(atom.config.configFilePath)).toEqual {}
describe "when a schema is specified", ->
beforeEach ->
schema =
type: 'object'
properties:
bar:
type: 'string'
default: 'def'
int:
type: 'integer'
default: 12
atom.config.setSchema('foo', schema)
describe "when the config file contains values that do not adhere to the schema", ->
warnSpy = null
beforeEach ->
warnSpy = spyOn console, 'warn'
fs.writeFileSync atom.config.configFilePath, """
foo:
bar: 'baz'
int: 'bad value'
"""
atom.config.loadUserConfig()
it "updates the config data based on the file contents", ->
expect(atom.config.get("foo.bar")).toBe 'baz'
expect(atom.config.get("foo.int")).toBe 12
expect(warnSpy).toHaveBeenCalled()
expect(warnSpy.mostRecentCall.args[0]).toContain "'foo.int' could not be set"
describe ".observeUserConfig()", ->
updatedHandler = null

View File

@@ -284,7 +284,7 @@ class Config
try
userConfig = CSON.readFileSync(@configFilePath)
_.extend(@settings, userConfig)
@setAllRecursive(userConfig)
@configFileHasErrors = false
@emit 'updated'
@emitter.emit 'did-change'
@@ -314,6 +314,22 @@ class Config
save: ->
CSON.writeFileSync(@configFilePath, @settings)
setAllRecursive: (value) ->
@setRecursive(key, childValue) for key, childValue of value
return
setRecursive: (keyPath, value) ->
if value? and isPlainObject(value)
keys = keyPath.split('.')
for key, childValue of value
continue unless value.hasOwnProperty(key)
@setRecursive(keys.concat([key]).join('.'), childValue)
else
unless @set(keyPath, value)
console.warn("'#{keyPath}' could not be set. Attempted value: #{JSON.stringify(value)}; Schema: #{JSON.stringify(@getSchema(keyPath))}")
return
setDefaults: (keyPath, defaults) ->
if typeof defaults isnt 'object'
return _.setValueForKeyPath(@defaultSettings, keyPath, defaults)
@@ -446,3 +462,6 @@ Config.addSchemaValidators
return value if _.isEqual(possibleValue, value)
throw new Error('Value is not one of the possible values')
isPlainObject = (value) ->
_.isObject(value) and not _.isArray(value) and not _.isFunction(value) and not _.isString(value)