A line such as "=z" parsed into the pair ("", "z"). An empty value1
matches at every position and consumes nothing, so the scan in
replace_line never advanced and the program looped forever, printing
nothing and growing its output buffer until it was killed.
Reject the entry while parsing, reporting the offending line the same
way as a line with no "=" at all, and document the termination
invariant the loop depends on.
82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
"""Unit tests for config_loader.py"""
|
|
|
|
import pytest
|
|
from config_loader import load_config
|
|
|
|
|
|
def test_valid_config(tmp_path):
|
|
config_file = tmp_path / "config.txt"
|
|
config_file.write_text("a=z\nbb=y\nc=x\n")
|
|
pairs = load_config(str(config_file))
|
|
assert pairs == [("a", "z"), ("bb", "y"), ("c", "x")]
|
|
|
|
|
|
def test_skips_blank_lines(tmp_path):
|
|
config_file = tmp_path / "config.txt"
|
|
config_file.write_text("a=z\n\nbb=y\n")
|
|
pairs = load_config(str(config_file))
|
|
assert pairs == [("a", "z"), ("bb", "y")]
|
|
|
|
|
|
def test_invalid_line_raises_value_error(tmp_path):
|
|
config_file = tmp_path / "config.txt"
|
|
config_file.write_text("a=z\nnoequalsign\n")
|
|
with pytest.raises(ValueError):
|
|
load_config(str(config_file))
|
|
|
|
|
|
def test_value_can_contain_equals_sign(tmp_path):
|
|
"""split('=', 1) should only split on the FIRST '=', so value2
|
|
is allowed to contain additional '=' characters."""
|
|
config_file = tmp_path / "config.txt"
|
|
config_file.write_text("a=x=y\n")
|
|
pairs = load_config(str(config_file))
|
|
assert pairs == [("a", "x=y")]
|
|
|
|
|
|
def test_trailing_whitespace_in_value_is_preserved(tmp_path):
|
|
"""Whitespace inside value2 is significant and deliberately kept.
|
|
|
|
The configuration format has no quoting or escaping, so stripping
|
|
would make a replacement value ending in a space impossible to
|
|
express. Guarding against accidental trailing spaces is therefore
|
|
left to the caller.
|
|
"""
|
|
config_file = tmp_path / "config.txt"
|
|
config_file.write_text("a=z \n") # trailing space before newline
|
|
|
|
pairs = load_config(str(config_file))
|
|
|
|
assert pairs == [("a", "z ")]
|
|
|
|
|
|
def test_empty_config_file(tmp_path):
|
|
"""An empty configuration is valid and simply replaces nothing."""
|
|
config_file = tmp_path / "config.txt"
|
|
config_file.write_text("", encoding="utf-8")
|
|
|
|
assert load_config(str(config_file)) == []
|
|
|
|
def test_empty_value1_raises_value_error(tmp_path):
|
|
"""A line such as '=z' has no value1 and must be rejected.
|
|
|
|
An empty value1 matches at every position and consumes nothing, so
|
|
the scan in replace_line would never advance past it. Rejecting the
|
|
pair here keeps that invariant a parsing concern rather than
|
|
something the replacement loop has to defend against.
|
|
"""
|
|
config_file = tmp_path / "config.txt"
|
|
config_file.write_text("a=z\n=x\n", encoding="utf-8")
|
|
|
|
with pytest.raises(ValueError):
|
|
load_config(str(config_file))
|
|
|
|
|
|
def test_line_that_is_only_an_equals_sign_raises_value_error(tmp_path):
|
|
"""'=' alone has neither value1 nor value2."""
|
|
config_file = tmp_path / "config.txt"
|
|
config_file.write_text("=\n", encoding="utf-8")
|
|
|
|
with pytest.raises(ValueError):
|
|
load_config(str(config_file))
|