Skip to content

Commit

Permalink
Merge pull request #46 from vil02/25-puzzle_generatorcreate-should-re…
Browse files Browse the repository at this point in the history
…turn-a-string

feat: return string from `create_puzzle.create`
  • Loading branch information
vil02 committed Jul 27, 2024
2 parents bd52be3 + be0f350 commit f326f22
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
4 changes: 1 addition & 3 deletions examples/basic_usage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import pathlib

from puzzle_generator.create_puzzle import create

_PUZZLE = {
Expand All @@ -12,4 +10,4 @@
},
}

create(_PUZZLE, pathlib.Path("example_puzzle.py"))
print(create(_PUZZLE))
8 changes: 2 additions & 6 deletions puzzle_generator/create_puzzle.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pathlib
import hashlib
import inspect
import sys
Expand Down Expand Up @@ -155,7 +154,7 @@ def _get_configurator(**kwargs):
return configurators[encryption](**kwargs)


def create(in_puzzle, output_path: pathlib.Path, **kwargs) -> None:
def create(in_puzzle, **kwargs) -> str:
configurator = _get_configurator(**kwargs)

encrypted_puzzle = encrypt_data(in_puzzle, configurator.get_encrypt())
Expand All @@ -164,7 +163,4 @@ def create(in_puzzle, output_path: pathlib.Path, **kwargs) -> None:
needed_objects = configurator.get_needed_objects()
constants_str = configurator.get_constants_str()

with open(output_path, "w", encoding="utf-8") as res_file:
res_file.write(
_create_str(needed_modules, needed_objects, encrypted_puzzle, constants_str)
)
return _create_str(needed_modules, needed_objects, encrypted_puzzle, constants_str)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "puzzle-generator"
version = "0.4.2"
version = "0.5.0"
description = "Generates python code representing a puzzle"
authors = ["piotr.idzik <vil02_puzzle_generator@10g.pl>"]
readme = "./puzzle_generator/README.md"
Expand Down
16 changes: 12 additions & 4 deletions tests/test_create_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def _run_puzzle_file(
)


def _run_puzzle_str(
in_puzzle_str: str, answers: list[str], in_puzzle_path: pathlib.Path
) -> subprocess.CompletedProcess[str]:
with open(in_puzzle_path, "w", encoding="utf-8") as puzzle_file:
puzzle_file.write(in_puzzle_str)
return _run_puzzle_file(in_puzzle_path, answers)


_CONFIGURATIONS = [
{},
{"proc_hasher": hashlib.md5},
Expand Down Expand Up @@ -101,8 +109,8 @@ def test_all_good_answers(
puzzle_path: pathlib.Path,
configuration,
) -> None:
cp.create(puzzle_tc.puzzle, puzzle_path, **configuration)
res = _run_puzzle_file(puzzle_path, puzzle_tc.correct.input)
puzzle_str = cp.create(puzzle_tc.puzzle, **configuration)
res = _run_puzzle_str(puzzle_str, puzzle_tc.correct.input, puzzle_path)

assert res.returncode == 0
assert res.stdout == puzzle_tc.correct.output
Expand All @@ -116,8 +124,8 @@ def test_wrong_answers(
configuration,
) -> None:
for cur_wrong in puzzle_tc.wrong:
cp.create(puzzle_tc.puzzle, puzzle_path, **configuration)
res = _run_puzzle_file(puzzle_path, cur_wrong.input)
puzzle_str = cp.create(puzzle_tc.puzzle, **configuration)
res = _run_puzzle_str(puzzle_str, cur_wrong.input, puzzle_path)
assert res.returncode == 1
assert res.stdout == cur_wrong.output
assert not res.stderr
Expand Down

0 comments on commit f326f22

Please sign in to comment.