Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test
  • Loading branch information
carlosmiei committed Feb 28, 2023
commit 53a99225628356e32e88beceb791344e219679d1
68 changes: 40 additions & 28 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def basic_test(self, cf):
'Spacey Bar',
'Spacey Bar From The Beginning',
'Types',
'This One Has A ] In It',
]

if self.allow_no_value:
Expand Down Expand Up @@ -130,6 +131,7 @@ def basic_test(self, cf):
eq(cf.get('Types', 'float'), "0.44")
eq(cf.getboolean('Types', 'boolean'), False)
eq(cf.get('Types', '123'), 'strange but acceptable')
eq(cf.get('This One Has A ] In It', 'forks'), 'spoons')
if self.allow_no_value:
eq(cf.get('NoValue', 'option-without-value'), None)

Expand Down Expand Up @@ -320,6 +322,8 @@ def test_basic(self):
float {0[0]} 0.44
boolean {0[0]} NO
123 {0[1]} strange but acceptable
[This One Has A ] In It]
forks {0[0]} spoons
""".format(self.delimiters, self.comment_prefixes)
if self.allow_no_value:
config_string += (
Expand Down Expand Up @@ -394,6 +398,9 @@ def test_basic_from_dict(self):
"boolean": False,
123: "strange but acceptable",
},
"This One Has A ] In It": {
"forks": "spoons"
},
}
if self.allow_no_value:
config.update({
Expand Down Expand Up @@ -521,7 +528,6 @@ def test_default_case_sensitivity(self):
cf.get(self.default_section, "Foo"), "Bar",
"could not locate option, expecting case-insensitive defaults")

@unittest.skipIf(os.name == "nt", "TODO: RUSTPYTHON, universal newlines")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably need to add this again, it skipped the tests for windows (which are now failing)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DimitrisJim restored thanks

def test_parse_errors(self):
cf = self.newconfig()
self.parse_error(cf, configparser.ParsingError,
Expand Down Expand Up @@ -709,57 +715,54 @@ class mystr(str):
cf.set("sect", "option1", "splat")
cf.set("sect", "option2", "splat")

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_read_returns_file_list(self):
if self.delimiters[0] != '=':
self.skipTest('incompatible format')
file1 = support.findfile("cfgparser.1")
# check when we pass a mix of readable and non-readable files:
cf = self.newconfig()
parsed_files = cf.read([file1, "nonexistent-file"])
parsed_files = cf.read([file1, "nonexistent-file"], encoding="utf-8")
self.assertEqual(parsed_files, [file1])
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
# check when we pass only a filename:
cf = self.newconfig()
parsed_files = cf.read(file1)
parsed_files = cf.read(file1, encoding="utf-8")
self.assertEqual(parsed_files, [file1])
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
# check when we pass only a Path object:
cf = self.newconfig()
parsed_files = cf.read(pathlib.Path(file1))
parsed_files = cf.read(pathlib.Path(file1), encoding="utf-8")
self.assertEqual(parsed_files, [file1])
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
# check when we passed both a filename and a Path object:
cf = self.newconfig()
parsed_files = cf.read([pathlib.Path(file1), file1])
parsed_files = cf.read([pathlib.Path(file1), file1], encoding="utf-8")
self.assertEqual(parsed_files, [file1, file1])
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
# check when we pass only missing files:
cf = self.newconfig()
parsed_files = cf.read(["nonexistent-file"])
parsed_files = cf.read(["nonexistent-file"], encoding="utf-8")
self.assertEqual(parsed_files, [])
# check when we pass no files:
cf = self.newconfig()
parsed_files = cf.read([])
parsed_files = cf.read([], encoding="utf-8")
self.assertEqual(parsed_files, [])

@unittest.skip("TODO: RUSTPYTHON, suspected to make CI hang")
def test_read_returns_file_list_with_bytestring_path(self):
if self.delimiters[0] != '=':
self.skipTest('incompatible format')
file1_bytestring = support.findfile("cfgparser.1").encode()
# check when passing an existing bytestring path
cf = self.newconfig()
parsed_files = cf.read(file1_bytestring)
parsed_files = cf.read(file1_bytestring, encoding="utf-8")
self.assertEqual(parsed_files, [file1_bytestring])
# check when passing an non-existing bytestring path
cf = self.newconfig()
parsed_files = cf.read(b'nonexistent-file')
parsed_files = cf.read(b'nonexistent-file', encoding="utf-8")
self.assertEqual(parsed_files, [])
# check when passing both an existing and non-existing bytestring path
cf = self.newconfig()
parsed_files = cf.read([file1_bytestring, b'nonexistent-file'])
parsed_files = cf.read([file1_bytestring, b'nonexistent-file'], encoding="utf-8")
self.assertEqual(parsed_files, [file1_bytestring])

# shared by subclasses
Expand Down Expand Up @@ -830,8 +833,6 @@ def test_clear(self):
self.assertEqual(set(cf.sections()), set())
self.assertEqual(set(cf[self.default_section].keys()), {'foo'})

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_setitem(self):
cf = self.fromstring("""
[section1]
Expand Down Expand Up @@ -924,8 +925,6 @@ def test_interpolation_missing_value(self):
self.assertEqual(e.args, ('name', 'Interpolation Error',
'%(reference)s', 'reference'))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_items(self):
self.check_items_config([('default', '<default>'),
('getdefault', '|<default>|'),
Expand Down Expand Up @@ -981,8 +980,6 @@ def test_add_section_default(self):
cf = self.newconfig()
self.assertRaises(ValueError, cf.add_section, self.default_section)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_defaults_keyword(self):
"""bpo-23835 fix for ConfigParser"""
cf = self.newconfig(defaults={1: 2.4})
Expand Down Expand Up @@ -1031,7 +1028,9 @@ class CustomConfigParser(configparser.ConfigParser):

class ConfigParserTestCaseLegacyInterpolation(ConfigParserTestCase):
config_class = configparser.ConfigParser
interpolation = configparser.LegacyInterpolation()
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
interpolation = configparser.LegacyInterpolation()

def test_set_malformatted_interpolation(self):
cf = self.fromstring("[sect]\n"
Expand All @@ -1051,6 +1050,14 @@ def test_set_malformatted_interpolation(self):
self.assertEqual(cf.get("sect", "option2"), "foo%%bar")


class ConfigParserTestCaseInvalidInterpolationType(unittest.TestCase):
def test_error_on_wrong_type_for_interpolation(self):
for value in [configparser.ExtendedInterpolation, 42, "a string"]:
with self.subTest(value=value):
with self.assertRaises(TypeError):
configparser.ConfigParser(interpolation=value)


class ConfigParserTestCaseNonStandardDelimiters(ConfigParserTestCase):
delimiters = (':=', '$')
comment_prefixes = ('//', '"')
Expand All @@ -1074,7 +1081,7 @@ def setUp(self):
cf.add_section(s)
for j in range(10):
cf.set(s, 'lovely_spam{}'.format(j), self.wonderful_spam)
with open(os_helper.TESTFN, 'w') as f:
with open(os_helper.TESTFN, 'w', encoding="utf-8") as f:
cf.write(f)

def tearDown(self):
Expand All @@ -1084,7 +1091,7 @@ def test_dominating_multiline_values(self):
# We're reading from file because this is where the code changed
# during performance updates in Python 3.2
cf_from_file = self.newconfig()
with open(os_helper.TESTFN) as f:
with open(os_helper.TESTFN, encoding="utf-8") as f:
cf_from_file.read_file(f)
self.assertEqual(cf_from_file.get('section8', 'lovely_spam4'),
self.wonderful_spam.replace('\t\n', '\n'))
Expand All @@ -1105,8 +1112,6 @@ def test_interpolation(self):
eq(cf.get("Foo", "bar11"),
"something %(with11)s lots of interpolation (11 steps)")

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_items(self):
self.check_items_config([('default', '<default>'),
('getdefault', '|%(default)s|'),
Expand Down Expand Up @@ -1485,7 +1490,7 @@ def fromstring(self, string, defaults=None):
class FakeFile:
def __init__(self):
file_path = support.findfile("cfgparser.1")
with open(file_path) as f:
with open(file_path, encoding="utf-8") as f:
self.lines = f.readlines()
self.lines.reverse()

Expand All @@ -1512,7 +1517,7 @@ def test_file(self):
pass # unfortunately we can't test bytes on this path
for file_path in file_paths:
parser = configparser.ConfigParser()
with open(file_path) as f:
with open(file_path, encoding="utf-8") as f:
parser.read_file(f)
self.assertIn("Foo Bar", parser)
self.assertIn("foo", parser["Foo Bar"])
Expand Down Expand Up @@ -1663,6 +1668,14 @@ def test_safeconfigparser_deprecation(self):
for warning in w:
self.assertTrue(warning.category is DeprecationWarning)

def test_legacyinterpolation_deprecation(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", DeprecationWarning)
configparser.LegacyInterpolation()
self.assertGreaterEqual(len(w), 1)
for warning in w:
self.assertIs(warning.category, DeprecationWarning)

def test_sectionproxy_repr(self):
parser = configparser.ConfigParser()
parser.read_string("""
Expand Down Expand Up @@ -2140,8 +2153,7 @@ def test_instance_assignment(self):

class MiscTestCase(unittest.TestCase):
def test__all__(self):
not_exported = {"Error"}
support.check__all__(self, configparser, not_exported=not_exported)
support.check__all__(self, configparser, not_exported={"Error"})


if __name__ == '__main__':
Expand Down