def test_nested_env_file_fails_if_self_referencial(mocker): contents = "# >> local.env\nBAR=baz" mocker.patch("envfiles.envfiles.open", return_value=StringIO(contents)) mocker.patch("envfiles.envfiles.Path.exists", return_value=True) with pytest.raises(envfiles.EnvFilesError): envfiles.load("local.env")
def test_env_file(mocker, contents, expected): mocker.patch("envfiles.envfiles.open", return_value=StringIO(contents)) mocker.patch("envfiles.envfiles.Path.exists", return_value=True) env_vars = envfiles.load("some/path") assert env_vars == expected
def test_layered_env_files(): env_vars = envfiles.load("tests/assets/test.env") assert env_vars == { "CACHE_ENABLED": "0", "DATABASE_HOST": "localhost", "DATABASE_NAME": "myapp_test", }
def test_nested_env_file_overrides_previous_values(mocker): contents = "FOO=override-me\n# >> base.env\nBAR=baz" base_contents = "FOO=bar" mocker.patch( "envfiles.envfiles.open", side_effect=[StringIO(contents), StringIO(base_contents)], ) mocker.patch("envfiles.envfiles.Path.exists", return_value=True) env_vars = envfiles.load("some/path") assert env_vars == {"FOO": "bar", "BAR": "baz"}
def get_apikey(directory: str = None): if not directory: apikey = get_apikey('.') if apikey: return apikey # fallback directory = os.path.dirname(__file__) try: env = envfiles.load( os.getenv('APIKEY', default=os.path.join(directory, KEY_FILE))) return env.get(APIKEY, None) except envfiles.EnvFilesError: pass return None
"format": "[%(asctime)s][%(name)s][%(levelname)s]: %(message)s", "datefmt": "%Y-%m-%d %H:%M:%S", } }, "handlers": { "console": { "class": "logging.StreamHandler", "level": self.LOGGING_LEVEL, "formatter": "default", } }, "loggers": { "": { "handlers": ["console"], "level": self.LOGGING_LEVEL, "propagate": True, } }, } env_file = os.getenv("ENV_FILE") if env_file: env = envfiles.load(env_file) env.update(os.environ) else: env = os.environ settings = environ.to_config(Config, env)
def test_path_does_not_exist(): with pytest.raises(envfiles.EnvFilesError): envfiles.load("path/to/nowhere")