From 709d4ca60dd91a2075477bad89b358a0f644933e Mon Sep 17 00:00:00 2001 From: Martin Stajnko Date: Tue, 25 Aug 2026 22:10:25 +0200 Subject: [PATCH 1/4] chore: declare pytest in one place only --- pyproject.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ea9ebe3..8f9c2d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,9 +5,6 @@ description = "Replace configured values in a text file." requires-python = ">=3.9" dependencies = [] -[project.optional-dependencies] -dev = ["pytest>=7.0"] - [tool.pytest.ini_options] pythonpath = ["."] testpaths = ["tests"] From 721d21a603684be9f78deb8898d021258fe3e27e Mon Sep 17 00:00:00 2001 From: Martin Stajnko Date: Tue, 25 Aug 2026 22:10:56 +0200 Subject: [PATCH 2/4] update --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index f0d93d4..083538c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ venv/ # assignment brief - kept locally, not published instructions.md +config2.txt +input2.txt \ No newline at end of file From dc401be837e240e263ae7e84d69500fe392e789a Mon Sep 17 00:00:00 2001 From: Martin Stajnko Date: Wed, 26 Aug 2026 14:35:32 +0200 Subject: [PATCH 3/4] fix: reject a configuration entry with an empty value1 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. --- config_loader.py | 9 +++++++++ replacer.py | 2 ++ tests/unit/test_config_loader.py | 25 ++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/config_loader.py b/config_loader.py index 13361e9..970c568 100644 --- a/config_loader.py +++ b/config_loader.py @@ -18,6 +18,15 @@ def load_config(filename: str) -> list[tuple[str, str]]: ) value1, value2 = line.split("=", 1) + + # An empty value1 would match at every position without + # consuming anything, leaving replace_line unable to advance. + if not value1: + raise ValueError( + f"Invalid configuration at line {line_number}: {line!r} " + f"(the value to replace must not be empty)" + ) + pairs.append((value1, value2)) return pairs \ No newline at end of file diff --git a/replacer.py b/replacer.py index 1623f18..d462ccd 100644 --- a/replacer.py +++ b/replacer.py @@ -24,6 +24,8 @@ def replace_line(line: str, pairs: list[tuple[str, str]]) -> tuple[str, int]: replaced_symbols = 0 position = 0 + # Terminates because every branch below advances `position`, which + # holds only while no value1 is empty. `load_config` rejects those. while position < len(line): match = None diff --git a/tests/unit/test_config_loader.py b/tests/unit/test_config_loader.py index d4b53e4..ba9edba 100644 --- a/tests/unit/test_config_loader.py +++ b/tests/unit/test_config_loader.py @@ -55,4 +55,27 @@ def test_empty_config_file(tmp_path): config_file = tmp_path / "config.txt" config_file.write_text("", encoding="utf-8") - assert load_config(str(config_file)) == [] \ No newline at end of file + 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)) From 800ded7819a5d0281bc7ce1951f26e23470caab4 Mon Sep 17 00:00:00 2001 From: Martin Stajnko Date: Wed, 26 Aug 2026 14:45:11 +0200 Subject: [PATCH 4/4] update --- .gitignore | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 083538c..04b6a21 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,8 @@ venv/ .idea/ .vscode/ -# assignment brief - kept locally, not published +# assignment brief instructions.md -config2.txt -input2.txt \ No newline at end of file + +# local experiments +scratch/