Exemplo n.º 1
0
    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'))
Exemplo n.º 2
0
        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'))
Exemplo n.º 3
0
        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"))
Exemplo n.º 4
0
    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'))
Exemplo n.º 5
0
    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"))
Exemplo n.º 6
0
    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
        section = config['b']
        section.set('option1', None)
        section = config[u'aä']
        section.set('öption1', 'z')
        section.set('öption2', None)
        # Note: the following would depend on the locale.getpreferredencoding()
        # config.set('a', 'option3', "Voil\xe0 l'\xe9t\xe9") # latin-1
        self.assertEqual('x', config.get(u'aä', u'öption0'))
        self.assertEqual(u"Voilà l'été", config.get(u'aä', 'option1'))
        self.assertEqual(u"Voilà l'été", config.get(u'aä', 'option2'))
        self.assertEqual('', config.get('b', 'option1'))
        self.assertEqual('z', config.get(u'aä', 'öption1'))
        self.assertEqual('', config.get(u'aä', 'öption2'))
        config.save()

        self.assertEqual(['# -*- coding: utf-8 -*-\n',
                          '\n',
                          '[aä]\n',
                          "option1 = Voilà l'été\n",
                          "option2 = Voilà l'été\n",
                          'öption0 = x\n',
                          'öption1 = z\n',
                          'öption2 = \n',
                          # "option3 = Voilà l'été\n",
                          '\n',
                          '[b]\n',
                          'option1 = \n',
                          'öption0 = y\n',
                          '\n'], readlines(self.filename))
        config2 = Configuration(self.filename)
        self.assertEqual('x', config2.get(u'aä', u'öption0'))
        self.assertEqual(u"Voilà l'été", config2.get(u'aä', 'option1'))
        self.assertEqual(u"Voilà l'été", config2.get(u'aä', 'option2'))
Exemplo n.º 7
0
    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'))
Exemplo n.º 8
0
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'))
Exemplo n.º 9
0
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'))