Esempio n. 1
0
 def test_load_unicode_file(self):
     """
     System test for loading unicode data.
     """
     config_content = (
         u'[another_muță]\n'
         u'another_muță_option: another_muță_value\n'
         u'[some_section]\n'
         u'some_section_option: some_section_value\n'
         u'')
     test_filesystem = manufacture.fs
     test_segments = None
     try:
         test_segments = test_filesystem.createFileInTemp(
             content=config_content)
         config_path = test_filesystem.getRealPathFromSegments(
             test_segments)
         with test_filesystem._impersonateUser():
             config = FileConfigurationProxy(
                 configuration_path=config_path)
             config.load()
             sections = config.sections
             self.assertEqual(2, len(sections))
             self.assertTrue(isinstance(sections[0], unicode))
             self.assertTrue(u'another_muță' in sections)
             self.assertTrue(u'some_section' in sections)
             self.assertEqual(
                 u'another_muță_value',
                 config.getString(
                     u'another_muță',
                     u'another_muță_option'))
     finally:
         if test_segments:
             test_filesystem.deleteFile(test_segments, ignore_errors=True)
Esempio n. 2
0
    def check_setStringOrNone(self, getter_name, setter_name):
        """
        Helper for testing string values which can be disabled.
        """
        config_file = StringIO(
            u'[some_section]\n'
            u'some_none: something\n'
            u'',
            )
        config = FileConfigurationProxy(
            configuration_file=config_file)
        config.load()
        getter = getattr(config, getter_name)
        setter = getattr(config, setter_name)

        self.assertEqual(
            u'something',
            getter(u'some_section', u'some_none'))

        setter(u'some_section', u'some_none', None)
        self.assertIsNone(
            getter(u'some_section', u'some_none'))

        setter(u'some_section', u'some_none', 'None')
        self.assertIsNone(
            getter(u'some_section', u'some_none'))
Esempio n. 3
0
    def check_setStringOrInherit(self, getter_name, setter_name):
        """
        Helper for setting string values which can be disabled.
        """
        config_file = StringIO(
            u'[some_section]\n'
            u'some_none: something\n'
            u'',
            )
        config = FileConfigurationProxy(
            configuration_file=config_file)
        config.load()
        getter = getattr(config, getter_name)
        setter = getattr(config, setter_name)
        self.assertEqual(
            u'something',
            getter(u'some_section', u'some_none'))
        setter(
            u'some_section', u'some_none', CONFIGURATION_INHERIT[0].upper())
        self.assertEqual(
            CONFIGURATION_INHERIT[0],
            getter(u'some_section', u'some_none'))

        setter(
            u'some_section', u'some_none', CONFIGURATION_INHERIT[1])
        self.assertEqual(
            CONFIGURATION_INHERIT[0],
            getter(u'some_section', u'some_none'))
Esempio n. 4
0
class DummyConfigurationFileMixin(ConfigurationFileMixin):
    """
    A test class implementing `ConfigurationFileMixin`.
    """

    class DummyConfigurationSection(ConfigurationSectionMixin):
        """
        A dummy configuration section used to help with testing.
        """

        _section_name = 'section1'
        _prefix = 'sec'

        def __init__(self, sign='sign'):
            self.property1 = u'value1-' + sign
            self.property2 = u'value2-' + sign
            self.hiden_property = u'value3-' + sign

    def __init__(self, configuration_path=None, configuration_file=None):
        self.checkConfigurationFileAndPathArguments(
            configuration_path, configuration_file)
        self._proxy = FileConfigurationProxy(
            configuration_path=configuration_path,
            configuration_file=configuration_file)
        self._proxy.load()
        self.section1 = self.DummyConfigurationSection(sign=u'1')
        self.section2 = self.DummyConfigurationSection(sign=u'2')

    @property
    def section_names(self):
        return [u'section1', u'section2']
Esempio n. 5
0
 def makeFileConfigurationProxy(self, content=None, defaults=None):
     if content is None:
         content = ''
     proxy_file = FileConfigurationProxy(
         configuration_file=StringIO(content),
         defaults=defaults)
     proxy_file.load()
     return proxy_file
Esempio n. 6
0
 def makeFileConfiguration(self, content, defaults=None):
     """
     Returns a file configuration with `content`.
     """
     config_file = StringIO(content)
     config = FileConfigurationProxy(
         configuration_file=config_file, defaults=defaults)
     config.load()
     return config
