diff --git a/rattail/config.py b/rattail/config.py index 93ee85d57db0b56f2357ec51092d66dd9d40db9b..a22d7037d0b0cde3611459535bc1864d93df177d 100644 --- a/rattail/config.py +++ b/rattail/config.py @@ -65,8 +65,8 @@ def parse_list(value): return [] # Per the shlex docs (https://docs.python.org/2/library/shlex.html): # "Prior to Python 2.7.3, this module did not support Unicode input." - if sys.version_info < (2, 7, 3) and isinstance(value, unicode): # pragma: no cover - value = value.encode(u'utf-8') + if isinstance(value, unicode): + value = value.encode('utf-8') parser = shlex.shlex(value) parser.whitespace += u',' parser.whitespace_split = True @@ -112,7 +112,9 @@ class RattailConfig(object): self.usedb = usedb if self.usedb is None: self.usedb = self.getbool('rattail.config', 'usedb', usedb=False, default=False) - self.preferdb = self.getbool('rattail.config', 'preferdb', usedb=False, default=False) + self.preferdb = preferdb + if self.usedb and self.preferdb is None: + self.preferdb = self.getbool('rattail.config', 'preferdb', usedb=False, default=False) # Attempt to detect lack of SQLAlchemy libraries etc. This allows us # to avoid installing those on a machine which will not need to access diff --git a/rattail/tests/test_config.py b/rattail/tests/test_config.py index 0b8e977beb51fca2eb9e25f1c2002c44975cbe8a..1cdc232a62aee58b15fa60ce7fcf647507bcb794 100644 --- a/rattail/tests/test_config.py +++ b/rattail/tests/test_config.py @@ -1,13 +1,39 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals +from __future__ import unicode_literals, absolute_import -from unittest import TestCase +import os +import unittest + +from fixture import TempIO from rattail import config -class TestParseList(TestCase): +class TestParseBoolFunc(unittest.TestCase): + + def test_none(self): + self.assertIsNone(config.parse_bool(None)) + + def test_true(self): + self.assertIs(config.parse_bool(True), True) + + def test_false(self): + self.assertIs(config.parse_bool(False), False) + + def test_string(self): + self.assertTrue(config.parse_bool('true')) + self.assertTrue(config.parse_bool('yes')) + self.assertTrue(config.parse_bool('on')) + self.assertTrue(config.parse_bool('1')) + + self.assertFalse(config.parse_bool('false')) + self.assertFalse(config.parse_bool('no')) + self.assertFalse(config.parse_bool('off')) + self.assertFalse(config.parse_bool('0')) + + +class TestParseListFunc(unittest.TestCase): def test_none(self): value = config.parse_list(None) @@ -50,12 +76,105 @@ class TestParseList(TestCase): self.assertEqual(value[2], u'baz') def test_multiple_values_separated_by_whitespace_and_commas_with_some_quoting(self): - value = config.parse_list(u""" + value = config.parse_list(""" foo "C:\\some path\\with spaces\\and, a comma", baz -""") + """) self.assertEqual(len(value), 3) self.assertEqual(value[0], u'foo') self.assertEqual(value[1], u'C:\\some path\\with spaces\\and, a comma') self.assertEqual(value[2], u'baz') + + +class TestRattailConfig(unittest.TestCase): + + def setUp(self): + self.tempio = TempIO() + + def tearDown(self): + del self.tempio + + def setup_files(self): + self.site_path = self.tempio.putfile('site.conf', """ +[rattail] + """) + + self.host_path = self.tempio.putfile('host.conf', """ +[rattail.config] +include = "{}" + """.format(self.site_path)) + + self.app_path = self.tempio.putfile('app.conf', """ +[rattail.config] +include = "{}" + """.format(self.host_path)) + + self.custom_path = self.tempio.putfile('custom.conf', """ +[rattail.config] +include = "%(here)s/app.conf" + """) + + def test_init_defaults(self): + cfg = config.RattailConfig() + self.assertEqual(cfg.files_requested, []) + self.assertEqual(cfg.files_read, []) + self.assertIsNone(cfg._session_factory) + + def test_init_params(self): + self.setup_files() + + # files + cfg = config.RattailConfig() + self.assertEqual(cfg.files_requested, []) + self.assertEqual(cfg.files_read, []) + cfg = config.RattailConfig(files=[self.site_path]) + self.assertEqual(cfg.files_requested, [self.site_path]) + self.assertEqual(cfg.files_read, [self.site_path]) + + # usedb + cfg = config.RattailConfig() + self.assertFalse(cfg.usedb) + cfg = config.RattailConfig(usedb=True) + self.assertTrue(cfg.usedb) + + # preferdb + cfg = config.RattailConfig() + self.assertFalse(cfg.preferdb) + cfg = config.RattailConfig(preferdb=True) + self.assertTrue(cfg.preferdb) + + def test_read_file_with_recurse(self): + self.setup_files() + cfg = config.RattailConfig() + cfg.read_file(self.custom_path, recurse=True) + self.assertEqual(cfg.files_requested, [self.custom_path, self.app_path, self.host_path, self.site_path]) + self.assertEqual(cfg.files_read, [self.site_path, self.host_path, self.app_path, self.custom_path]) + + def test_read_file_once_only(self): + self.setup_files() + + another_path = self.tempio.putfile('another.conf', """ +[rattail.config] +include = "{custom}" "{site}" "{app}" "{site}" "{custom}" + """.format(custom=self.custom_path, app=self.app_path, site=self.site_path)) + + cfg = config.RattailConfig() + cfg.read_file(another_path, recurse=True) + self.assertEqual(cfg.files_requested, [another_path, self.custom_path, self.app_path, self.host_path, self.site_path]) + self.assertEqual(cfg.files_read, [self.site_path, self.host_path, self.app_path, self.custom_path, another_path]) + + def test_read_file_skip_missing(self): + self.setup_files() + bogus_path = '/tmp/does-not/exist' + self.assertFalse(os.path.exists(bogus_path)) + + another_path = self.tempio.putfile('another.conf', """ +[rattail.config] +include = "{bogus}" "{app}" "{bogus}" "{site}" + """.format(bogus=bogus_path, app=self.app_path, site=self.site_path)) + + cfg = config.RattailConfig() + cfg.read_file(another_path, recurse=True) + self.assertEqual(cfg.files_requested, [another_path, bogus_path, self.app_path, self.host_path, self.site_path]) + self.assertEqual(cfg.files_read, [self.site_path, self.host_path, self.app_path, another_path])