def test_marten_cli(): """Test the output of marten cli command""" d = {'TEST': 1} with print_patch as mock: with patch('marten.config', Configuration(d)): marten_cli() mock.assert_called_once_with('{\n "TEST": 1\n}')
def get_supported_extensions(): """Returns a dict mapping file extension string to Configuration class that can handle it""" supported_extensions = {} for cls in Configuration.__subclasses__(): for ext in cls.file_extensions: supported_extensions[ext] = cls return supported_extensions
def get_config_from_env(): """ Attempt to build and return a Configuration instance based on the files found in the .marten directory matching the MARTEN_ENV environment variable """ marten_dir = os.path.join(os.getcwd(), '.marten') if os.path.isdir(marten_dir): config = parse_directory(marten_dir, os.environ['MARTEN_ENV']) else: config = Configuration({}) return config
def test_replace_env(self): """Test that Configuration._replace_env() correctly replaces $VAR entries""" input_dict = { 'REPLACE': '$VAR', 'EMPTY': '$EMPTY', 'KEEP': 'VAR', 'INTERLACED': '_ $VAR with other content' } output_dict = { 'REPLACE': 'value', 'EMPTY': '$EMPTY', 'KEEP': 'VAR', 'INTERLACED': '_ value with other content' } with mock.patch('os.environ', {'VAR': 'value'}): self.assertEqual(Configuration._replace_env(input_dict), output_dict)
def parse_directory(path, name): """ Parse path for all supported config file types beginning with name Merges multiple configs with the same name into a single Configuration instance, overriding duplicate attributes in order files were loaded Ignores files with unsupported extensions """ merged_raw_configs = {} supported_extensions = get_supported_extensions() for filename in os.listdir(path): if filename.startswith(name): ext = filename.split(name, 1)[1] if ext in supported_extensions: config = supported_extensions[ext](os.path.join( path, filename)) merged_raw_configs.update(config.raw_config) return Configuration(merged_raw_configs)
def test_filter_config(self): """Test that Configuration._filter_config() only returns uppercase attributes not starting with underscore""" self.assertEqual( sorted( Configuration._filter_config(self.sample_source_dict).keys()), sorted(['SHOULD_EXIST', 'NESTED']))
def get_configuration(self): return Configuration(self.sample_source_dict)
def test_filter_config(self): """Test that Configuration._filter_config() only returns uppercase attributes not starting with underscore""" self.assertEqual( sorted(Configuration._filter_config(self.sample_source_dict).keys()), sorted(['SHOULD_EXIST', 'NESTED']) )