Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

Commit

Permalink
fix KeyError bug
Browse files Browse the repository at this point in the history
Fixes #7 and adds more thorough tests.
  • Loading branch information
bradleygolden committed Oct 3, 2018
1 parent b0065aa commit 2c58060
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
fail_fast: true
repos:
- repo: https://github.com/ambv/black
rev: stable
Expand Down
2 changes: 1 addition & 1 deletion pytailor/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
# |__| \/ \/


VERSION = (0, 1, 2)
VERSION = (0, 1, 3)

__version__ = ".".join(map(str, VERSION))
Empty file added pytailor/config.py
Empty file.
13 changes: 9 additions & 4 deletions pytailor/tailor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import os
import warnings

from .helpers import dotenv_to_dict, str_to_py

Expand Down Expand Up @@ -68,7 +69,11 @@ def from_dotenv(self, path: str):
self.store[name] = value

def watch_env_var(self, name: str):
"""Set configuration and watch a system wideenvironment variable."""
value = os.environ[name]
mod_value = str_to_py(value)
self.env_store[name] = mod_value
"""Set configuration and watch a system wide environment variable."""
value = os.getenv(name)
if not value:
warn_msg = f"Environment variable '{name}' not found."
warnings.warn(warn_msg, RuntimeWarning)
else:
mod_value = str_to_py(value)
self.env_store[name] = mod_value
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[pytest]
markers =
unit
filterwarnings =
ignore::RuntimeWarning
21 changes: 21 additions & 0 deletions tests/test_tailor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@


def setup_function():
try:
del os.environ["FOO"]
except KeyError:
pass
os.environ["FOO"] = "BAR"


Expand Down Expand Up @@ -45,6 +49,23 @@ def test_from_object_and_then_dotenv(env_path):
assert config["TESTING"] is False


def test_watch_env_var_that_doesnt_exist_raises_warning():
config = Tailor()
with pytest.warns(RuntimeWarning) as warn:
config.watch_env_var("BAR")

assert len(warn) == 1
assert "not found" in warn[0].message.args[0]


def test_watch_env_var_that_doesnt_exist_but_exists_in_config_object():
config = Tailor()
config["BAR"] = "BAZ"
config.watch_env_var("BAR")
assert "BAR" in config
assert config["BAR"] == "BAZ"


def test_watch_env_var_and_change_after_watching():
config = Tailor()
config.watch_env_var("FOO")
Expand Down

0 comments on commit 2c58060

Please sign in to comment.