Ejemplo n.º 1
0
 def test_gather_configs(self):
     """Tests that the environment variables are properly gathered."""
     config_entry_metas = [
         ConfigEntryMeta(env_var_name='here', acts_config_key='there'),
         ConfigEntryMeta(cli_flags=['not', 'here'])
     ]
     with patch.object(os, 'environ', {'here': 'value'}):
         configs = EnvConfigSource().gather_configs(config_entry_metas)
     self.assertDictEqual({'there': 'value'}, configs)
 def test_add_arguments(self):
     """Tests that _add_arguments properly adds ConfigEntryMetas as args."""
     config_entry_metas = [
         ConfigEntryMeta(cli_flags='--i-have-a-flag'),
         ConfigEntryMeta(help='I do not have a flag')
     ]
     parser = Mock()
     CliConfigSource()._add_arguments(parser, config_entry_metas)
     parser.add_argument.assert_called_once_with(
         '--i-have-a-flag',
         **CliConfigSource._get_cli_kwargs(config_entry_metas[0]))
Ejemplo n.º 3
0
 def test_cli_kwargs_are_attributes_in_config_entry_meta_class(self):
     """Tests that all cli_kwargs exist as attributes in ConfigEntryMeta."""
     config_entry_meta = ConfigEntryMeta()
     for attribute_name in ConfigEntryMeta.cli_kwarg_attributes:
         if not hasattr(config_entry_meta, attribute_name):
             self.fail('Attribute %s exists in ConfigEntryMeta.cli_kwargs, '
                       'but is not an attribute in ConfigEntryMeta.' %
                       attribute_name)
Ejemplo n.º 4
0
    def _get_cli_kwargs(cls, config_entry_meta):
        """Returns a dict of kwargs needed for ArgumentParser.add_argument().

        Args:
            config_entry_meta: The entry to generate add_argument() kwargs for.
        """
        kwarg_dict = {}
        for attr_name in ConfigEntryMeta.cli_kwarg_attributes:
            kwarg = ConfigEntryMeta.attr_to_cli_kwarg(attr_name)
            value = getattr(config_entry_meta, attr_name, None)
            kwarg_dict[kwarg] = value
        return utils.dict_purge_key_if_value_is_none(kwarg_dict)
    def test_get_cli_kwargs_returns_all_cli_kwarg_attributes(self):
        """Tests that _get_cli_kwargs returns all cli kwarg attributes."""
        attributes = {}
        for attr in ConfigEntryMeta.cli_kwarg_attributes:
            attributes[attr] = Mock()
        # This one is renamed:
        constructor_kwargs = dict(attributes)
        constructor_kwargs['acts_config_key'] = constructor_kwargs['cli_dest']
        del constructor_kwargs['cli_dest']
        # Sets all constructor kwargs to their own mock objects.
        meta = ConfigEntryMeta(**constructor_kwargs)

        cli_kwargs = CliConfigSource._get_cli_kwargs(meta)

        expected_dict = {}
        for key, value in attributes.items():
            if value is not None:
                new_key = ConfigEntryMeta.attr_to_cli_kwarg(key)
                expected_dict[new_key] = value

        self.assertDictEqual(expected_dict, cli_kwargs)
Ejemplo n.º 6
0
 def test_get_cli_kwarg_no_prefix(self):
     """Tests that a prefix-less cli_kwarg can be received."""
     kwarg = ConfigEntryMeta.attr_to_cli_kwarg('help')
     self.assertEqual(
         'help', kwarg, 'The cli_ prefix was not removed '
         'properly from the attribute.')
Ejemplo n.º 7
0
 def test_get_cli_kwarg_with_prefix(self):
     """Tests that a prefix-less cli_kwarg can be received."""
     key = ConfigEntryMeta.attr_to_cli_kwarg('cli_nargs')
     self.assertEqual(
         'nargs', key, 'The cli_ prefix was not removed '
         'properly from the attribute.')
Ejemplo n.º 8
0
 def test_config_entry_meta_keeps_cli_flags_as_list(self):
     """Tests that ConfigEntryMeta's cli_flags stays as a list."""
     self.assertTrue(
         type(ConfigEntryMeta(cli_flags=['test']).cli_flags) is list)
Ejemplo n.º 9
0
 def test_config_entry_meta_converts_str_cli_flags_to_a_list(self):
     """Tests that ConfigEntryMeta's cli_flags will return as a list."""
     self.assertTrue(
         type(ConfigEntryMeta(cli_flags='test').cli_flags) is list)