Files
Martin Stajnko 40ea68be11 Initial implementation with unit and integration tests
Reads value1=value2 pairs from a configuration file and replaces every
non-overlapping occurrence of value1 with value2 in a text file, printing
the resulting lines sorted by the number of symbols replaced.

Overlapping patterns resolve to the longest match so the result does not
depend on configuration file ordering. Ties keep their original file order.
2026-08-25 21:21:38 +02:00

140 lines
4.0 KiB
Python

"""
End-to-end tests that invoke main.py as a real subprocess.
These cover what unit tests cannot: argument handling, exit status,
stderr messages, and the final sort order as actually printed.
"""
import subprocess
import sys
from pathlib import Path
import pytest
PROJECT_ROOT = Path(__file__).resolve().parents[2]
MAIN = PROJECT_ROOT / "main.py"
def run_cli(*args: str) -> subprocess.CompletedProcess:
"""Invoke main.py with the given arguments."""
return subprocess.run(
[sys.executable, str(MAIN), *args],
capture_output=True,
text=True,
cwd=PROJECT_ROOT,
)
def assert_clean_error(result: subprocess.CompletedProcess) -> None:
"""A failure must be a user-facing message, never a stack trace.
Asserted explicitly because an escaping exception also exits with
status 1 and prints the same text as part of its traceback, which
would otherwise satisfy the message assertions on its own.
"""
assert result.returncode == 1
assert result.stdout == ""
assert "Traceback" not in result.stderr
assert result.stderr.strip().startswith("error:")
@pytest.fixture
def sample(tmp_path):
"""The configuration and text files from the assignment."""
config_file = tmp_path / "config.txt"
config_file.write_text("a=z\nbb=y\nc=x\n", encoding="utf-8")
text_file = tmp_path / "input.txt"
text_file.write_text(
"jgrebbk6hnae\n"
"cnhjrfyjvth3nxr\n"
"b#sjcf_ansbbbbbvo!\n"
"dajf#aemfaocfna%\n",
encoding="utf-8",
)
return config_file, text_file
def test_assignment_example_end_to_end(sample):
"""The full worked example, sorted most-changed first."""
config_file, text_file = sample
result = run_cli(str(config_file), str(text_file))
assert result.returncode == 0
assert result.stderr == ""
assert result.stdout.splitlines() == [
"b#sjxf_znsyybvo!", # 6 symbols replaced
"dzjf#zemfzoxfnz%", # 5
"jgreyk6hnze", # 3
"xnhjrfyjvth3nxr", # 1
]
def test_equal_counts_keep_original_file_order(tmp_path):
"""The sort is stable and keys only on the count, so lines with the
same number of replacements must stay in their original order."""
config_file = tmp_path / "config.txt"
config_file.write_text("a=z\n", encoding="utf-8")
text_file = tmp_path / "input.txt"
text_file.write_text("first a\nsecond a\nthird a\n", encoding="utf-8")
result = run_cli(str(config_file), str(text_file))
assert result.returncode == 0
assert result.stdout.splitlines() == ["first z", "second z", "third z"]
def test_missing_config_file(tmp_path, sample):
_, text_file = sample
missing = tmp_path / "does_not_exist.txt"
result = run_cli(str(missing), str(text_file))
assert_clean_error(result)
assert "No such file or directory" in result.stderr
assert str(missing) in result.stderr
def test_missing_text_file(tmp_path, sample):
config_file, _ = sample
missing = tmp_path / "does_not_exist.txt"
result = run_cli(str(config_file), str(missing))
assert_clean_error(result)
assert "No such file or directory" in result.stderr
def test_malformed_config_reports_line_number(tmp_path, sample):
_, text_file = sample
config_file = tmp_path / "bad.txt"
config_file.write_text("a=z\nnoequalsign\n", encoding="utf-8")
result = run_cli(str(config_file), str(text_file))
assert_clean_error(result)
assert "line 2" in result.stderr
def test_non_utf8_text_file(tmp_path, sample):
"""A decoding failure must be reported, not raised as a traceback."""
config_file, _ = sample
text_file = tmp_path / "latin1.txt"
text_file.write_bytes(b"caf\xe9 abc\n")
result = run_cli(str(config_file), str(text_file))
assert_clean_error(result)
assert "codec can't decode" in result.stderr
def test_missing_arguments_is_a_usage_error():
"""argparse reserves exit status 2 for command line misuse."""
result = run_cli()
assert result.returncode == 2
assert "usage:" in result.stderr