Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/instrumentserver/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def loadConfig(
yaml = ruamel.yaml.YAML()
rawConfig = yaml.load(configPath)

# ruamel.yaml returns ``None`` for an empty (or comments-only) config
# file. The membership test on the next line would then raise an opaque
# ``TypeError: argument of type 'NoneType' is not iterable``, so we
# surface a descriptive error that matches the style of the missing-key
# message below.
if rawConfig is None:
raise AttributeError(
f"The config file '{configPath}' is empty. "
"It needs at least an 'instruments:' section."
)

if "instruments" not in rawConfig:
raise AttributeError(
"All configurations must be inside the 'instruments' field. "
Expand Down
33 changes: 33 additions & 0 deletions test/pytest/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,39 @@ def test_missing_instruments_key_raises(tmp_path):
loadConfig(cfg)


# ---------------------------------------------------------------------------
# Error: empty / comments-only config file
# ---------------------------------------------------------------------------


def test_empty_config_raises(tmp_path):
"""An empty config file must not crash with an opaque TypeError."""
cfg = _write_config(tmp_path, "")
with pytest.raises(AttributeError, match="empty"):
loadConfig(cfg)


def test_comments_only_config_raises(tmp_path):
"""A config file that contains only comments also parses to ``None``."""
cfg = _write_config(
tmp_path,
"""\
# this file is intentionally just comments
# nothing else
""",
)
with pytest.raises(AttributeError, match="empty"):
loadConfig(cfg)


def test_empty_config_error_mentions_instruments(tmp_path):
"""The empty-config error should guide the user toward the required key."""
cfg = _write_config(tmp_path, "")
with pytest.raises(AttributeError) as excinfo:
loadConfig(cfg)
assert "instruments" in str(excinfo.value)


# ---------------------------------------------------------------------------
# pollingRate
# ---------------------------------------------------------------------------
Expand Down