diff options
author | Rob Dennis <rdennis@gmail.com> | 2014-03-09 23:08:57 -0400 |
---|---|---|
committer | Rob Dennis <rdennis@gmail.com> | 2014-03-09 23:08:57 -0400 |
commit | dfda0e5d88a566d6f899eca5caae163b3db8d32f (patch) | |
tree | 8debc51e5b3c051daf6e94b181b80fb7bd2234fd | |
parent | 0ca42491581b921cff9de7cba854fb86d4c3fa80 (diff) | |
download | configobj-git-dfda0e5d88a566d6f899eca5caae163b3db8d32f.tar.gz |
#34, #14 - corrected and moved over doctests effected by writing out in binary mode; handling encoding that in a py2/3-compatible way
-rw-r--r-- | configobj.py | 8 | ||||
-rw-r--r-- | test_configobj.py | 56 | ||||
-rw-r--r-- | tests/test_configobj.py | 56 |
3 files changed, 59 insertions, 61 deletions
diff --git a/configobj.py b/configobj.py index 2d6f267..ef064e4 100644 --- a/configobj.py +++ b/configobj.py @@ -2117,10 +2117,10 @@ class ConfigObj(Section): if outfile is not None: outfile.write(output) else: - h = open(self.filename, 'wb') - h.write(output) - h.close() - + with open(self.filename, 'wb') as h: + h.write(output.encode(self.encoding or + self.default_encoding or + 'ascii')) def validate(self, validator, preserve_errors=False, copy=False, section=None): diff --git a/test_configobj.py b/test_configobj.py index bb75cb6..00279ab 100644 --- a/test_configobj.py +++ b/test_configobj.py @@ -45,62 +45,6 @@ def _test_configobj(): """ Testing ConfigObj - >>> filename = a.filename - >>> a.filename = None - >>> values = a.write() - >>> index = 0 - >>> while index < 23: - ... index += 1 - ... line = values[index-1] - ... assert line.endswith('# comment ' + str(index)) - >>> a.filename = filename - - >>> start_comment = ['# Initial Comment', '', '#'] - >>> end_comment = ['', '#', '# Final Comment'] - >>> newconfig = start_comment + testconfig1.split('\\n') + end_comment - >>> nc = ConfigObj(newconfig) - >>> nc.initial_comment - ['# Initial Comment', '', '#'] - >>> nc.final_comment - ['', '#', '# Final Comment'] - >>> nc.initial_comment == start_comment - 1 - >>> nc.final_comment == end_comment - 1 - - Test the _handle_comment method - - >>> c = ConfigObj() - >>> c['foo'] = 'bar' - >>> c.inline_comments['foo'] = 'Nice bar' - >>> c.write() - ['foo = bar # Nice bar'] - - tekNico: FIXME: use StringIO instead of real files - - >>> filename = a.filename - >>> a.filename = 'test.ini' - >>> a.write() - >>> a.filename = filename - >>> a == ConfigObj('test.ini', raise_errors=True) - 1 - >>> os.remove('test.ini') - >>> b.filename = 'test.ini' - >>> b.write() - >>> b == ConfigObj('test.ini', raise_errors=True) - 1 - >>> os.remove('test.ini') - >>> i.filename = 'test.ini' - >>> i.write() - >>> i == ConfigObj('test.ini', raise_errors=True) - 1 - >>> os.remove('test.ini') - >>> a = ConfigObj() - >>> a['DEFAULT'] = {'a' : 'fish'} - >>> a['a'] = '%(a)s' - >>> a.write() - ['a = %(a)s', '[DEFAULT]', 'a = fish'] - Test indentation handling >>> ConfigObj({'sect': {'sect': {'foo': 'bar'}}}).write() diff --git a/tests/test_configobj.py b/tests/test_configobj.py index ac976a9..d8a1140 100644 --- a/tests/test_configobj.py +++ b/tests/test_configobj.py @@ -1029,4 +1029,58 @@ def test_multiline_comments(i): 'name2': ' another single line value ', 'name3': ' a single line value ', 'name1': ' a single line value ', - }
\ No newline at end of file + } + + +class TestComments(object): + def test_starting_and_ending_comments(self, a, testconfig1): + + filename = a.filename + a.filename = None + values = a.write() + index = 0 + while index < 23: + index += 1 + line = values[index-1] + assert line.endswith('# comment ' + str(index)) + a.filename = filename + + start_comment = ['# Initial Comment', '', '#'] + end_comment = ['', '#', '# Final Comment'] + newconfig = start_comment + testconfig1.splitlines() + end_comment + nc = ConfigObj(newconfig) + assert nc.initial_comment == ['# Initial Comment', '', '#'] + assert nc.final_comment == ['', '#', '# Final Comment'] + assert nc.initial_comment == start_comment + assert nc.final_comment == end_comment + + def test_inline_comments(self): + c = ConfigObj() + c['foo'] = 'bar' + c.inline_comments['foo'] = 'Nice bar' + assert c.write() == ['foo = bar # Nice bar'] + + +def test_overwriting_filenames(a, b, i): + #TODO: I'm not entirely sure what this test is actually asserting + filename = a.filename + a.filename = 'test.ini' + a.write() + a.filename = filename + assert a == ConfigObj('test.ini', raise_errors=True) + os.remove('test.ini') + b.filename = 'test.ini' + b.write() + assert b == ConfigObj('test.ini', raise_errors=True) + os.remove('test.ini') + i.filename = 'test.ini' + i.write() + assert i == ConfigObj('test.ini', raise_errors=True) + os.remove('test.ini') + + +def test_interpolation_using_default_sections(): + c = ConfigObj() + c['DEFAULT'] = {'a' : 'fish'} + c['a'] = '%(a)s' + assert c.write() == ['a = %(a)s', '[DEFAULT]', 'a = fish'] |