|
| 1 | +import textwrap |
| 2 | + |
| 3 | +import pytest |
| 4 | +from _pytask.nodes import create_task_name |
| 5 | +from _pytask.outcomes import ExitCode |
| 6 | +from pytask import main |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize("func_name", ["task_example", "func"]) |
| 10 | +@pytest.mark.parametrize("task_name", ["the_only_task", None]) |
| 11 | +def test_task_with_task_decorator(tmp_path, func_name, task_name): |
| 12 | + task_decorator_input = f"{task_name!r}" if task_name else task_name |
| 13 | + source = f""" |
| 14 | + import pytask |
| 15 | +
|
| 16 | + @pytask.mark.task({task_decorator_input}) |
| 17 | + @pytask.mark.produces("out.txt") |
| 18 | + def {func_name}(produces): |
| 19 | + produces.write_text("Hello. It's me.") |
| 20 | + """ |
| 21 | + tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source)) |
| 22 | + |
| 23 | + session = main({"paths": tmp_path}) |
| 24 | + |
| 25 | + assert session.exit_code == ExitCode.OK |
| 26 | + |
| 27 | + if task_name: |
| 28 | + assert session.tasks[0].name == create_task_name( |
| 29 | + tmp_path.joinpath("task_module.py"), task_name |
| 30 | + ) |
| 31 | + else: |
| 32 | + assert session.tasks[0].name == create_task_name( |
| 33 | + tmp_path.joinpath("task_module.py"), func_name |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize("func_name", ["task_example", "func"]) |
| 38 | +@pytest.mark.parametrize("task_name", ["the_only_task", None]) |
| 39 | +def test_task_with_task_decorator_with_parametrize(tmp_path, func_name, task_name): |
| 40 | + task_decorator_input = f"{task_name!r}" if task_name else task_name |
| 41 | + source = f""" |
| 42 | + import pytask |
| 43 | +
|
| 44 | + @pytask.mark.task({task_decorator_input}) |
| 45 | + @pytask.mark.parametrize("produces", ["out_1.txt", "out_2.txt"]) |
| 46 | + def {func_name}(produces): |
| 47 | + produces.write_text("Hello. It's me.") |
| 48 | + """ |
| 49 | + tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source)) |
| 50 | + |
| 51 | + session = main({"paths": tmp_path}) |
| 52 | + |
| 53 | + assert session.exit_code == ExitCode.OK |
| 54 | + |
| 55 | + if task_name: |
| 56 | + assert session.tasks[0].name == create_task_name( |
| 57 | + tmp_path.joinpath("task_module.py"), f"{task_name}[out_1.txt]" |
| 58 | + ) |
| 59 | + assert session.tasks[1].name == create_task_name( |
| 60 | + tmp_path.joinpath("task_module.py"), f"{task_name}[out_2.txt]" |
| 61 | + ) |
| 62 | + else: |
| 63 | + assert session.tasks[0].name == create_task_name( |
| 64 | + tmp_path.joinpath("task_module.py"), f"{func_name}[out_1.txt]" |
| 65 | + ) |
| 66 | + assert session.tasks[1].name == create_task_name( |
| 67 | + tmp_path.joinpath("task_module.py"), f"{func_name}[out_2.txt]" |
| 68 | + ) |
0 commit comments