diff --git a/puzzle_generator/create_puzzle.py b/puzzle_generator/create_puzzle.py index 0b37474..e10a9f6 100644 --- a/puzzle_generator/create_puzzle.py +++ b/puzzle_generator/create_puzzle.py @@ -1,5 +1,6 @@ import pathlib import inspect +import sys from .puzzle_data_encryption import decrypt_data, encrypt_data from . import simple_encryption_utils as seu @@ -16,6 +17,7 @@ def _run_puzzle(in_puzzle): ) if new_puzzle is None: print("This is a wrong answer. Try again!") + sys.exit(1) else: _run_puzzle(new_puzzle) @@ -32,7 +34,7 @@ def _create_str(in_modules, in_functions, in_encrypted_puzzle): def create(in_puzzle, output_path: pathlib.Path) -> None: encrypted_puzzle = encrypt_data(in_puzzle, seu.encrypt_str) - needed_modules = ["hashlib", "itertools", "base64", "json"] + needed_modules = ["hashlib", "itertools", "base64", "json", "sys"] needed_functions = [ seu.hash_bytes, diff --git a/pyproject.toml b/pyproject.toml index 13cdaad..e0db6ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "puzzle-generator" -version = "0.1.2" +version = "0.1.3" description = "Generates python code representing a puzzle" authors = ["piotr.idzik "] readme = "./puzzle_generator/README.md" diff --git a/tests/test_create_puzzle.py b/tests/test_create_puzzle.py new file mode 100644 index 0000000..3395b2c --- /dev/null +++ b/tests/test_create_puzzle.py @@ -0,0 +1,66 @@ +import pathlib +import subprocess +import pytest + +import puzzle_generator.create_puzzle as cp + + +@pytest.fixture(name="puzzle") +def fixture_puzzle(): + return { + "str": "Question 1?", + "pass": "Answer 1", + "rest": { + "str": "Question 2?", + "pass": "Is this the final answer?", + "rest": {"str": "Congratulations!"}, + }, + } + + +@pytest.fixture(name="puzzle_path") +def fixture_puzzle_path(tmp_path) -> pathlib.Path: + return tmp_path / "puzzle.py" + + +def _run_puzzle_file( + in_puzzle_path: pathlib.Path, answers: list[str] +) -> subprocess.CompletedProcess[str]: + assert in_puzzle_path.is_file() + puzzle_dir = in_puzzle_path.parent + puzzle_name = in_puzzle_path.name + return subprocess.run( + ["python3", puzzle_name], + cwd=puzzle_dir, + input="\n".join(answers), + text=True, + capture_output=True, + check=False, + ) + + +def test_all_good_answers(puzzle, puzzle_path: pathlib.Path) -> None: + cp.create(puzzle, puzzle_path) + res = _run_puzzle_file(puzzle_path, ["Answer 1", "Is this the final answer?"]) + + assert res.returncode == 0 + assert res.stdout == "Question 1?\nQuestion 2?\nCongratulations!\n" + assert not res.stderr + + +def test_second_answer_wrong(puzzle, puzzle_path: pathlib.Path) -> None: + cp.create(puzzle, puzzle_path) + res = _run_puzzle_file(puzzle_path, ["Answer 1", "This is a wrong answer"]) + assert res.returncode == 1 + assert ( + res.stdout == "Question 1?\nQuestion 2?\nThis is a wrong answer. Try again!\n" + ) + assert not res.stderr + + +def test_first_answer_wrong(puzzle, puzzle_path: pathlib.Path) -> None: + cp.create(puzzle, puzzle_path) + res = _run_puzzle_file(puzzle_path, ["This is a wrong answer."]) + assert res.returncode == 1 + assert res.stdout == "Question 1?\nThis is a wrong answer. Try again!\n" + assert not res.stderr