From 2bf6e775318876cf005daff71b4412e1d95e9761 Mon Sep 17 00:00:00 2001 From: fuzzyman Date: Sun, 22 Nov 2009 20:57:42 +0000 Subject: Test additions. --- configobj.py | 33 +++++++++++++++++---- docs/configobj.txt | 15 ++++++---- functionaltests/__init__.py | 0 functionaltests/conf.ini | 10 +++++++ functionaltests/conf.spec | 13 +++++++++ functionaltests/test_validate_errors.py | 51 +++++++++++++++++++++++++++++++++ 6 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 functionaltests/__init__.py create mode 100644 functionaltests/conf.ini create mode 100644 functionaltests/conf.spec create mode 100644 functionaltests/test_validate_errors.py diff --git a/configobj.py b/configobj.py index 7377cc9..10f4759 100644 --- a/configobj.py +++ b/configobj.py @@ -1180,21 +1180,44 @@ class ConfigObj(Section): } - def __init__(self, infile=None, options=None, _inspec=False, **kwargs): + def __init__(self, infile=None, options=None, configspec=None, encoding=None, + interpolation=True, raise_errors=False, list_values=True, + create_empty=False, file_error=False, stringify=True, + indent_type=None, default_encoding=None, unrepr=False, + write_empty_values=False, _inspec=False): """ Parse a config file or create a config file object. - ``ConfigObj(infile=None, options=None, **kwargs)`` + ``ConfigObj(infile=None, configspec=None, encoding=None, + interpolation=True, raise_errors=False, list_values=True, + create_empty=False, file_error=False, stringify=True, + indent_type=None, default_encoding=None, unrepr=False, + write_empty_values=False, _inspec=False)`` """ self._inspec = _inspec # init the superclass Section.__init__(self, self, 0, self) infile = infile or [] + if options is not None: + import warnings + warnings.warn('Passing in an options dictionary to ConfigObj() is ', + 'deprecated. Use **options instead.', + DeprecationWarning, stacklevel=2) + + _options = {'configspec': configspec, + 'encoding': encoding, 'interpolation': interpolation, + 'raise_errors': raise_errors, 'list_values': list_values, + 'create_empty': create_empty, 'file_error': file_error, + 'stringify': stringify, 'indent_type': indent_type, + 'default_encoding': default_encoding, 'unrepr': unrepr, + 'write_empty_values': write_empty_values} + options = dict(options or {}) - - # keyword arguments take precedence over an options dictionary - options.update(kwargs) + options.update(_options) + + # XXXX this ignores an explicit list_values = True in combination + # with _inspec. The user should *never* do that anyway, but still... if _inspec: options['list_values'] = False diff --git a/docs/configobj.txt b/docs/configobj.txt index 86bb889..0d1ee75 100644 --- a/docs/configobj.txt +++ b/docs/configobj.txt @@ -2289,17 +2289,20 @@ From version 4 it lists all releases and changes. * Minimum supported version of Python is now 2.3 * ~25% performance improvement thanks to Christian Heimes -* String interpolation now works in list values +* String interpolation now works in list value members * After validation any additional entries not in the configspec are listed in - the 'extra_values' section member + the ``extra_values`` section member +* Deprecated the use of the ``options`` dictionary in the ConfigObj constructor + and added explicit keyword arguments instead * BUGFIX: Checks that failed validation would not populate 'default_values' and 'restore_default_value' wouldn't work for those entries * BUGFIX: clear() clears 'defaults' * BUGFIX: empty values in list values were accidentally valid syntax. They now - raise a ParseError. e.g. "value = 1, , 2" -* If a section specified in a configspec is missing, or all the values in a - section are missing, will have False for all the values in the result - returned by by ``ConfigObj.validate`` + raise a ``ParseError``. e.g. "value = 1, , 2" +* BUGFIX: Change to the result of a call to ``validate`` when ``preserve_errors`` + is True. Previously sections where *all* values failed validation would + return False for the section rather than preserving the errors. False will + now only be returned for a section if it is missing * Distribution includes version 1.0.1 of validate.py * Removed __revision__ and __docformat__ diff --git a/functionaltests/__init__.py b/functionaltests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/functionaltests/conf.ini b/functionaltests/conf.ini new file mode 100644 index 0000000..aa429ff --- /dev/null +++ b/functionaltests/conf.ini @@ -0,0 +1,10 @@ + +extra = 3 + +[extra-section] + +[section] + [[sub-section]] + extra = 3 + [[extra-sub-section]] + extra = 3 diff --git a/functionaltests/conf.spec b/functionaltests/conf.spec new file mode 100644 index 0000000..3af70ac --- /dev/null +++ b/functionaltests/conf.spec @@ -0,0 +1,13 @@ + +value = integer + +[section] + value = integer + + [[sub-section]] + value = integer + [[missing-subsection]] + value = integer + +[missing-section] + value = integer diff --git a/functionaltests/test_validate_errors.py b/functionaltests/test_validate_errors.py new file mode 100644 index 0000000..06f855b --- /dev/null +++ b/functionaltests/test_validate_errors.py @@ -0,0 +1,51 @@ +import os +import unittest +from configobj import ConfigObj +from validate import Validator + +thisdir = os.path.dirname(os.path.join(os.getcwd(), __file__)) +inipath = os.path.join(thisdir, 'conf.ini') +specpath = os.path.join(thisdir, 'conf.spec') + + +class TestValidateErrors(unittest.TestCase): + + def test_validate_no_valid_entries(self): + conf = ConfigObj(inipath, configspec=specpath) + + validator = Validator() + result = conf.validate(validator) + self.assertFalse(result) + + + def test_validate_preserve_errors(self): + conf = ConfigObj(inipath, configspec=specpath) + + validator = Validator() + result = conf.validate(validator, preserve_errors=True) + + self.assertFalse(result['value']) + self.assertFalse(result['missing-section']) + + section = result['section'] + self.assertFalse(section['value']) + self.assertFalse(section['sub-section']['value']) + self.assertFalse(section['missing-subsection']) + + + def test_validate_extra_values(self): + conf = ConfigObj(inipath, configspec=specpath) + conf.validate(Validator(), preserve_errors=True) + + self.assertEqual(conf.extra_values, ['extra', 'extra-section']) + + self.assertEqual(conf['section'].extra_values, ['extra-sub-section']) + self.assertEqual(conf['section']['sub-section'].extra_values, + ['extra']) + + + + + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.1