Esempio n. 7
0
 def test_load_file(self):
     """
     Check loading settings from a file.
     """
     config_file = StringIO(
         u'[another_muță]')
     config = FileConfigurationProxy(
         configuration_file=config_file)
     self.assertEqual([], config.sections)
     config.load()
     self.assertEqual(u'another_muță', config.sections[0])
Esempio n. 8
0
    def test_isDisabledValue(self):
        """
        Return `True` if value is one of the disabled values or `False`
        otherwise.
        """
        config = FileConfigurationProxy(configuration_file=StringIO())

        self.assertFalse(config.isDisabledValue(u'some-value'))

        self.assertTrue(config.isDisabledValue('disable'))
        self.assertTrue(config.isDisabledValue('Disable'))
        self.assertTrue(config.isDisabledValue('disabled'))
Esempio n. 9
0
 def test_bad_section_header(self):
     """
     An error is raised if a section name is bad formated.
     """
     config_file = StringIO(
         '[section\n'
         'new_option: value\n'
         '[remote_admin]\n'
         'remote_admin_enabled: No\n')
     config = FileConfigurationProxy(configuration_file=config_file)
     with self.assertRaises(UtilsError) as context:
         config.load()
     self.assertEqual(u'1002', context.exception.event_id)
     config_file.close()
Esempio n. 10
0
 def test_getString_quotes(self):
     """
     String values may be quoted.
     """
     config_file = StringIO(
         u'[section]\n'
         u'no_quote: \'maca" \n'
         u'single_quote: \'maca\' \n'
         u'double_quote: "maca" \n'
         u'multiple_single_quote: \'\'maca\'\' \n'
         u'multiple_double_quote: ""maca"" \n'
         u'',
         )
     config = FileConfigurationProxy(
         configuration_file=config_file)
     config.load()
     self.assertEqual(
         u'\'maca"',
         config.getString(u'section', u'no_quote'))
     self.assertEqual(
         u'maca',
         config.getString(u'section', u'single_quote'))
     self.assertEqual(
         u'maca',
         config.getString(u'section', u'double_quote'))
     self.assertEqual(
         u'\'maca\'',
         config.getString(u'section', u'multiple_single_quote'))
     self.assertEqual(
         u'"maca"',
         config.getString(u'section', u'multiple_double_quote'))
Esempio n. 11
0
 def test_hasSection(self):
     """
     Check hasSection.
     """
     config_file = StringIO(
         u'[some_section]\n'
         u'[another_muță]\n'
         u'some_value: 3\n'
         u'',
         )
     config = FileConfigurationProxy(
         configuration_file=config_file)
     config.load()
     self.assertTrue(config.hasSection(u'some_section'))
     self.assertTrue(config.hasSection(u'another_muță'))
Esempio n. 12
0
 def test_add_section(self):
     """
     Check adding a section.
     """
     config_file = StringIO(
         u'[another_muță]\n'
         u'some_value: 3\n'
         u'',
         )
     config = FileConfigurationProxy(
         configuration_file=config_file)
     config.load()
     self.assertEqual(1, len(config.sections))
     config.addSection(u'another_muță2')
     self.assertEqual(2, len(config.sections))
     self.assertTrue(u'another_muță2' in config.sections)
Esempio n. 13
0
 def test_getString_with_wild_charactes(self):
     """
     A string configuration can hold any character and it does not
     require any special escaping.
     """
     config_file = StringIO((
         u'[section]\n'
         u'value: %s\n'
         u'') % (self.special_characters)
         )
     config = FileConfigurationProxy(
         configuration_file=config_file)
     config.load()
     self.assertEqual(
         self.special_characters,
         config.getString(u'section', u'value'))
Esempio n. 14
0
 def test_getString_with_python_formating(self):
     """
     A string configuration can hold a python string formating
     expressions.
     """
     config_file = StringIO(
         u'[section]\n'
         u'value: do %(some)s value\n'
         u'',
         )
     config = FileConfigurationProxy(
         configuration_file=config_file)
     config.load()
     self.assertEqual(
         u'do %(some)s value',
         config.getString(u'section', u'value'))
Esempio n. 15
0
 def __init__(self, configuration_path=None, configuration_file=None):
     self.checkConfigurationFileAndPathArguments(
         configuration_path, configuration_file)
     self._proxy = FileConfigurationProxy(
         configuration_path=configuration_path,
         configuration_file=configuration_file)
     self._proxy.load()
     self.section1 = self.DummyConfigurationSection(sign=u'1')
     self.section2 = self.DummyConfigurationSection(sign=u'2')
