Skip to content

Commit

Permalink
Better config test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
snorklerjoe committed Jun 20, 2024
1 parent c7833ff commit d4debf9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/pvhotwatercore/common/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from tomlkit.container import Container
from tomlkit.items import Item

# Valid config files will end with this
VALID_CONFIG_EXTENSION = ".toml"


class Config:
"""Represents a single config file.
Expand Down Expand Up @@ -74,13 +77,19 @@ def __init__(self, filename: str) -> None:
filename (str): Filename of toml config file, without a path
Raises:
ValueError: if filename contains a path separator
ValueError: if filename contains a path separator or does not have the
right extension
"""
if os.sep in filename:
raise ValueError("Filename cannot contain a path. "
"The config search path should be established statically"
"with Config.set_config_path()")

if not filename.endswith(VALID_CONFIG_EXTENSION):
raise ValueError(
f"The filename must end in {VALID_CONFIG_EXTENSION}."
)

Config._CONFIG_PATH_ACCESSED = True

self._conf_file_path: str = os.path.join(Config._CONFIG_PATH, filename)
Expand Down
26 changes: 26 additions & 0 deletions core/pvhotwatercore/common/tests/test_config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,29 @@ def test_config_load(tmp_conf_path):
assert a['Test']['a'] == 2
assert a['Test']['b'] == "bumble"
assert a['Test']['today']['napoleon']['plan'] == "whateverIFeelLikeGosh"


def test_instantiation_with_fullpath():
with pytest.raises(ValueError):
config.Config("a" + os.sep + "b.toml") # Contains os.sep

try:
assert config.Config("b.toml") is not None
except FileNotFoundError:
pass


def test_instantiation_with_badext():
with pytest.raises(ValueError):
config.Config("test.conf")
with pytest.raises(ValueError):
config.Config("test.yaml")
with pytest.raises(ValueError):
config.Config("test.json")
with pytest.raises(ValueError):
config.Config("test.ini")

try:
assert config.Config("test.toml") is not None
except FileNotFoundError:
pass

0 comments on commit d4debf9

Please sign in to comment.