From 40ea68be11fdda03e5055c4180fa3c9d6f1424e1 Mon Sep 17 00:00:00 2001 From: Martin Stajnko Date: Tue, 25 Aug 2026 21:21:38 +0200 Subject: [PATCH] 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. --- .gitignore | 10 +++ README.md | 116 ++++++++++++++++++++++++++ config.txt | 3 + config_loader.py | 23 +++++ conftest.py | 0 input.txt | 4 + main.py | 53 ++++++++++++ pyproject.toml | 13 +++ replacer.py | 46 ++++++++++ requirements-dev.txt | 1 + tests/__init__.py | 0 tests/integration/__init__.py | 0 tests/integration/test_cli.py | 139 +++++++++++++++++++++++++++++++ tests/unit/__init__.py | 0 tests/unit/test_config_loader.py | 58 +++++++++++++ tests/unit/test_replacer.py | 61 ++++++++++++++ tests/unit/test_sample_data.py | 29 +++++++ 17 files changed, 556 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config.txt create mode 100644 config_loader.py create mode 100644 conftest.py create mode 100644 input.txt create mode 100644 main.py create mode 100644 pyproject.toml create mode 100644 replacer.py create mode 100644 requirements-dev.txt create mode 100644 tests/__init__.py create mode 100644 tests/integration/__init__.py create mode 100644 tests/integration/test_cli.py create mode 100644 tests/unit/__init__.py create mode 100644 tests/unit/test_config_loader.py create mode 100644 tests/unit/test_replacer.py create mode 100644 tests/unit/test_sample_data.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7c4869e --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +__pycache__/ +*.py[cod] +.pytest_cache/ +.coverage +htmlcov/ +.venv/ +venv/ +*.egg-info/ +.idea/ +.vscode/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..45689ef --- /dev/null +++ b/README.md @@ -0,0 +1,116 @@ +# Replace Configured Values in a Text File + +Reads `value1=value2` pairs from a configuration file, replaces every +non-overlapping occurrence of `value1` with `value2` in a text file, and prints +the resulting lines sorted by how many symbols were replaced, most-changed +first. + +## Requirements + +Python 3.9 or newer. No runtime dependencies. + +## Usage + +```bash +python main.py +``` + +### Example + +With the provided `config.txt`: + +``` +a=z +bb=y +c=x +``` + +and `input.txt`: + +``` +jgrebbk6hnae +cnhjrfyjvth3nxr +b#sjcf_ansbbbbbvo! +dajf#aemfaocfna% +``` + +running: + +```bash +python main.py config.txt input.txt +``` + +produces: + +``` +b#sjxf_znsyybvo! 6 symbols replaced +dzjf#zemfzoxfnz% 5 +jgreyk6hnze 3 +xnhjrfyjvth3nxr 1 +``` + +(annotations added here for clarity; the program prints the lines only) + +## Design decisions + +The assignment leaves a few points open. The choices made here are: + +**"Total number of symbols replaced" counts source symbols.** A line where +`bb` and `a` were both replaced counts 3, being the combined length of the +matched `value1` strings, not the number of replacements and not the length of +the substituted text. This makes the count a measure of how much of the +original line changed. + +**Overlapping patterns resolve to the longest match.** If the configuration +contains both `b` and `bb`, the input `bbbb` is read as two `bb` matches rather +than four `b` matches. The assignment guarantees unique values but not that +none is a prefix of another, so `prepare_pairs` orders the pairs by descending +length once, and `replace_line` takes the first match at each position. Without +a rule like this the output would depend on configuration file ordering. + +**Ties keep their original file order.** The final sort keys only on the +replacement count and Python's sort is stable, so lines replaced an equal +number of times appear in the order they had in the input file. + +**Unchanged lines are still printed.** The assignment says to sort the changed +lines but also to output the resulting text. Dropping lines that happened to +match nothing would silently lose content, so every line is printed and lines +with zero replacements simply sort last. + +**Whitespace inside configuration values is significant.** The format has no +quoting or escaping, so a trailing space in `a=z ` is kept as part of the +replacement value; stripping it would make such a value impossible to express. + +**Only the line terminator is stripped.** Both `\n` and `\r\n` are handled, and +every other character of the line is preserved and eligible for replacement. + +## Error handling + +The configuration file is validated while loading: a line without `=` is +rejected, reporting the offending line number. Missing files, unreadable +files, and input that is not valid UTF-8 are reported as messages on stderr +rather than tracebacks, and exit with status 1. Status 2 is reserved by +`argparse` for command line misuse. + +## Tests + +```bash +pip install -r requirements-dev.txt +pytest +``` + +- `tests/unit/` covers configuration parsing and the replacement algorithm, + including the overlapping-pattern and empty-input edge cases, plus a line by + line check of the worked example in `test_sample_data.py`. +- `tests/integration/test_cli.py` invokes `main.py` as a subprocess to cover + what unit tests cannot: argument handling, exit status, stderr messages, and + the sort order as actually printed. + +## Layout + +``` +main.py command line entry point, sorting and output +config_loader.py configuration file parsing and validation +replacer.py replacement algorithm +tests/ unit and integration tests +``` diff --git a/config.txt b/config.txt new file mode 100644 index 0000000..e756a0a --- /dev/null +++ b/config.txt @@ -0,0 +1,3 @@ +a=z +bb=y +c=x \ No newline at end of file diff --git a/config_loader.py b/config_loader.py new file mode 100644 index 0000000..13361e9 --- /dev/null +++ b/config_loader.py @@ -0,0 +1,23 @@ +"""Configuration file parsing.""" + + +def load_config(filename: str) -> list[tuple[str, str]]: + """Load value1=value2 pairs from the configuration file.""" + pairs = [] + + with open(filename, "r", encoding="utf-8") as file: + for line_number, line in enumerate(file, start=1): + line = line.rstrip("\n\r") + + if not line: + continue + + if "=" not in line: + raise ValueError( + f"Invalid configuration at line {line_number}: {line!r}" + ) + + value1, value2 = line.split("=", 1) + pairs.append((value1, value2)) + + return pairs \ No newline at end of file diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..e69de29 diff --git a/input.txt b/input.txt new file mode 100644 index 0000000..5f20305 --- /dev/null +++ b/input.txt @@ -0,0 +1,4 @@ +jgrebbk6hnae +cnhjrfyjvth3nxr +b#sjcf_ansbbbbbvo! +dajf#aemfaocfna% \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..6165814 --- /dev/null +++ b/main.py @@ -0,0 +1,53 @@ +# Technical Assignment: Replace Configured Values in a Text File +# By: Martin Stajnko + +import argparse +import sys + +from config_loader import load_config +from replacer import prepare_pairs, replace_line + + +def main() -> None: + """Parse CLI arguments, apply configured replacements, and print + the resulting lines sorted by number of replaced symbols + (most-changed first).""" + parser = argparse.ArgumentParser( + description="Replace configured values in a text file." + ) + parser.add_argument("config_file", help="Configuration file") + parser.add_argument("text_file", help="Input text file") + + args = parser.parse_args() + + changed_lines = [] + + try: + # Ordered once here rather than on every line of the text file. + pairs = prepare_pairs(load_config(args.config_file)) + + with open(args.text_file, "r", encoding="utf-8") as file: + for line in file: + # Remove only the newline, preserving all other characters. + line = line.rstrip("\r\n") + + changed_line, replaced_symbols = replace_line(line, pairs) + + changed_lines.append((replaced_symbols, changed_line)) + + except OSError as error: + sys.exit(f"error: {error.filename}: {error.strerror}") + except ValueError as error: + # Also covers UnicodeDecodeError, which is a subclass of ValueError. + sys.exit(f"error: {error}") + + # Most changed first. The sort is stable and keys only on the count, + # so lines with equal counts keep their original file order. + changed_lines.sort(key=lambda item: item[0], reverse=True) + + for _, line in changed_lines: + print(line) + + +if __name__ == "__main__": + main() diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..ea9ebe3 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,13 @@ +[project] +name = "value-replacer" +version = "1.0.0" +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"] diff --git a/replacer.py b/replacer.py new file mode 100644 index 0000000..1623f18 --- /dev/null +++ b/replacer.py @@ -0,0 +1,46 @@ +"""Core text replacement logic.""" + + +def prepare_pairs(pairs: list[tuple[str, str]]) -> list[tuple[str, str]]: + """Order the pairs so the longest value1 comes first. + + `replace_line` relies on this ordering to select the longest match at + each position, which makes the result deterministic when configuration + values overlap (for example "b" and "bb"). Preparing the pairs once + here keeps that cost out of the per-line loop. + """ + return sorted(pairs, key=lambda pair: len(pair[0]), reverse=True) + + +def replace_line(line: str, pairs: list[tuple[str, str]]) -> tuple[str, int]: + """Replace all non-overlapping matches in the line. + + At each position the first matching value1 wins, so `pairs` must be + ordered by `prepare_pairs` for the longest match to be selected. + + Returns the changed line and the number of symbols replaced. + """ + result = [] + replaced_symbols = 0 + position = 0 + + while position < len(line): + match = None + + for value1, value2 in pairs: + if line.startswith(value1, position): + match = (value1, value2) + break + + if match is None: + result.append(line[position]) + position += 1 + continue + + value1, value2 = match + + result.append(value2) + replaced_symbols += len(value1) + position += len(value1) + + return "".join(result), replaced_symbols diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..b197d32 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1 @@ +pytest>=7.0 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py new file mode 100644 index 0000000..48e580b --- /dev/null +++ b/tests/integration/test_cli.py @@ -0,0 +1,139 @@ +""" +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 diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/test_config_loader.py b/tests/unit/test_config_loader.py new file mode 100644 index 0000000..d4b53e4 --- /dev/null +++ b/tests/unit/test_config_loader.py @@ -0,0 +1,58 @@ +"""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)) == [] \ No newline at end of file diff --git a/tests/unit/test_replacer.py b/tests/unit/test_replacer.py new file mode 100644 index 0000000..41522ee --- /dev/null +++ b/tests/unit/test_replacer.py @@ -0,0 +1,61 @@ +"""Unit tests for replacer.py""" + +from replacer import prepare_pairs, replace_line + + +def test_sample_line_from_assignment(): + """First sample line from the assignment: 'jgrebbk6hnae'.""" + pairs = prepare_pairs([("a", "z"), ("bb", "y"), ("c", "x")]) + result, replaced = replace_line("jgrebbk6hnae", pairs) + assert result == "jgreyk6hnze" + assert replaced == 3 # "bb" (2) + "a" (1) + + +def test_no_matches(): + pairs = prepare_pairs([("a", "z"), ("bb", "y")]) + result, replaced = replace_line("xyz123", pairs) + assert result == "xyz123" + assert replaced == 0 + + +def test_empty_line(): + pairs = prepare_pairs([("a", "z")]) + result, replaced = replace_line("", pairs) + assert result == "" + assert replaced == 0 + + +def test_overlapping_prefix_patterns_longest_wins(): + """ + Config has both 'b' and 'bb'. Input 'bbbb' should be read as two + 'bb' matches (longest-match-first), not four 'b' matches. + This is the key edge case the greedy algorithm is designed for. + """ + pairs = prepare_pairs([("b", "1"), ("bb", "2")]) + result, replaced = replace_line("bbbb", pairs) + assert result == "22" + assert replaced == 4 # 2 matches of "bb", 2 symbols each + + +def test_adjacent_matches_non_overlapping(): + """Consecutive matches of the same pattern shouldn't overlap.""" + pairs = prepare_pairs([("bb", "y")]) + # Five b's -> "bb", "bb", and a leftover "b". + result, replaced = replace_line("bbbbb", pairs) + assert result == "yyb" + assert replaced == 4 + + +def test_full_line_replacement(): + pairs = prepare_pairs([("abc", "X")]) + result, replaced = replace_line("abc", pairs) + assert result == "X" + assert replaced == 3 + + +def test_single_character_values(): + # Replacement with an empty string is a valid edge case. + pairs = prepare_pairs([("a", "")]) + result, replaced = replace_line("banana", pairs) + assert result == "bnn" + assert replaced == 3 \ No newline at end of file diff --git a/tests/unit/test_sample_data.py b/tests/unit/test_sample_data.py new file mode 100644 index 0000000..c31ee87 --- /dev/null +++ b/tests/unit/test_sample_data.py @@ -0,0 +1,29 @@ +""" +Verifies the worked example from instructions.md line by line. + +The end-to-end behaviour of that same example, including the sort order +and the printed output, is covered by tests/integration/test_cli.py. +""" + +import pytest +from replacer import prepare_pairs, replace_line + +SAMPLE_PAIRS = prepare_pairs([("a", "z"), ("bb", "y"), ("c", "x")]) + +# Each sample line mapped to its expected result and replacement count. +EXPECTED_RESULTS = { + "jgrebbk6hnae": ("jgreyk6hnze", 3), + "cnhjrfyjvth3nxr": ("xnhjrfyjvth3nxr", 1), + "b#sjcf_ansbbbbbvo!": ("b#sjxf_znsyybvo!", 6), + "dajf#aemfaocfna%": ("dzjf#zemfzoxfnz%", 5), +} + + +@pytest.mark.parametrize("line", list(EXPECTED_RESULTS)) +def test_each_sample_line(line): + expected_result, expected_replaced = EXPECTED_RESULTS[line] + + result, replaced = replace_line(line, SAMPLE_PAIRS) + + assert result == expected_result + assert replaced == expected_replaced