Index: Lib/test/test_cfgparser.py =================================================================== --- Lib/test/test_cfgparser.py (revision 41859) +++ Lib/test/test_cfgparser.py (working copy) @@ -415,11 +415,153 @@ self.assertRaises(TypeError, cf.set, "sect", "option2", object()) +example = """\ +# Leading comment. +Rem Another comment. +REM Yet more comments. +reM Strange comment. + +;Notice the blank lines above and below. + +[sect1] +opt1=2 +opt2=This is a value with + a continuation. +opt3:another option. +opt4=5 ; There's a comment here. +[sect2] +opt5 = old +opt6 = 4 +opt7=a value with + a continuation ; and a comment. +; This is an empty section. +[sect3] +""" +stripped = """\ +[DEFAULT] +default = not to be written + +[sect1] +opt1 = 2 +opt2 = This is a value with +\ta continuation. +opt3 = option +\twith a newline +opt4 = 5 + +[sect2] +opt5 = new +opt7 = new2 +opt8 = add + +[sect3] + + +""" +class UpdateFileTest(unittest.TestCase): + """Class which tests the update_file() method of the ConfigParser + class against the test cases. + """ + def setUp(self): + defaults = {"default" : "not to be written"} + self.config = ConfigParser.RawConfigParser(defaults) + self.example_fp = StringIO.StringIO(example) + self.example_fp.seek(0) + self.config.readfp(self.example_fp) + self.example_fp.seek(0) + self.config.set("sect2", "opt5", "new") + self.config.set("sect2", "opt8", "add") + self.config.set("sect1", "opt3", "option\nwith a newline") + self.config.set("sect2", "opt7", "new2") + self.config.remove_option("sect2", "opt6") + + def test_update_empty(self): + empty = StringIO.StringIO() + self.config.update_file(empty) + self.assertEqual(empty.getvalue(), stripped) + + def test_continuation_comment(self): + self.config.update_file(self.example_fp) + self.assert_("opt7=new2 ; and a comment." + in self.example_fp.getvalue()) + + def test_add_continuation(self): + self.config.update_file(self.example_fp) + self.assert_("opt3:option\n\twith a newline" + in self.example_fp.getvalue()) + + def test_removed_option(self): + self.config.update_file(self.example_fp) + self.assert_("opt6" not in self.example_fp.getvalue()) + + def test_blank_sorted(self): + empty = StringIO.StringIO() + self.config.update_file(empty) + empty.seek(0) + self._test_ordering(empty) + + def test_ordering_preserved(self): + self.config.update_file(self.example_fp) + self.example_fp.seek(0) + self._test_ordering(self.example_fp) + + def _test_ordering(self, result): + sect = '1' + opt = '1' + for line in result.readlines(): + if line.startswith("[sect"): + self.assert_(line.startswith("[sect" + sect)) + sect = str(int(sect)+1) + elif line.startswith("opt"): + self.assert_(line.startswith("opt" + opt)) + opt = str(int(opt)+1) + # We remove #6. + if opt == '6': + opt = '7' + + def test_blank_lines_preserved(self): + self.config.update_file(self.example_fp) + self.assert_("\n;Notice the blank lines above and below.\n" in + self.example_fp.getvalue()) + + def test_values_updated(self): + self.config.update_file(self.example_fp) + self.assert_("opt5=new" in self.example_fp.getvalue()) + + def test_defaults_included(self): + self.config.update_file(self.example_fp) + self.assert_("default" in self.example_fp.getvalue()) + + def test_name_excluded(self): + self.config.update_file(self.example_fp) + self.assert_("__name__" not in self.example_fp.getvalue()) + + def test_preserve_comments(self): + self.config.update_file(self.example_fp) + self.assert_("# Leading comment." in self.example_fp.getvalue()) + self.assert_("Rem Another comment." in self.example_fp.getvalue()) + self.assert_("REM Yet more comments." + in self.example_fp.getvalue()) + self.assert_("reM Strange comment." in self.example_fp.getvalue()) + self.assert_(";Notice the blank lines above and below." + in self.example_fp.getvalue()) + self.assert_("opt4=5 ; There's a comment here." + in self.example_fp.getvalue()) + + def test_add_missing(self): + self.config.update_file(self.example_fp) + self.assert_("opt8" in self.example_fp.getvalue()) + + def test_not_add_missing(self): + self.config.update_file(self.example_fp, False) + self.assert_("opt8" not in self.example_fp.getvalue()) + def test_main(): test_support.run_unittest( ConfigParserTestCase, RawConfigParserTestCase, - SafeConfigParserTestCase + SafeConfigParserTestCase, + UpdateFileTest, ) if __name__ == "__main__":