Skip to content

Commit 2686e16

Browse files
committed
Add test.
1 parent fbbcaa8 commit 2686e16

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

src/pytask_stata/collect.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import subprocess
55
import warnings
6+
from pathlib import Path
67
from typing import Any
78

89
from pytask import has_mark
@@ -21,8 +22,6 @@
2122
from pytask import TaskWithoutPath
2223
from pytask_stata.shared import convert_task_id_to_name_of_log_file
2324
from pytask_stata.shared import stata
24-
from pathlib import Path
25-
2625

2726

2827
def run_stata_script(

src/pytask_stata/execute.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""Execute tasks."""
22
from __future__ import annotations
3+
from pathlib import Path
34

45
import re
56

67
from pytask import has_mark
78
from pytask import hookimpl
89
from pytask import PTask
9-
from pytask import Session
10+
from pytask import Session, PTaskWithPath
1011
from pytask_stata.shared import convert_task_id_to_name_of_log_file
1112
from pytask_stata.shared import STATA_COMMANDS
1213

@@ -37,7 +38,10 @@ def pytask_execute_task_teardown(session: Session, task: PTask) -> None:
3738
if has_mark(task, "stata"):
3839
if session.config["platform"] == "win32":
3940
log_name = convert_task_id_to_name_of_log_file(task)
40-
path_to_log = task.path.with_name(log_name).with_suffix(".log")
41+
if isinstance(task, PTaskWithPath):
42+
path_to_log = task.path.with_name(log_name).with_suffix(".log")
43+
else:
44+
path_to_log = Path.cwd(log_name).with_name(log_name).with_suffix(".log")
4145
else:
4246
node = task.depends_on["_script"]
4347
path_to_log = node.path.with_suffix(".log")

src/pytask_stata/shared.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from typing import TYPE_CHECKING
99

1010

11-
1211
if TYPE_CHECKING:
1312
from pytask import PTask
1413
from pathlib import Path
@@ -83,7 +82,12 @@ def convert_task_id_to_name_of_log_file(task: PTask) -> str:
8382
"""
8483
id_ = getattr(task, "short_name", task.name)
8584
id_without_parent_directories = id_.rsplit("/")[-1]
86-
converted_id = id_without_parent_directories.replace(".", "_").replace("::", "_")
85+
converted_id = (
86+
id_without_parent_directories.replace(".", "_")
87+
.replace("::", "_")
88+
.replace("<", "")
89+
.replace(">", "")
90+
)
8791
return converted_id
8892

8993

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from __future__ import annotations
22

33
import shutil
4+
import sys
5+
from contextlib import contextmanager
46
from typing import Callable
57

68
import pytest
79
from click.testing import CliRunner
810
from pytask_stata.config import STATA_COMMANDS
9-
import sys
10-
from contextlib import contextmanager
1111

1212

1313
needs_stata = pytest.mark.skipif(

tests/test_execute.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from pathlib import Path
77

88
import pytest
9+
from pytask import build
910
from pytask import cli
1011
from pytask import ExitCode
11-
from pytask import build
1212
from pytask import Mark
1313
from pytask import Session
1414
from pytask import Task
@@ -196,3 +196,33 @@ def task_run_do_file():
196196

197197
assert result.exit_code == ExitCode.COLLECTION_FAILED
198198
assert "has multiple @pytask.mark.stata marks" in result.output
199+
200+
201+
@needs_stata
202+
@pytest.mark.end_to_end()
203+
def test_with_task_without_path(runner, tmp_path):
204+
task_source = """
205+
import pytask
206+
from pytask import task
207+
208+
task_example = pytask.mark.stata(script="script.do")(
209+
pytask.mark.produces("auto.dta")(task()(lambda x: None))
210+
)
211+
"""
212+
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(task_source))
213+
214+
do_file = """
215+
sysuse auto, clear
216+
save auto
217+
"""
218+
tmp_path.joinpath("script.do").write_text(textwrap.dedent(do_file))
219+
220+
result = runner.invoke(cli, [tmp_path.as_posix(), "--stata-keep-log"])
221+
222+
assert result.exit_code == ExitCode.OK
223+
assert tmp_path.joinpath("auto.dta").exists()
224+
225+
if sys.platform == "win32":
226+
assert tmp_path.joinpath("lambda.log").exists()
227+
else:
228+
assert tmp_path.joinpath("lambda.log").exists()

0 commit comments

Comments
 (0)