* A rather disruptive change but for a few reasons such as easier to read,
easier type, keep consistent and javascript code uses single quotes.
* There are a few exceptions for the automated process:
* Any double quotes in comments
* Triple double quotes for docstrings
* Strings containing single quotes are left e.g. "they're"
* To deal with merge conflicts from feature branches it is best to follow
these steps for each commit:
* Create a patch: `git format-patch -1 <sha1>`
* Edit the patch and replace double quotes with single except those in
comments or strings containing an unescaped apostrophe.
* Check the patch `git apply --check <patchfile>` and fix any remaining
issues if it outputs an error.
* Apply the patch `git am < <patchfile>`
169 lines
5.2 KiB
Python
169 lines
5.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
|
|
from twisted.internet import task
|
|
from twisted.trial import unittest
|
|
|
|
import deluge.config
|
|
from deluge.config import Config
|
|
|
|
from .common import set_tmp_config_dir
|
|
|
|
DEFAULTS = {'string': 'foobar', 'int': 1, 'float': 0.435, 'bool': True, 'unicode': u'foobar'}
|
|
|
|
|
|
class ConfigTestCase(unittest.TestCase):
|
|
def setUp(self): # NOQA
|
|
self.config_dir = set_tmp_config_dir()
|
|
|
|
def test_init(self):
|
|
config = Config('test.conf', defaults=DEFAULTS, config_dir=self.config_dir)
|
|
self.assertEquals(DEFAULTS, config.config)
|
|
|
|
config = Config('test.conf', config_dir=self.config_dir)
|
|
self.assertEquals({}, config.config)
|
|
|
|
def test_set_get_item(self):
|
|
config = Config('test.conf', config_dir=self.config_dir)
|
|
config['foo'] = 1
|
|
self.assertEquals(config['foo'], 1)
|
|
self.assertRaises(ValueError, config.set_item, 'foo', 'bar')
|
|
|
|
config['foo'] = 2
|
|
self.assertEquals(config.get_item('foo'), 2)
|
|
|
|
config['foo'] = '3'
|
|
self.assertEquals(config.get_item('foo'), 3)
|
|
|
|
config['unicode'] = u'ВИДЕОФИЛЬМЫ'
|
|
self.assertEquals(config['unicode'], u'ВИДЕОФИЛЬМЫ')
|
|
|
|
config['unicode'] = 'foostring'
|
|
self.assertTrue(isinstance(config.get_item('unicode'), unicode))
|
|
|
|
config._save_timer.cancel()
|
|
|
|
def test_set_get_item_none(self):
|
|
config = Config('test.conf', config_dir=self.config_dir)
|
|
|
|
config['foo'] = None
|
|
self.assertIsNone(config['foo'])
|
|
self.assertIsInstance(config['foo'], type(None))
|
|
|
|
config['foo'] = 1
|
|
self.assertEquals(config.get('foo'), 1)
|
|
|
|
config['foo'] = None
|
|
self.assertIsNone(config['foo'])
|
|
|
|
config['bar'] = None
|
|
self.assertIsNone(config['bar'])
|
|
|
|
config['bar'] = None
|
|
self.assertIsNone(config['bar'])
|
|
|
|
config._save_timer.cancel()
|
|
|
|
def test_get(self):
|
|
config = Config('test.conf', config_dir=self.config_dir)
|
|
config['foo'] = 1
|
|
self.assertEquals(config.get('foo'), 1)
|
|
self.assertEquals(config.get('foobar'), None)
|
|
self.assertEquals(config.get('foobar', 2), 2)
|
|
config['foobar'] = 5
|
|
self.assertEquals(config.get('foobar', 2), 5)
|
|
|
|
def test_load(self):
|
|
def check_config():
|
|
config = Config('test.conf', config_dir=self.config_dir)
|
|
|
|
self.assertEquals(config['string'], 'foobar')
|
|
self.assertEquals(config['float'], 0.435)
|
|
|
|
# Test loading an old config from 1.1.x
|
|
import pickle
|
|
with open(os.path.join(self.config_dir, 'test.conf'), 'wb') as _file:
|
|
pickle.dump(DEFAULTS, _file)
|
|
|
|
check_config()
|
|
|
|
# Test opening a previous 1.2 config file of just a json object
|
|
import json
|
|
with open(os.path.join(self.config_dir, 'test.conf'), 'wb') as _file:
|
|
json.dump(DEFAULTS, _file, indent=2)
|
|
|
|
check_config()
|
|
|
|
# Test opening a previous 1.2 config file of having the format versions
|
|
# as ints
|
|
with open(os.path.join(self.config_dir, 'test.conf'), 'wb') as _file:
|
|
_file.write(str(1) + '\n')
|
|
_file.write(str(1) + '\n')
|
|
json.dump(DEFAULTS, _file, indent=2)
|
|
|
|
check_config()
|
|
|
|
# Test the 1.2 config format
|
|
version = {'format': 1, 'file': 1}
|
|
with open(os.path.join(self.config_dir, 'test.conf'), 'wb') as _file:
|
|
json.dump(version, _file, indent=2)
|
|
json.dump(DEFAULTS, _file, indent=2)
|
|
|
|
check_config()
|
|
|
|
def test_save(self):
|
|
config = Config('test.conf', defaults=DEFAULTS, config_dir=self.config_dir)
|
|
# We do this twice because the first time we need to save the file to disk
|
|
# and the second time we do a compare and we should not write
|
|
ret = config.save()
|
|
self.assertTrue(ret)
|
|
ret = config.save()
|
|
self.assertTrue(ret)
|
|
|
|
config['string'] = 'baz'
|
|
config['int'] = 2
|
|
ret = config.save()
|
|
self.assertTrue(ret)
|
|
del config
|
|
|
|
config = Config('test.conf', defaults=DEFAULTS, config_dir=self.config_dir)
|
|
self.assertEquals(config['string'], 'baz')
|
|
self.assertEquals(config['int'], 2)
|
|
|
|
def test_save_timer(self):
|
|
self.clock = task.Clock()
|
|
deluge.config.callLater = self.clock.callLater
|
|
|
|
config = Config('test.conf', defaults=DEFAULTS, config_dir=self.config_dir)
|
|
config['string'] = 'baz'
|
|
config['int'] = 2
|
|
self.assertTrue(config._save_timer.active())
|
|
|
|
# Timeout set for 5 seconds in config, so lets move clock by 5 seconds
|
|
self.clock.advance(5)
|
|
|
|
def check_config(config):
|
|
self.assertTrue(not config._save_timer.active())
|
|
del config
|
|
config = Config('test.conf', defaults=DEFAULTS, config_dir=self.config_dir)
|
|
self.assertEquals(config['string'], 'baz')
|
|
self.assertEquals(config['int'], 2)
|
|
|
|
check_config(config)
|
|
|
|
def test_find_json_objects(self):
|
|
s = """{
|
|
"file": 1,
|
|
"format": 1
|
|
}{
|
|
"ssl": true,
|
|
"enabled": false,
|
|
"port": 8115
|
|
}\n"""
|
|
|
|
from deluge.config import find_json_objects
|
|
|
|
objects = find_json_objects(s)
|
|
self.assertEquals(len(objects), 2)
|