diff options
author | Rob Dennis <rdennis@gmail.com> | 2014-03-19 21:02:24 -0400 |
---|---|---|
committer | Rob Dennis <rdennis@gmail.com> | 2014-03-19 21:02:24 -0400 |
commit | 21e232572bbb49873a1ef1bd58674c3bb25c4612 (patch) | |
tree | 5d3bc0469fced6cd3b2c091043c555b631668b6e | |
parent | e67de2f163eb17f664f661ad76069dbafddead7e (diff) | |
parent | 1e71e397ce2b5f3d4b7b69f84fb45e9ed2b8efac (diff) | |
download | configobj-git-21e232572bbb49873a1ef1bd58674c3bb25c4612.tar.gz |
Merge pull request #43 from robdennis/master
fixes #42
-rw-r--r-- | configobj.py | 2 | ||||
-rw-r--r-- | test_configobj.py | 53 | ||||
-rw-r--r-- | tests/test_configobj.py | 37 |
3 files changed, 38 insertions, 54 deletions
diff --git a/configobj.py b/configobj.py index ef064e4..26b7fd0 100644 --- a/configobj.py +++ b/configobj.py @@ -2058,7 +2058,7 @@ class ConfigObj(Section): this_entry = section[entry] comment = self._handle_comment(section.inline_comments[entry]) - if isinstance(this_entry, dict): + if isinstance(this_entry, Section): # a section out.append(self._write_marker( indent_string, diff --git a/test_configobj.py b/test_configobj.py index 96d19b7..49ebd78 100644 --- a/test_configobj.py +++ b/test_configobj.py @@ -517,59 +517,6 @@ def _test_unrepr_comments(): """ -def _test_newline_terminated(): - """ - >>> c = ConfigObj() - >>> c.newlines = '\\n' - >>> c['a'] = 'b' - >>> collector = StringIO() - >>> c.write(collector) - >>> collector.getvalue() - 'a = b\\n' - """ - - -def _test_hash_escaping(): - """ - >>> c = ConfigObj() - >>> c.newlines = '\\n' - >>> c['#a'] = 'b # something' - >>> collector = StringIO() - >>> c.write(collector) - >>> collector.getvalue() - '"#a" = "b # something"\\n' - - >>> c = ConfigObj() - >>> c.newlines = '\\n' - >>> c['a'] = 'b # something', 'c # something' - >>> collector = StringIO() - >>> c.write(collector) - >>> collector.getvalue() - 'a = "b # something", "c # something"\\n' - """ - - -def _test_lineendings(): - """ - NOTE: Need to use a real file because this code is only - exercised when reading from the filesystem. - - >>> h = open('temp', 'w') - >>> _ = h.write('\\r\\n') - >>> h.close() - >>> c = ConfigObj('temp') - >>> c.newlines - '\\r\\n' - >>> h = open('temp', 'w') - >>> _ = h.write('\\n') - >>> h.close() - >>> c = ConfigObj('temp') - >>> c.newlines - '\\n' - >>> os.remove('temp') - """ - - def _test_validate_with_copy_and_many(): """ >>> spec = ''' diff --git a/tests/test_configobj.py b/tests/test_configobj.py index f4f9a31..8f95434 100644 --- a/tests/test_configobj.py +++ b/tests/test_configobj.py @@ -1106,3 +1106,40 @@ class TestIndentation(object): assert ConfigObj(max_tabbed_cfg, indent_type=chr(9)).write() == one_tab assert ConfigObj(one_tab, indent_type=' ').write() == max_tabbed_cfg + +class TestEdgeCasesWhenWritingOut(object): + def test_newline_terminated(self, empty_cfg): + empty_cfg.newlines = '\n' + empty_cfg['a'] = 'b' + collector = six.StringIO() + empty_cfg.write(collector) + assert collector.getvalue() == 'a = b\n' + + def test_hash_escaping(self, empty_cfg): + empty_cfg.newlines = '\n' + empty_cfg['#a'] = 'b # something' + collector = six.StringIO() + empty_cfg.write(collector) + assert collector.getvalue() == '"#a" = "b # something"\n' + + empty_cfg = ConfigObj() + empty_cfg.newlines = '\n' + empty_cfg['a'] = 'b # something', 'c # something' + collector = six.StringIO() + empty_cfg.write(collector) + assert collector.getvalue() == 'a = "b # something", "c # something"\n' + + def test_detecting_line_endings_from_existing_files(self): + for expected_line_ending in ('\r\n', '\n'): + with open('temp', 'w') as h: + h.write(expected_line_ending) + c = ConfigObj('temp') + assert c.newlines == expected_line_ending + os.remove('temp') + + def test_writing_out_dict_value_with_unrepr(self): + # issue #42 + cfg = ['thing = {"a": 1}'] + c = ConfigObj(cfg, unrepr=True) + assert repr(c) == "ConfigObj({'thing': {'a': 1}})" + assert c.write() == ["thing = {'a': 1}"] |