Skip to content

Commit

Permalink
[#232]: fixes bug with mangled toml file
Browse files Browse the repository at this point in the history
  • Loading branch information
g.trantham committed Jul 31, 2023
1 parent e03befe commit 26983e9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/nldi_crawler/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self, *args, **kwargs) -> None:
self.setdefault(_k, _v)

@classmethod
def from_toml(cls, filepath: str) -> CrawlerConfig | None:
def from_toml(cls, filepath: str) -> CrawlerConfig:
"""
Read key configuration values from a TOML-formatted configuration file.
The config file must contain a 'nldi-db' section, else will return an empty
Expand All @@ -64,7 +64,7 @@ def from_toml(cls, filepath: str) -> CrawlerConfig | None:
_ = dbconfig.read(filepath)
if _section_ not in dbconfig.sections():
logging.info(" No '%s' section in configuration file %s.", _section_, filepath)
return None
return cls(retval)
retval["NLDI_DB_HOST"] = dbconfig[_section_].get("hostname").strip("'\"")
retval["NLDI_DB_PORT"] = dbconfig[_section_].get("port").strip("'\"")
retval["NLDI_DB_USER"] = dbconfig[_section_].get("username").strip("'\"")
Expand Down
5 changes: 2 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ def test_toml_broken_config():
"""parse cfg file"""
_test_dir = os.path.dirname(os.path.realpath(__file__))
cfg = CrawlerConfig.from_toml(os.path.join(_test_dir, r"cfg-test-2.toml"))
## This config file does not have an 'nldi-db' section, so the config dict should be empty.
with pytest.raises(KeyError):
assert cfg["NLDI_DB_NAME"] == "test1"
## This config file does not have an 'nldi-db' section, so the config should contain defaults.
assert cfg["NLDI_DB_NAME"] == "nldi"
with pytest.raises(KeyError):
assert cfg["NLDI_DB_PASS"] == "changeMe"

Expand Down

0 comments on commit 26983e9

Please sign in to comment.