-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from masenf/allow-invalid-tone
Allow invalid tone
- Loading branch information
Showing
6 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import os | ||
|
||
|
||
STR_TO_BOOL = { | ||
"false": False, | ||
"no": False, | ||
"off": False, | ||
"0": False, | ||
0: False, | ||
"true": True, | ||
"yes": True, | ||
"on": True, | ||
"1": True, | ||
1: True, | ||
} | ||
|
||
|
||
def getenv_bool(var_name, default=False): | ||
""" | ||
Retrieve the given environment variable as a bool. | ||
Will use the text translation table STR_TO_BOOL to facilitate the conversion | ||
so that "yes"/"no" and "on"/"off" can also be used. | ||
""" | ||
val = os.environ.get(var_name, None) | ||
if val is None: | ||
return default | ||
return STR_TO_BOOL[val.lower()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import pytest | ||
|
||
import dzcb.util | ||
|
||
|
||
ENV_VAR_NAME = "TEST_UTIL_ENVVAR" | ||
|
||
|
||
@pytest.fixture( | ||
params=[ | ||
("yes", True), | ||
("on", True), | ||
(0, False), | ||
("no", False), | ||
("off", False), | ||
(None, None), # should use default | ||
("foo", KeyError), # should use default | ||
] | ||
) | ||
def exp_env_bool(request, monkeypatch): | ||
env_value, exp_bool_value = request.param | ||
if env_value is not None: | ||
monkeypatch.setenv(ENV_VAR_NAME, env_value) | ||
return exp_bool_value | ||
|
||
|
||
@pytest.mark.parametrize("default", [True, False]) | ||
def test_getenv_bool(exp_env_bool, default): | ||
if isinstance(exp_env_bool, type) and issubclass(exp_env_bool, Exception): | ||
with pytest.raises(exp_env_bool): | ||
_ = dzcb.util.getenv_bool(ENV_VAR_NAME, default=default) | ||
return | ||
|
||
val = dzcb.util.getenv_bool(ENV_VAR_NAME, default=default) | ||
if exp_env_bool is None: | ||
assert val is default | ||
else: | ||
assert val is exp_env_bool |