def testcb(): config = self._read() config.set('a', 'option2', "Voilà l'été") # UTF-8 config.set('a', 'option1', u"Voilà l'été") # unicode self.assertEquals('x', config.get('a', 'option')) self.assertEquals(u"Voilà l'été", config.get('a', 'option1')) self.assertEquals(u"Voilà l'été", config.get('a', 'option2')) config.save() configfile = open(self.filename, 'r') self.assertEquals(['# -*- coding: utf-8 -*-\n', '\n', '[a]\n', "option1 = Voilà l'été\n", "option2 = Voilà l'été\n", '\n', '[inherit]\n', "file = trac-site.ini\n", '\n'], configfile.readlines()) configfile.close() config2 = Configuration(self.filename) self.assertEquals('x', config2.get('a', 'option')) self.assertEquals(u"Voilà l'été", config2.get('a', 'option1')) self.assertEquals(u"Voilà l'été", config2.get('a', 'option2'))
def testcb(): config = self._read() config.set("a", "option2", "Voilà l'été") # UTF-8 config.set("a", "option1", u"Voilà l'été") # unicode self.assertEquals("x", config.get("a", "option")) self.assertEquals(u"Voilà l'été", config.get("a", "option1")) self.assertEquals(u"Voilà l'été", config.get("a", "option2")) config.save() configfile = open(self.filename, "r") self.assertEquals( [ "# -*- coding: utf-8 -*-\n", "\n", "[a]\n", "option1 = Voilà l'été\n", "option2 = Voilà l'été\n", "\n", "[inherit]\n", "file = trac-site.ini\n", "\n", ], configfile.readlines(), ) configfile.close() config2 = Configuration(self.filename) self.assertEquals("x", config2.get("a", "option")) self.assertEquals(u"Voilà l'été", config2.get("a", "option1")) self.assertEquals(u"Voilà l'été", config2.get("a", "option2"))
def test_set_and_save_inherit(self): with self.inherited_file(): self._write(['[a]', 'option = x'], site=True) config = self._read() config.set('a', 'option2', "Voilà l'été") # UTF-8 config.set('a', 'option1', u"Voilà l'été") # unicode self.assertEqual('x', config.get('a', 'option')) self.assertEqual(u"Voilà l'été", config.get('a', 'option1')) self.assertEqual(u"Voilà l'été", config.get('a', 'option2')) config.save() configfile = open(self.filename, 'r') self.assertEqual(['# -*- coding: utf-8 -*-\n', '\n', '[a]\n', "option1 = Voilà l'été\n", "option2 = Voilà l'été\n", '\n', '[inherit]\n', "file = trac-site.ini\n", '\n'], configfile.readlines()) configfile.close() config2 = Configuration(self.filename) self.assertEqual('x', config2.get('a', 'option')) self.assertEqual(u"Voilà l'été", config2.get('a', 'option1')) self.assertEqual(u"Voilà l'été", config2.get('a', 'option2'))
def test_set_and_save(self): config = self._read() config.set('b', u'öption0', 'y') config.set(u'aä', 'öption0', 'x') config.set('aä', 'option2', "Voilà l'été") # UTF-8 config.set(u'aä', 'option1', u"Voilà l'été") # unicode # Note: the following would depend on the locale.getpreferredencoding() # config.set('a', 'option3', "Voil\xe0 l'\xe9t\xe9") # latin-1 self.assertEquals('x', config.get(u'aä', u'öption0')) self.assertEquals(u"Voilà l'été", config.get(u'aä', 'option1')) self.assertEquals(u"Voilà l'été", config.get(u'aä', 'option2')) config.save() configfile = open(self.filename, 'r') self.assertEquals(['# -*- coding: utf-8 -*-\n', '\n', '[aä]\n', "option1 = Voilà l'été\n", "option2 = Voilà l'été\n", 'öption0 = x\n', # "option3 = Voilà l'été\n", '\n', '[b]\n', 'öption0 = y\n', '\n'], configfile.readlines()) configfile.close() config2 = Configuration(self.filename) self.assertEquals('x', config2.get(u'aä', u'öption0')) self.assertEquals(u"Voilà l'été", config2.get(u'aä', 'option1')) self.assertEquals(u"Voilà l'été", config2.get(u'aä', 'option2'))
def test_set_and_save(self): config = self._read() config.set('b', u'öption0', 'y') config.set(u'aä', 'öption0', 'x') config.set('aä', 'option2', "Voilà l'été") # UTF-8 config.set(u'aä', 'option1', u"Voilà l'été") # unicode # Note: the following would depend on the locale.getpreferredencoding() # config.set('a', 'option3', "Voil\xe0 l'\xe9t\xe9") # latin-1 self.assertEquals('x', config.get(u'aä', u'öption0')) self.assertEquals(u"Voilà l'été", config.get(u'aä', 'option1')) self.assertEquals(u"Voilà l'été", config.get(u'aä', 'option2')) config.save() configfile = open(self.filename, 'r') self.assertEquals( [ '# -*- coding: utf-8 -*-\n', '\n', '[aä]\n', "option1 = Voilà l'été\n", "option2 = Voilà l'été\n", 'öption0 = x\n', # "option3 = Voilà l'été\n", '\n', '[b]\n', 'öption0 = y\n', '\n' ], configfile.readlines()) configfile.close() config2 = Configuration(self.filename) self.assertEquals('x', config2.get(u'aä', u'öption0')) self.assertEquals(u"Voilà l'été", config2.get(u'aä', 'option1')) self.assertEquals(u"Voilà l'été", config2.get(u'aä', 'option2'))
def test_component_no_overwrite_parent(self): """Values in parent configuration are not overwritten.""" parent_config = Configuration(self.sitename) parent_config.set('compa', 'opt1', 3) parent_config.save() config = self._read() config.set('inherit', 'file', 'trac-site.ini') config.save() config.parse_if_needed(True) config.set_defaults(component='trac.tests.config.CompA') config.save() with open(self.sitename, 'r') as f: self.assertEqual('# -*- coding: utf-8 -*-\n', f.next()) self.assertEqual('\n', f.next()) self.assertEqual('[compa]\n', f.next()) self.assertEqual('opt1 = 3\n', f.next()) self.assertEqual('\n', f.next()) self.assertRaises(StopIteration, f.next) with open(self.filename, 'r') as f: self.assertEqual('# -*- coding: utf-8 -*-\n', f.next()) self.assertEqual('\n', f.next()) self.assertEqual('[compa]\n', f.next()) self.assertEqual('opt2 = a\n', f.next()) self.assertEqual('\n', f.next()) self.assertEqual('[inherit]\n', f.next()) self.assertEqual('file = trac-site.ini\n', f.next()) self.assertEqual('\n', f.next()) self.assertRaises(StopIteration, f.next)
def test_set_and_save(self): config = self._read() config.set("b", u"öption0", "y") config.set(u"aä", "öption0", "x") config.set("aä", "option2", "Voilà l'été") # UTF-8 config.set(u"aä", "option1", u"Voilà l'été") # unicode # Note: the following would depend on the locale.getpreferredencoding() # config.set('a', 'option3', "Voil\xe0 l'\xe9t\xe9") # latin-1 self.assertEquals("x", config.get(u"aä", u"öption0")) self.assertEquals(u"Voilà l'été", config.get(u"aä", "option1")) self.assertEquals(u"Voilà l'été", config.get(u"aä", "option2")) config.save() configfile = open(self.filename, "r") self.assertEquals( [ "# -*- coding: utf-8 -*-\n", "\n", "[aä]\n", "option1 = Voilà l'été\n", "option2 = Voilà l'été\n", "öption0 = x\n", # "option3 = Voilà l'été\n", "\n", "[b]\n", "öption0 = y\n", "\n", ], configfile.readlines(), ) configfile.close() config2 = Configuration(self.filename) self.assertEquals("x", config2.get(u"aä", u"öption0")) self.assertEquals(u"Voilà l'été", config2.get(u"aä", "option1")) self.assertEquals(u"Voilà l'été", config2.get(u"aä", "option2"))
def setUp(self): self.config = Configuration(None) self.config.parser.add_section(u'séction1') self.config.parser.set(u'séction1', u'öption1', u'cönfig-valué') self.config.parser.set(u'séction1', u'öption4', u'cönfig-valué') parent_config = Configuration(None) parent_config.parser.add_section(u'séction1') parent_config.parser.add_section(u'séction2') parent_config.parser.set(u'séction1', u'öption1', u'cönfig-valué') parent_config.parser.set(u'séction1', u'öption2', u'înherited-valué') parent_config.parser.set(u'séction2', u'öption2', u'înherited-valué') self.config.parents = [parent_config] class OptionClass(object): Option(u'séction1', u'öption1', u'dēfault-valué') Option(u'séction1', u'öption2', u'dēfault-valué') Option(u'séction1', u'öption3', u'dēfault-valué') Option(u'séction3', u'öption1', u'dēfault-valué')
def test_component_no_overwrite_parent(self): """Values in parent configuration are not overwritten.""" parent_config = Configuration(self.sitename) parent_config.set('compa', 'opt1', 3) parent_config.save() config = self._read() config.set('inherit', 'file', 'trac-site.ini') config.save() config.parse_if_needed(True) config.set_defaults(component='trac.tests.config.CompA') config.save() expected = [ '# -*- coding: utf-8 -*-\n', '\n', '[compa]\n', 'opt1 = 3\n', '\n', ] self.assertEqual(expected, readlines(self.sitename)) expected = [ '# -*- coding: utf-8 -*-\n', '\n', '[compa]\n', 'opt2 = a\n', '\n', '[inherit]\n', 'file = trac-site.ini\n', '\n', ] self.assertEqual(expected, readlines(self.filename))
def test_repr(self): self.assertEqual('<Configuration None>', repr(Configuration(None))) config = self._read() self.assertEqual("<Configuration %r>" % self.filename, repr(config))
def _read(self): return Configuration(self.filename)
class ConfigurationTestCase(unittest.TestCase): def setUp(self): self.config = Configuration(None) self.config.parser.add_section(u'séction1') self.config.parser.set(u'séction1', u'öption1', u'cönfig-valué') self.config.parser.set(u'séction1', u'öption4', u'cönfig-valué') parent_config = Configuration(None) parent_config.parser.add_section(u'séction1') parent_config.parser.add_section(u'séction2') parent_config.parser.set(u'séction1', u'öption1', u'cönfig-valué') parent_config.parser.set(u'séction1', u'öption2', u'înherited-valué') parent_config.parser.set(u'séction2', u'öption2', u'înherited-valué') self.config.parents = [parent_config] class OptionClass(object): Option(u'séction1', u'öption1', u'dēfault-valué') Option(u'séction1', u'öption2', u'dēfault-valué') Option(u'séction1', u'öption3', u'dēfault-valué') Option(u'séction3', u'öption1', u'dēfault-valué') def test_get_from_config(self): """Value is retrieved from the config.""" self.assertEqual(u'cönfig-valué', self.config.get(u'séction1', u'öption1')) def test_get_from_inherited(self): """Value not specified in the config is retrieved from the inherited config. """ self.assertEqual(u'înherited-valué', self.config.get(u'séction1', u'öption2')) def test_get_from_default(self): """Value not specified in the config or the inherited config is retrieved from the option default. """ self.assertEqual(u'dēfault-valué', self.config.get(u'séction1', u'öption3')) def test_get_is_cached(self): """Value is cached on first retrieval from the parser.""" option1 = self.config.get(u'séction1', u'öption1') self.config.parser.set(u'séction1', u'öption1', u'cönfig-valué2') self.assertIs(self.config.get(u'séction1', u'öption1'), option1) def test_contains_from_config(self): """Contains returns `True` for section defined in config.""" self.assertTrue(u'séction1' in self.config) def test_contains_from_inherited(self): """Contains returns `True` for section defined in inherited config.""" self.assertTrue(u'séction2' in self.config) def test_contains_from_default(self): """Contains returns `True` for section defined in an option.""" self.assertTrue(u'séction3' in self.config) def test_remove_from_config(self): """Value is removed from configuration.""" self.config.remove(u'séction1', u'öption4') parser = self.config.parser self.assertFalse(parser.has_option(u'séction1', u'öption4')) self.assertEqual('', self.config.get(u'séction1', u'öption4')) def test_remove_leaves_inherited_unchanged(self): """Value is not removed from inherited configuration.""" self.config.remove(u'séction1', u'öption2') parser = self.config.parents[0].parser self.assertTrue(parser.has_option(u'séction1', u'öption1')) self.assertEqual(u'înherited-valué', self.config.get(u'séction1', u'öption2'))
class ConfigurationTestCase(unittest.TestCase): def setUp(self): self.config = Configuration(None) self.config.parser.add_section(u'séction1') self.config.parser.set(u'séction1', u'öption1', u'cönfig-valué') self.config.parser.set(u'séction1', u'öption4', u'cönfig-valué') parent_config = Configuration(None) parent_config.parser.add_section(u'séction1') parent_config.parser.add_section(u'séction2') parent_config.parser.set(u'séction1', u'öption1', u'cönfig-valué') parent_config.parser.set(u'séction1', u'öption2', u'înherited-valué') parent_config.parser.set(u'séction2', u'öption2', u'înherited-valué') self.config.parents = [parent_config] class OptionClass(object): Option(u'séction1', u'öption1', u'dēfault-valué') Option(u'séction1', u'öption2', u'dēfault-valué') Option(u'séction1', u'öption3', u'dēfault-valué') Option(u'séction3', u'öption1', u'dēfault-valué') def test_get_from_config(self): """Value is retrieved from the config.""" self.assertEqual(u'cönfig-valué', self.config.get(u'séction1', u'öption1')) def test_get_from_inherited(self): """Value not specified in the config is retrieved from the inherited config. """ self.assertEqual(u'înherited-valué', self.config.get(u'séction1', u'öption2')) def test_get_from_default(self): """Value not specified in the config or the inherited config is retrieved from the option default. """ self.assertEqual(u'dēfault-valué', self.config.get(u'séction1', u'öption3')) def test_get_is_cached(self): """Value is cached on first retrieval from the parser.""" option1 = self.config.get(u'séction1', u'öption1') self.config.parser.set(u'séction1', u'öption1', u'cönfig-valué2') self.assertIs(self.config.get(u'séction1', u'öption1'), option1) def test_contains_from_config(self): """Contains returns `True` for section defined in config.""" self.assertIn(u'séction1', self.config) def test_contains_from_inherited(self): """Contains returns `True` for section defined in inherited config.""" self.assertIn(u'séction2', self.config) def test_contains_from_default(self): """Contains returns `True` for section defined in an option.""" self.assertIn(u'séction3', self.config) def test_remove_from_config(self): """Value is removed from configuration.""" self.config.remove(u'séction1', u'öption4') parser = self.config.parser self.assertFalse(parser.has_option(u'séction1', u'öption4')) self.assertEqual('', self.config.get(u'séction1', u'öption4')) def test_remove_leaves_inherited_unchanged(self): """Value is not removed from inherited configuration.""" self.config.remove(u'séction1', u'öption2') parser = self.config.parents[0].parser self.assertTrue(parser.has_option(u'séction1', u'öption1')) self.assertEqual(u'înherited-valué', self.config.get(u'séction1', u'öption2'))