from test
import test_support
class TestCaseBase(unittest
.TestCase
):
def newconfig(self
, defaults
=None):
self
.cf
= self
.config_class()
self
.cf
= self
.config_class(defaults
)
def fromstring(self
, string
, defaults
=None):
cf
= self
.newconfig(defaults
)
sio
= StringIO
.StringIO(string
)
"foo: this line is much, much longer than my editor\n"
"[Section\\with$weird%characters[\t]\n"
"[Internationalized Stuff]\n"
"key with spaces : value\n"
"another with spaces = splat!\n"
r
'Internationalized Stuff',
r
'Section\with$weird%characters[' '\t',
# The use of spaces in the section names serves as a
# regression test for SourceForge bug #583248:
# http://www.python.org/sf/583248
eq(cf
.get('Foo Bar', 'foo'), 'bar')
eq(cf
.get('Spacey Bar', 'foo'), 'bar')
eq(cf
.get('Commented Bar', 'foo'), 'bar')
eq(cf
.get('Spaces', 'key with spaces'), 'value')
eq(cf
.get('Spaces', 'another with spaces'), 'splat!')
self
.failIf('__name__' in cf
.options("Foo Bar"),
'__name__ "option" should not be exposed by the API!')
# Make sure the right things happen for remove_option();
# added to include check for SourceForge bug #123324:
self
.failUnless(cf
.remove_option('Foo Bar', 'foo'),
"remove_option() failed to report existance of option")
self
.failIf(cf
.has_option('Foo Bar', 'foo'),
"remove_option() failed to remove option")
self
.failIf(cf
.remove_option('Foo Bar', 'foo'),
"remove_option() failed to report non-existance of option"
self
.assertRaises(ConfigParser
.NoSectionError
,
cf
.remove_option
, 'No Such Section', 'foo')
eq(cf
.get('Long Line', 'foo'),
'this line is much, much longer than my editor\nlikes it.')
def test_case_sensitivity(self
):
cf
.set("a", "B", "value")
eq(cf
.options("a"), ["b"])
eq(cf
.get("a", "b"), "value",
"could not locate option, expecting case-insensitive option names")
self
.failUnless(cf
.has_option("a", "b"))
cf
.set("A", "A-B", "A-B value")
for opt
in ("a-b", "A-b", "a-B", "A-B"):
"has_option() returned false for option which should exist")
eq(cf
.options("A"), ["a-b"])
eq(cf
.options("a"), ["b"])
cf
.remove_option("a", "B")
"[MySection]\nOption: first line\n\tsecond line\n")
eq(cf
.options("MySection"), ["option"])
eq(cf
.get("MySection", "Option"), "first line\nsecond line")
cf
= self
.fromstring("[section]\nnekey=nevalue\n",
defaults
={"key":"value"})
self
.failUnless(cf
.has_option("section", "Key"))
def test_default_case_sensitivity(self
):
cf
= self
.newconfig({"foo": "Bar"})
cf
.get("DEFAULT", "Foo"), "Bar",
"could not locate option, expecting case-insensitive option names")
cf
= self
.newconfig({"Foo": "Bar"})
cf
.get("DEFAULT", "Foo"), "Bar",
"could not locate option, expecting case-insensitive defaults")
def test_parse_errors(self
):
self
.parse_error(ConfigParser
.ParsingError
,
"[Foo]\n extra-spaces: splat\n")
self
.parse_error(ConfigParser
.ParsingError
,
"[Foo]\n extra-spaces= splat\n")
self
.parse_error(ConfigParser
.ParsingError
,
"[Foo]\noption-without-value\n")
self
.parse_error(ConfigParser
.ParsingError
,
"[Foo]\n:value-without-option-name\n")
self
.parse_error(ConfigParser
.ParsingError
,
"[Foo]\n=value-without-option-name\n")
self
.parse_error(ConfigParser
.MissingSectionHeaderError
,
def parse_error(self
, exc
, src
):
sio
= StringIO
.StringIO(src
)
self
.assertRaises(exc
, self
.cf
.readfp
, sio
)
def test_query_errors(self
):
self
.assertEqual(cf
.sections(), [],
"new ConfigParser should have no defined sections")
self
.failIf(cf
.has_section("Foo"),
"new ConfigParser should have no acknowledged sections")
self
.assertRaises(ConfigParser
.NoSectionError
,
self
.assertRaises(ConfigParser
.NoSectionError
,
cf
.set, "foo", "bar", "value")
self
.get_error(ConfigParser
.NoSectionError
, "foo", "bar")
self
.get_error(ConfigParser
.NoOptionError
, "foo", "bar")
def get_error(self
, exc
, section
, option
):
self
.cf
.get(section
, option
)
self
.fail("expected exception type %s.%s"
% (exc
.__module
__, exc
.__name
__))
self
.failUnless(cf
.getboolean('BOOLTEST', 't%d' % x
))
self
.failIf(cf
.getboolean('BOOLTEST', 'f%d' % x
))
self
.assertRaises(ValueError,
cf
.getboolean
, 'BOOLTEST', 'e%d' % x
)
def test_weird_errors(self
):
self
.assertRaises(ConfigParser
.DuplicateSectionError
,
"foo: this line is much, much longer than my editor\n"
output
= StringIO
.StringIO()
"foo = this line is much, much longer than my editor\n"
def test_set_string_types(self
):
cf
= self
.fromstring("[sect]\n"
# Check that we don't get an exception when setting values in
# an existing section using strings:
cf
.set("sect", "option1", "splat")
cf
.set("sect", "option1", mystr("splat"))
cf
.set("sect", "option2", "splat")
cf
.set("sect", "option2", mystr("splat"))
cf
.set("sect", "option1", unicode("splat"))
cf
.set("sect", "option2", unicode("splat"))
def test_read_returns_file_list(self
):
file1
= test_support
.findfile("cfgparser.1")
# check when we pass a mix of readable and non-readable files:
parsed_files
= cf
.read([file1
, "nonexistant-file"])
self
.assertEqual(parsed_files
, [file1
])
self
.assertEqual(cf
.get("Foo Bar", "foo"), "newbar")
# check when we pass only a filename:
parsed_files
= cf
.read(file1
)
self
.assertEqual(parsed_files
, [file1
])
self
.assertEqual(cf
.get("Foo Bar", "foo"), "newbar")
# check when we pass only missing files:
parsed_files
= cf
.read(["nonexistant-file"])
self
.assertEqual(parsed_files
, [])
# check when we pass no files:
parsed_files
= cf
.read([])
self
.assertEqual(parsed_files
, [])
def get_interpolation_config(self
):
"bar=something %(with1)s interpolation (1 step)\n"
"bar9=something %(with9)s lots of interpolation (9 steps)\n"
"bar10=something %(with10)s lots of interpolation (10 steps)\n"
"bar11=something %(with11)s lots of interpolation (11 steps)\n"
"[Interpolation Error]\n"
# no definition for 'reference'
defaults
={"getname": "%(__name__)s"})
def check_items_config(self
, expected
):
"getdefault: |%(default)s|\n"
"getname: |%(__name__)s|",
defaults
={"default": "<default>"})
L
= list(cf
.items("section"))
self
.assertEqual(L
, expected
)
class ConfigParserTestCase(TestCaseBase
):
config_class
= ConfigParser
.ConfigParser
def test_interpolation(self
):
cf
= self
.get_interpolation_config()
eq(cf
.get("Foo", "getname"), "Foo")
eq(cf
.get("Foo", "bar"), "something with interpolation (1 step)")
eq(cf
.get("Foo", "bar9"),
"something with lots of interpolation (9 steps)")
eq(cf
.get("Foo", "bar10"),
"something with lots of interpolation (10 steps)")
self
.get_error(ConfigParser
.InterpolationDepthError
, "Foo", "bar11")
def test_interpolation_missing_value(self
):
cf
= self
.get_interpolation_config()
e
= self
.get_error(ConfigParser
.InterpolationError
,
"Interpolation Error", "name")
self
.assertEqual(e
.reference
, "reference")
self
.assertEqual(e
.section
, "Interpolation Error")
self
.assertEqual(e
.option
, "name")
self
.check_items_config([('default', '<default>'),
('getdefault', '|<default>|'),
('getname', '|section|'),
def test_set_nonstring_types(self
):
cf
.add_section('non-string')
cf
.set('non-string', 'int', 1)
cf
.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13, '%('])
cf
.set('non-string', 'dict', {'pi': 3.14159, '%(': 1,
cf
.set('non-string', 'string_with_interpolation', '%(list)s')
self
.assertEqual(cf
.get('non-string', 'int', raw
=True), 1)
self
.assertRaises(TypeError, cf
.get
, 'non-string', 'int')
self
.assertEqual(cf
.get('non-string', 'list', raw
=True),
[0, 1, 1, 2, 3, 5, 8, 13, '%('])
self
.assertRaises(TypeError, cf
.get
, 'non-string', 'list')
self
.assertEqual(cf
.get('non-string', 'dict', raw
=True),
{'pi': 3.14159, '%(': 1, '%(list)': '%(list)'})
self
.assertRaises(TypeError, cf
.get
, 'non-string', 'dict')
self
.assertEqual(cf
.get('non-string', 'string_with_interpolation',
self
.assertRaises(ValueError, cf
.get
, 'non-string',
'string_with_interpolation', raw
=False)
class RawConfigParserTestCase(TestCaseBase
):
config_class
= ConfigParser
.RawConfigParser
def test_interpolation(self
):
cf
= self
.get_interpolation_config()
eq(cf
.get("Foo", "getname"), "%(__name__)s")
"something %(with1)s interpolation (1 step)")
eq(cf
.get("Foo", "bar9"),
"something %(with9)s lots of interpolation (9 steps)")
eq(cf
.get("Foo", "bar10"),
"something %(with10)s lots of interpolation (10 steps)")
eq(cf
.get("Foo", "bar11"),
"something %(with11)s lots of interpolation (11 steps)")
self
.check_items_config([('default', '<default>'),
('getdefault', '|%(default)s|'),
('getname', '|%(__name__)s|'),
def test_set_nonstring_types(self
):
cf
.add_section('non-string')
cf
.set('non-string', 'int', 1)
cf
.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13])
cf
.set('non-string', 'dict', {'pi': 3.14159})
self
.assertEqual(cf
.get('non-string', 'int'), 1)
self
.assertEqual(cf
.get('non-string', 'list'),
[0, 1, 1, 2, 3, 5, 8, 13])
self
.assertEqual(cf
.get('non-string', 'dict'), {'pi': 3.14159})
class SafeConfigParserTestCase(ConfigParserTestCase
):
config_class
= ConfigParser
.SafeConfigParser
def test_safe_interpolation(self
):
# See http://www.python.org/sf/511737
cf
= self
.fromstring("[section]\n"
"option2=%(option1)s/xxx\n"
"not_ok=%(option2)s/%%s")
self
.assertEqual(cf
.get("section", "ok"), "xxx/%s")
self
.assertEqual(cf
.get("section", "not_ok"), "xxx/xxx/%s")
def test_set_nonstring_types(self
):
cf
= self
.fromstring("[sect]\n"
# Check that we get a TypeError when setting non-string values
# in an existing section:
self
.assertRaises(TypeError, cf
.set, "sect", "option1", 1)
self
.assertRaises(TypeError, cf
.set, "sect", "option1", 1.0)
self
.assertRaises(TypeError, cf
.set, "sect", "option1", object())
self
.assertRaises(TypeError, cf
.set, "sect", "option2", 1)
self
.assertRaises(TypeError, cf
.set, "sect", "option2", 1.0)
self
.assertRaises(TypeError, cf
.set, "sect", "option2", object())
test_support
.run_unittest(
if __name__
== "__main__":