def test_custom_returned_slot(expected_path): """Test custom config file name can be returned from the default paths""" with patch('atkinson.config.search._check_available') as aval_mock: file_name = 'my_config.yml' expected = [os.path.join(expected_path, file_name)] check = CheckAvailMock(expected) aval_mock.side_effect = check.check assert list(search.get_config_files(filenames=file_name)) == expected
def test_default_slots(expected_path): """Test config returned from each default path individually""" with patch('atkinson.config.search._check_available') as aval_mock: file_name = 'config.yml' expected = os.path.join(expected_path, file_name) check = CheckAvailMock(expected) aval_mock.side_effect = check.check assert list(search.get_config_files()) == [expected]
def test_several_confs_no_override(): """ Test that several configs can be found""" with patch('atkinson.config.search._check_available') as aval_mock: file_names = ['configA.yml', 'configB.yml'] expected = [os.path.join('/etc/atkinson', x) for x in file_names] check = CheckAvailMock(expected) aval_mock.side_effect = check.check assert list(search.get_config_files(filenames=file_names)) == expected
def test_default_returned_all(): """Test config returned from ~/.atkinson""" with patch('atkinson.config.search._check_available') as aval_mock: aval_mock.return_value = True expected = [ os.path.join(x, 'config.yml') for x in search.DEFAULT_CONFIG_PATHS ] assert list(search.get_config_files()) == expected
def test_default_override(): """Test default config from override""" with patch('atkinson.config.search._check_available') as aval_mock: file_name = 'config.yml' override = '~/my_config_dir' expected = [os.path.join(os.path.expanduser(override), file_name)] check = CheckAvailMock(expected) aval_mock.side_effect = check.check assert list(search.get_config_files(overrides=override)) == expected
def test_override_extra_defaults(): """Test an extra filename, override and default filename returns the correct order""" with patch('atkinson.config.search._check_available') as aval_mock: override = '/opt/configs' expected = ['/opt/configs/config.yml', '/opt/configs/extra.yml'] check = CheckAvailMock(expected) aval_mock.side_effect = check.check actual = list( search.get_config_files(filenames='extra.yml', overrides=override)) assert actual == expected
def test_several_confs_mix_override(): """Test that several configs can be found in overrides and defaults""" with patch('atkinson.config.search._check_available') as aval_mock: file_names = ['configA.yml', 'configB.yml'] override = '/opt/configs' expected = ['/etc/atkinson/configB.yml', '/opt/configs/configA.yml'] check = CheckAvailMock(expected) aval_mock.side_effect = check.check actual = list( search.get_config_files(filenames=file_names, overrides=override)) assert actual == expected
def test_default_second_override(): """Test overrides are given, the file is found in the second.""" with patch('atkinson.config.search._check_available') as aval_mock: my_overrides = ['~/my_config_dir', '~/my_second_configs'] file_name = 'config.yml' expected = [ os.path.join(os.path.expanduser(x), file_name) for x in my_overrides ] check = CheckAvailMock(expected) aval_mock.side_effect = check.check actual = list(search.get_config_files(overrides=my_overrides)) assert actual == expected
def __init__(self, filenames=None, paths=None, defaults=True): """ Constructor :param filenames: The file name(s) to load :param paths: Additional paths to use in the search :param defaults: Use the default file name and search paths """ self._config_data = {} self._config_files = [] for found_config in get_config_files(filenames=filenames, overrides=paths, add_defaults=defaults): self._parse(found_config)
def test_default_override_not_found(): """Test an override is given but the file is not found. Fall back to the defaults. """ with patch('atkinson.config.search._check_available') as aval_mock: my_overrides = ['~/my_config_dir'] file_name = 'config.yml' expected = [ os.path.join(os.path.expanduser(x), file_name) for x in my_overrides ] check = CheckAvailMock(expected) aval_mock.side_effect = check.check actual = list(search.get_config_files(overrides=my_overrides)) assert actual == expected
def test_default_not_found(): """Test config can't be found in the default paths""" with patch('atkinson.config.search._check_available') as aval_mock: aval_mock.return_value = False assert list(search.get_config_files()) == []