def load_config_from_cli(config: GoodConf, argv: List[str]) -> List[str]: """Loads config, checking CLI arguments for a config file""" # Monkey patch Django's command parser from django.core.management.base import BaseCommand original_parser = BaseCommand.create_parser def patched_parser(self, prog_name, subcommand): parser = original_parser(self, prog_name, subcommand) argparser_add_argument(parser, config) return parser BaseCommand.create_parser = patched_parser try: parser = argparse.ArgumentParser(add_help=False) argparser_add_argument(parser, config) config_arg, default_args = parser.parse_known_args(argv) config.load(config_arg.config) yield default_args finally: # Put that create_parser back where it came from or so help me! BaseCommand.create_parser = original_parser
def test_provided_file(mocker, tmpdir): mocked_load_config = mocker.patch("goodconf._load_config") path = tmpdir.join("myapp.json") path.write("") g = GoodConf() g.load(str(path)) mocked_load_config.assert_called_once_with(str(path)) assert g.Config._config_file == str(path)
def test_mgmt_command(mocker, tmpdir): mocked_load_config = mocker.patch('goodconf._load_config') mocked_dj_execute = mocker.patch( 'django.core.management.execute_from_command_line') temp_config = tmpdir.join('config.yml') temp_config.write('') c = GoodConf() dj_args = ['manage.py', 'diffsettings', '-v', '2'] c.django_manage(dj_args + ['-C', str(temp_config)]) mocked_load_config.assert_called_once_with(str(temp_config)) mocked_dj_execute.assert_called_once_with(dj_args)
def test_conf_env_var(mocker, tmpdir): mocked_load_config = mocker.patch('goodconf._load_config') path = tmpdir.join('myapp.json') path.write('') with env_var('CONF', str(path)): GoodConf(file_env_var='CONF').load() mocked_load_config.assert_called_once_with(str(path))
def test_default_files(mocker, tmpdir): mocked_load_config = mocker.patch('goodconf._load_config') path = tmpdir.join('myapp.json') path.write('') bad_path = tmpdir.join('does-not-exist.json') GoodConf(default_files=[str(bad_path), str(path)]).load() mocked_load_config.assert_called_once_with(str(path))
def test_help(mocker, tmpdir, capsys): mocker.patch('sys.exit') mocked_load_config = mocker.patch('goodconf._load_config') temp_config = tmpdir.join('config.yml') temp_config.write('') c = GoodConf(file_env_var='MYAPP_CONF', default_files=['/etc/myapp.json']) assert c.file_env_var == 'MYAPP_CONF' c.django_manage([ 'manage.py', 'diffsettings', '-C', str(temp_config), '--settings', __name__, '-h' ]) mocked_load_config.assert_called_once_with(str(temp_config)) output = capsys.readouterr() assert '-C FILE, --config FILE' in output.out assert 'MYAPP_CONF' in output.out assert '/etc/myapp.json' in output.out
def test_provided_file(mocker, tmpdir): mocked_load_config = mocker.patch('goodconf._load_config') path = tmpdir.join('myapp.json') path.write('') GoodConf().load(str(path)) mocked_load_config.assert_called_once_with(str(path))
def test_all_env_vars(mocker): mocked_set_values = mocker.patch('goodconf.GoodConf.set_values') GoodConf().load() mocked_set_values.assert_called_once_with({})
def test_undefined(): c = GoodConf() with pytest.raises(AttributeError): c.UNDEFINED