2013-05-14 22:58:34 +04:00
|
|
|
fs = require 'fs'
|
2013-04-03 22:01:01 +04:00
|
|
|
fsUtils = require 'fs-utils'
|
2013-01-05 04:06:03 +04:00
|
|
|
|
2012-12-13 06:05:28 +04:00
|
|
|
describe "Config", ->
|
2013-05-02 22:35:13 +04:00
|
|
|
describe ".get(keyPath)", ->
|
|
|
|
it "allows a key path's value to be read", ->
|
2012-12-18 05:52:20 +04:00
|
|
|
expect(config.set("foo.bar.baz", 42)).toBe 42
|
|
|
|
expect(config.get("foo.bar.baz")).toBe 42
|
|
|
|
expect(config.get("bogus.key.path")).toBeUndefined()
|
|
|
|
|
2013-05-02 22:35:13 +04:00
|
|
|
describe ".set(keyPath, value)", ->
|
|
|
|
it "allows a key path's value to be written", ->
|
|
|
|
expect(config.set("foo.bar.baz", 42)).toBe 42
|
|
|
|
expect(config.get("foo.bar.baz")).toBe 42
|
|
|
|
|
2012-12-18 05:52:20 +04:00
|
|
|
it "updates observers and saves when a key path is set", ->
|
|
|
|
observeHandler = jasmine.createSpy "observeHandler"
|
|
|
|
config.observe "foo.bar.baz", observeHandler
|
|
|
|
observeHandler.reset()
|
|
|
|
|
|
|
|
config.set("foo.bar.baz", 42)
|
|
|
|
|
|
|
|
expect(config.save).toHaveBeenCalled()
|
|
|
|
expect(observeHandler).toHaveBeenCalledWith 42
|
|
|
|
|
2013-05-02 22:35:13 +04:00
|
|
|
describe "when the value equals the default value", ->
|
|
|
|
it "does not store the value", ->
|
|
|
|
config.setDefaults("foo", same: 1, changes: 1)
|
|
|
|
expect(config.settings.foo).toBeUndefined()
|
|
|
|
config.set('foo.same', 1)
|
|
|
|
config.set('foo.changes', 2)
|
|
|
|
expect(config.settings.foo).toEqual {changes: 2}
|
|
|
|
|
|
|
|
config.set('foo.changes', 1)
|
|
|
|
expect(config.settings.foo).toEqual {}
|
|
|
|
|
2013-05-01 09:29:48 +04:00
|
|
|
describe ".getPositiveInt(keyPath, defaultValue)", ->
|
|
|
|
it "returns the proper current or default value", ->
|
|
|
|
config.set('editor.preferredLineLength', 0)
|
|
|
|
expect(config.getPositiveInt('editor.preferredLineLength', 80)).toBe 80
|
|
|
|
config.set('editor.preferredLineLength', -1234)
|
|
|
|
expect(config.getPositiveInt('editor.preferredLineLength', 80)).toBe 80
|
|
|
|
config.set('editor.preferredLineLength', 'abcd')
|
|
|
|
expect(config.getPositiveInt('editor.preferredLineLength', 80)).toBe 80
|
|
|
|
config.set('editor.preferredLineLength', null)
|
|
|
|
expect(config.getPositiveInt('editor.preferredLineLength', 80)).toBe 80
|
|
|
|
|
2013-01-05 04:06:03 +04:00
|
|
|
describe ".save()", ->
|
|
|
|
beforeEach ->
|
2013-05-14 22:58:34 +04:00
|
|
|
spyOn(fs, 'writeFileSync')
|
2013-01-05 04:06:03 +04:00
|
|
|
jasmine.unspy config, 'save'
|
|
|
|
|
2013-02-05 00:38:00 +04:00
|
|
|
describe "when ~/.atom/config.json exists", ->
|
|
|
|
it "writes any non-default properties to ~/.atom/config.json", ->
|
2013-04-03 22:01:01 +04:00
|
|
|
config.configFilePath = fsUtils.join(config.configDirPath, "config.json")
|
2013-02-05 00:38:00 +04:00
|
|
|
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)
|
|
|
|
|
2013-05-14 22:58:34 +04:00
|
|
|
fs.writeFileSync.reset()
|
2013-02-05 00:38:00 +04:00
|
|
|
config.save()
|
|
|
|
|
2013-05-14 22:58:34 +04:00
|
|
|
expect(fs.writeFileSync.argsForCall[0][0]).toBe(fsUtils.join(config.configDirPath, "config.json"))
|
|
|
|
writtenConfig = JSON.parse(fs.writeFileSync.argsForCall[0][1])
|
2013-02-05 00:38:00 +04:00
|
|
|
expect(writtenConfig).toEqual config.settings
|
|
|
|
|
|
|
|
describe "when ~/.atom/config.json doesn't exist", ->
|
|
|
|
it "writes any non-default properties to ~/.atom/config.cson", ->
|
2013-04-03 22:01:01 +04:00
|
|
|
config.configFilePath = fsUtils.join(config.configDirPath, "config.cson")
|
2013-02-05 00:38:00 +04:00
|
|
|
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)
|
|
|
|
|
2013-05-14 22:58:34 +04:00
|
|
|
fs.writeFileSync.reset()
|
2013-02-05 00:38:00 +04:00
|
|
|
config.save()
|
|
|
|
|
2013-05-14 22:58:34 +04:00
|
|
|
expect(fs.writeFileSync.argsForCall[0][0]).toBe(fsUtils.join(config.configDirPath, "config.cson"))
|
2013-03-12 21:38:05 +04:00
|
|
|
CoffeeScript = require 'coffee-script'
|
2013-05-14 22:58:34 +04:00
|
|
|
writtenConfig = CoffeeScript.eval(fs.writeFileSync.argsForCall[0][1], bare: true)
|
2013-02-05 00:38:00 +04:00
|
|
|
expect(writtenConfig).toEqual config.settings
|
2013-01-05 04:06:03 +04:00
|
|
|
|
2012-12-18 07:56:28 +04:00
|
|
|
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)
|
2013-01-05 04:06:03 +04:00
|
|
|
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
|
2012-12-18 07:56:28 +04:00
|
|
|
|
|
|
|
config.setDefaults("foo.quux", x: 0, y: 1)
|
2013-01-05 04:06:03 +04:00
|
|
|
expect(config.get("foo.quux.x")).toBe 0
|
|
|
|
expect(config.get("foo.quux.y")).toBe 1
|
2012-12-18 07:56:28 +04:00
|
|
|
|
2012-12-18 05:52:20 +04:00
|
|
|
describe ".update()", ->
|
2012-12-18 07:56:28 +04:00
|
|
|
it "updates observers if a value is mutated without the use of .set", ->
|
2012-12-18 05:52:20 +04:00
|
|
|
config.set("foo.bar.baz", ["a"])
|
|
|
|
observeHandler = jasmine.createSpy "observeHandler"
|
|
|
|
config.observe "foo.bar.baz", observeHandler
|
|
|
|
observeHandler.reset()
|
|
|
|
|
|
|
|
config.get("foo.bar.baz").push("b")
|
|
|
|
config.update()
|
|
|
|
expect(observeHandler).toHaveBeenCalledWith config.get("foo.bar.baz")
|
2012-12-18 06:00:47 +04:00
|
|
|
observeHandler.reset()
|
|
|
|
|
|
|
|
config.update()
|
|
|
|
expect(observeHandler).not.toHaveBeenCalled()
|
2012-12-15 03:21:18 +04:00
|
|
|
|
|
|
|
describe ".observe(keyPath)", ->
|
2012-12-13 06:05:28 +04:00
|
|
|
observeHandler = null
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
observeHandler = jasmine.createSpy("observeHandler")
|
2012-12-18 06:00:47 +04:00
|
|
|
config.set("foo.bar.baz", "value 1")
|
2012-12-13 06:05:28 +04:00
|
|
|
config.observe "foo.bar.baz", observeHandler
|
|
|
|
|
|
|
|
it "fires the given callback with the current value at the keypath", ->
|
|
|
|
expect(observeHandler).toHaveBeenCalledWith("value 1")
|
|
|
|
|
|
|
|
it "fires the callback every time the observed value changes", ->
|
|
|
|
observeHandler.reset() # clear the initial call
|
2012-12-18 05:52:20 +04:00
|
|
|
config.set('foo.bar.baz', "value 2")
|
2012-12-13 06:05:28 +04:00
|
|
|
expect(observeHandler).toHaveBeenCalledWith("value 2")
|
|
|
|
observeHandler.reset()
|
|
|
|
|
2012-12-18 05:52:20 +04:00
|
|
|
config.set('foo.bar.baz', "value 1")
|
2012-12-13 06:05:28 +04:00
|
|
|
expect(observeHandler).toHaveBeenCalledWith("value 1")
|
|
|
|
|
2013-04-26 04:52:29 +04:00
|
|
|
it "fires the callback when the observed value is deleted", ->
|
|
|
|
observeHandler.reset() # clear the initial call
|
|
|
|
config.set('foo.bar.baz', undefined)
|
|
|
|
expect(observeHandler).toHaveBeenCalledWith(undefined)
|
|
|
|
|
2012-12-13 06:05:28 +04:00
|
|
|
it "fires the callback when the full key path goes into and out of existence", ->
|
|
|
|
observeHandler.reset() # clear the initial call
|
2012-12-18 05:52:20 +04:00
|
|
|
config.set("foo.bar", undefined)
|
2012-12-13 06:05:28 +04:00
|
|
|
|
|
|
|
expect(observeHandler).toHaveBeenCalledWith(undefined)
|
|
|
|
observeHandler.reset()
|
|
|
|
|
2012-12-18 05:52:20 +04:00
|
|
|
config.set("foo.bar.baz", "i'm back")
|
2012-12-13 06:05:28 +04:00
|
|
|
expect(observeHandler).toHaveBeenCalledWith("i'm back")
|
2013-02-23 02:35:13 +04:00
|
|
|
|
2013-04-09 06:00:55 +04:00
|
|
|
describe ".initializeConfigDirectory()", ->
|
2013-02-23 02:35:13 +04:00
|
|
|
beforeEach ->
|
|
|
|
config.configDirPath = '/tmp/dot-atom-dir'
|
2013-04-03 22:01:01 +04:00
|
|
|
expect(fsUtils.exists(config.configDirPath)).toBeFalsy()
|
2013-02-23 02:35:13 +04:00
|
|
|
|
|
|
|
afterEach ->
|
2013-04-03 22:01:01 +04:00
|
|
|
fsUtils.remove('/tmp/dot-atom-dir') if fsUtils.exists('/tmp/dot-atom-dir')
|
2013-02-23 02:35:13 +04:00
|
|
|
|
|
|
|
describe "when the configDirPath doesn't exist", ->
|
2013-02-23 02:44:57 +04:00
|
|
|
it "copies the contents of dot-atom to ~/.atom", ->
|
2013-04-24 05:13:49 +04:00
|
|
|
initializationDone = false
|
|
|
|
jasmine.unspy(window, "setTimeout")
|
|
|
|
config.initializeConfigDirectory ->
|
|
|
|
initializationDone = true
|
|
|
|
|
|
|
|
waitsFor -> initializationDone
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
expect(fsUtils.exists(config.configDirPath)).toBeTruthy()
|
|
|
|
expect(fsUtils.exists(fsUtils.join(config.configDirPath, 'packages'))).toBeTruthy()
|
|
|
|
expect(fsUtils.exists(fsUtils.join(config.configDirPath, 'snippets'))).toBeTruthy()
|
|
|
|
expect(fsUtils.exists(fsUtils.join(config.configDirPath, 'themes'))).toBeTruthy()
|
|
|
|
expect(fsUtils.isFile(fsUtils.join(config.configDirPath, 'config.cson'))).toBeTruthy()
|
2013-02-23 02:44:57 +04:00
|
|
|
|
|
|
|
it "copies the bundles themes to ~/.atom", ->
|
2013-04-24 05:13:49 +04:00
|
|
|
initializationDone = false
|
|
|
|
jasmine.unspy(window, "setTimeout")
|
|
|
|
config.initializeConfigDirectory ->
|
|
|
|
initializationDone = true
|
|
|
|
|
|
|
|
waitsFor -> initializationDone
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
expect(fsUtils.isFile(fsUtils.join(config.configDirPath, 'themes/atom-dark-ui/package.cson'))).toBeTruthy()
|
|
|
|
expect(fsUtils.isFile(fsUtils.join(config.configDirPath, 'themes/atom-light-ui/package.cson'))).toBeTruthy()
|
|
|
|
expect(fsUtils.isFile(fsUtils.join(config.configDirPath, 'themes/atom-dark-syntax.css'))).toBeTruthy()
|
|
|
|
expect(fsUtils.isFile(fsUtils.join(config.configDirPath, 'themes/atom-light-syntax.css'))).toBeTruthy()
|
2013-03-13 03:47:26 +04:00
|
|
|
|
2013-04-09 06:00:55 +04:00
|
|
|
describe ".loadUserConfig()", ->
|
2013-04-09 06:08:19 +04:00
|
|
|
beforeEach ->
|
|
|
|
config.configDirPath = '/tmp/dot-atom-dir'
|
|
|
|
config.configFilePath = fsUtils.join(config.configDirPath, "config.cson")
|
|
|
|
expect(fsUtils.exists(config.configDirPath)).toBeFalsy()
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
fsUtils.remove('/tmp/dot-atom-dir') if fsUtils.exists('/tmp/dot-atom-dir')
|
|
|
|
|
|
|
|
describe "when the config file contains valid cson", ->
|
2013-04-09 06:00:55 +04:00
|
|
|
beforeEach ->
|
2013-04-09 06:08:19 +04:00
|
|
|
fsUtils.write(config.configFilePath, "foo: bar: 'baz'")
|
2013-04-09 20:44:20 +04:00
|
|
|
config.loadUserConfig()
|
2013-04-09 06:00:55 +04:00
|
|
|
|
2013-04-09 06:08:19 +04:00
|
|
|
it "updates the config data based on the file contents", ->
|
|
|
|
expect(config.get("foo.bar")).toBe 'baz'
|
2013-04-09 06:00:55 +04:00
|
|
|
|
2013-04-09 06:08:19 +04:00
|
|
|
describe "when the config file contains invalid cson", ->
|
|
|
|
beforeEach ->
|
2013-04-09 06:00:55 +04:00
|
|
|
spyOn(console, 'error')
|
|
|
|
fsUtils.write(config.configFilePath, "{{{{{")
|
2013-04-09 06:08:19 +04:00
|
|
|
|
|
|
|
it "logs an error to the console and does not overwrite the config file on a subsequent save", ->
|
2013-04-09 06:00:55 +04:00
|
|
|
config.loadUserConfig()
|
|
|
|
expect(console.error).toHaveBeenCalled()
|
2013-04-09 20:44:20 +04:00
|
|
|
config.set("hair", "blonde") # trigger a save
|
2013-04-09 06:00:55 +04:00
|
|
|
expect(config.save).not.toHaveBeenCalled()
|
2013-04-09 20:44:20 +04:00
|
|
|
|
|
|
|
describe ".observeUserConfig()", ->
|
|
|
|
updatedHandler = null
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
config.configDirPath = '/tmp/dot-atom-dir'
|
|
|
|
config.configFilePath = fsUtils.join(config.configDirPath, "config.cson")
|
|
|
|
expect(fsUtils.exists(config.configDirPath)).toBeFalsy()
|
|
|
|
fsUtils.write(config.configFilePath, "foo: bar: 'baz'")
|
|
|
|
config.loadUserConfig()
|
|
|
|
config.observeUserConfig()
|
|
|
|
updatedHandler = jasmine.createSpy("updatedHandler")
|
|
|
|
config.on 'updated', updatedHandler
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
config.unobserveUserConfig()
|
|
|
|
fsUtils.remove('/tmp/dot-atom-dir') if fsUtils.exists('/tmp/dot-atom-dir')
|
|
|
|
|
|
|
|
describe "when the config file changes to contain valid cson", ->
|
|
|
|
it "updates the config data", ->
|
|
|
|
fsUtils.write(config.configFilePath, "foo: { bar: 'quux', baz: 'bar'}")
|
|
|
|
waitsFor 'update event', -> updatedHandler.callCount > 0
|
|
|
|
runs ->
|
|
|
|
expect(config.get('foo.bar')).toBe 'quux'
|
|
|
|
expect(config.get('foo.baz')).toBe 'bar'
|
|
|
|
|
|
|
|
describe "when the config file changes to contain invalid cson", ->
|
|
|
|
beforeEach ->
|
|
|
|
spyOn(console, 'error')
|
|
|
|
fsUtils.write(config.configFilePath, "}}}")
|
|
|
|
waitsFor "error to be logged", -> console.error.callCount > 0
|
|
|
|
|
|
|
|
it "logs a warning and does not update config data", ->
|
|
|
|
expect(updatedHandler.callCount).toBe 0
|
|
|
|
expect(config.get('foo.bar')).toBe 'baz'
|
|
|
|
config.set("hair", "blonde") # trigger a save
|
|
|
|
expect(config.save).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
describe "when the config file subsequently changes again to contain valid cson", ->
|
|
|
|
beforeEach ->
|
|
|
|
fsUtils.write(config.configFilePath, "foo: bar: 'baz'")
|
|
|
|
waitsFor 'update event', -> updatedHandler.callCount > 0
|
|
|
|
|
|
|
|
it "updates the config data and resumes saving", ->
|
|
|
|
config.set("hair", "blonde")
|
|
|
|
expect(config.save).toHaveBeenCalled()
|