-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_execute.py
230 lines (180 loc) · 6.47 KB
/
test_execute.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from __future__ import annotations
import sys
import textwrap
from contextlib import ExitStack as does_not_raise # noqa: N813
from pathlib import Path
import pytest
from pytask import ExitCode
from pytask import Mark
from pytask import Session
from pytask import Task
from pytask import build
from pytask import cli
from pytask_stata.config import STATA_COMMANDS
from pytask_stata.execute import pytask_execute_task_setup
from tests.conftest import needs_stata
@pytest.mark.unit
@pytest.mark.parametrize(
("stata", "expectation"),
[(executable, does_not_raise()) for executable in STATA_COMMANDS]
+ [(None, pytest.raises(RuntimeError, match="Stata is needed"))],
)
@pytest.mark.parametrize("platform", ["win32", "linux", "darwin"])
def test_pytask_execute_task_setup_raise_error(stata, platform, expectation):
"""Make sure that the task setup raises errors."""
# Act like r is installed since we do not test this.
task = Task(
base_name="task_example",
path=Path(),
function=None,
markers=[Mark("stata", (), {})],
)
session = Session(config={"stata": stata, "platform": platform})
with expectation:
pytask_execute_task_setup(session, task)
@needs_stata
@pytest.mark.end_to_end
def test_run_do_file(runner, tmp_path):
task_source = """
import pytask
@pytask.mark.stata(script="script.do")
@pytask.mark.produces("auto.dta")
def task_run_do_file():
pass
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(task_source))
do_file = """
sysuse auto, clear
save auto
"""
tmp_path.joinpath("script.do").write_text(textwrap.dedent(do_file))
result = runner.invoke(cli, [tmp_path.as_posix(), "--stata-keep-log"])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("auto.dta").exists()
if sys.platform == "win32":
assert tmp_path.joinpath("task_example_py_task_run_do_file.log").exists()
else:
assert tmp_path.joinpath("script.log").exists()
@needs_stata
@pytest.mark.end_to_end
def test_run_do_file_w_task_decorator(runner, tmp_path):
task_source = """
import pytask
@pytask.mark.task
@pytask.mark.stata(script="script.do")
@pytask.mark.produces("auto.dta")
def run_do_file():
pass
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(task_source))
do_file = """
sysuse auto, clear
save auto
"""
tmp_path.joinpath("script.do").write_text(textwrap.dedent(do_file))
result = runner.invoke(cli, [tmp_path.as_posix(), "--stata-keep-log"])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("auto.dta").exists()
if sys.platform == "win32":
assert tmp_path.joinpath("task_example_py_run_do_file.log").exists()
else:
assert tmp_path.joinpath("script.log").exists()
@pytest.mark.end_to_end
def test_raise_error_if_stata_is_not_found(tmp_path, monkeypatch):
task_source = """
from pytask import mark, task
@task(kwargs={"produces": "out.dta"})
@mark.stata(script="script.do")
def task_run_do_file():
pass
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(task_source))
tmp_path.joinpath("script.do").write_text(textwrap.dedent("1 + 1"))
# Hide Stata if available.
monkeypatch.setattr(
"pytask_stata.config.shutil.which",
lambda x: None, # noqa: ARG005
)
session = build(paths=tmp_path)
assert session.exit_code == ExitCode.FAILED
assert isinstance(session.execution_reports[0].exc_info[1], RuntimeError)
@needs_stata
@pytest.mark.end_to_end
def test_run_do_file_w_wrong_cmd_option(runner, tmp_path):
"""Apparently, Stata simply discards wrong cmd options."""
task_source = """
import pytask
@pytask.mark.stata(script="script.do", options="--wrong-flag")
@pytask.mark.produces("out.dta")
def task_run_do_file():
pass
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(task_source))
do_file = """
sysuse auto, clear
save out
"""
tmp_path.joinpath("script.do").write_text(textwrap.dedent(do_file))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("out.dta").exists()
@needs_stata
@pytest.mark.end_to_end
def test_run_do_file_by_passing_path(runner, tmp_path):
"""Replicates example under "Command Line Arguments" in Readme."""
task_source = """
import pytask
from pathlib import Path
@pytask.mark.stata(script="script.do", options=Path(__file__).parent / "auto.dta")
@pytask.mark.produces("auto.dta")
def task_run_do_file():
pass
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(task_source))
do_file = """
args produces
sysuse auto, clear
save "`produces'"
"""
tmp_path.joinpath("script.do").write_text(textwrap.dedent(do_file))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
@needs_stata
@pytest.mark.end_to_end
def test_run_do_file_fails_with_multiple_marks(runner, tmp_path):
task_source = """
import pytask
@pytask.mark.stata(script="script.do")
@pytask.mark.stata(script="script.do")
@pytask.mark.produces("auto.dta")
def task_run_do_file():
pass
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(task_source))
tmp_path.joinpath("script.do").touch()
result = runner.invoke(cli, [tmp_path.as_posix(), "--stata-keep-log"])
assert result.exit_code == ExitCode.COLLECTION_FAILED
assert "has multiple @pytask.mark.stata marks" in result.output
@needs_stata
@pytest.mark.end_to_end
def test_with_task_without_path(runner, tmp_path):
task_source = """
import pytask
from pytask import task
task_example = pytask.mark.stata(script="script.do")(
pytask.mark.produces("auto.dta")(task()(lambda x: None))
)
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(task_source))
do_file = """
sysuse auto, clear
save auto
"""
tmp_path.joinpath("script.do").write_text(textwrap.dedent(do_file))
result = runner.invoke(cli, [tmp_path.as_posix(), "--stata-keep-log"])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("auto.dta").exists()
if sys.platform == "win32":
assert tmp_path.joinpath("task_example_py_lambda.log").exists()
else:
assert not tmp_path.joinpath("task_example_py_lambda.log").exists()