Esempio n. 16
0
 def check_getStringOrNone(self, getter_name):
     """
     Helper for retrieving strings which can be disabled.
     """
     config_file = StringIO(
         u'[some_section]\n'
         u'some_none:  None   \n'
         u'some_disable: disable\n'
         u'some_disabled: DiSabled\n'
         u'',
         )
     config = FileConfigurationProxy(
         configuration_file=config_file)
     config.load()
     getter = getattr(config, getter_name)
     self.assertIsNone(getter(u'some_section', u'some_none'))
     self.assertIsNone(getter(u'some_section', u'some_disable'))
     self.assertIsNone(getter(u'some_section', u'some_disabled'))
Esempio n. 17
0
 def test_sections(self):
     """
     Check sections property.
     """
     config_file = StringIO(
         u'[some_section]\n'
         u'[another_muță]\n'
         u'[yet_another]\n'
         u'some_value: 3\n'
         u'',
         )
     config = FileConfigurationProxy(
         configuration_file=config_file)
     config.load()
     sections = config.sections
     self.assertEqual(3, len(sections))
     self.assertTrue(u'some_section' in sections)
     self.assertTrue(u'another_muță' in sections)
     self.assertTrue(u'yet_another' in sections)
Esempio n. 18
0
 def test_getString(self):
     """
     Check getString.
     """
     config_file = StringIO(
         u'[some_section]\n'
         u'some_value:  maca   \n'
         u'[another_muță]\n'
         u'some_value: raca\n'
         u'',
         )
     config = FileConfigurationProxy(
         configuration_file=config_file)
     config.load()
     self.assertEqual(
         u'maca',
         config.getString(u'some_section', u'some_value'))
     self.assertEqual(
         u'raca',
         config.getString(u'another_muță', u'some_value'))
Esempio n. 19
0
    def check_getStringOrInherit(self, getter_name):
        """
        Helper for getting inheritable strings.
        """
        config_file = StringIO(
            u'[some_section]\n'
            u'some_none: Inherit   \n'
            u'some_disable: inherited\n'
            u'',
            )
        config = FileConfigurationProxy(
            configuration_file=config_file)
        config.load()

        getter = getattr(config, getter_name)
        self.assertEqual(
            CONFIGURATION_INHERIT[0],
            getter(u'some_section', u'some_none'))
        self.assertEqual(
            CONFIGURATION_INHERIT[0],
            getter(u'some_section', u'some_disable'))
Esempio n. 20
0
    def test_remove_section(self):
        """
        Check removing a section.
        """
        section_name = u'another_muță'
        config_file = StringIO((
            u'[%s]\n'
            u'some_value: 3\n'
            u''
            ) % (section_name))
        config = FileConfigurationProxy(
            configuration_file=config_file)
        config.load()
        self.assertTrue(config.hasSection(section_name))

        config.removeSection(section_name)

        self.assertFalse(config.hasSection(section_name))
Esempio n. 21
0
    def test_setString_with_wild_charactes(self):
        """
        A string configuration can be set to text containing any characters.
        """
        config_file = StringIO(
            u'[section]\n'
            u'value: value\n'
            u'',
            )
        config = FileConfigurationProxy(
            configuration_file=config_file)
        config.load()

        config.setString(u'section', u'value', self.special_characters)

        self.assertEqual(
            self.special_characters,
            config.getString(u'section', u'value'))
Esempio n. 22
0
 def test_save(self):
     """
     System test for persisting changes into a file.
     """
     config_content = (
         u'[another_muță]\n'
         u'another_muță_option: another_muță_value\n\n')
     test_filesystem = manufacture.fs
     test_segments = None
     try:
         test_segments = test_filesystem.createFileInTemp(
             content=config_content)
         config_path = test_filesystem.getRealPathFromSegments(
             test_segments)
         with test_filesystem._impersonateUser():
             config = FileConfigurationProxy(
                 configuration_path=config_path)
             config.load()
             self.assertEqual(
                 u'another_muță_value',
                 config.getString(
                     u'another_muță',
                     u'another_muță_option'))
             config.setString(
                     u'another_muță',
                     u'another_muță_option',
                     u'muță_value',
                     )
             config.save()
             config = FileConfigurationProxy(
                 configuration_path=config_path)
             config.load()
             self.assertEqual(
                 u'muță_value',
                 config.getString(
                     u'another_muță',
                     u'another_muță_option'))
     finally:
         if test_segments:
             test_filesystem.deleteFile(test_segments, ignore_errors=True)