Skip to content

Commit 305185c

Browse files
committed
Allow .R.
1 parent f5a9ffd commit 305185c

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

src/pytask_r/collect.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ def pytask_collect_task(
9393
),
9494
)
9595

96-
if not (isinstance(script_node, PathNode) and script_node.path.suffix == ".r"):
96+
if not (
97+
isinstance(script_node, PathNode)
98+
and script_node.path.suffix in (".r", ".R")
99+
):
97100
msg = (
98101
"The 'script' keyword of the @pytask.mark.r decorator must point "
99-
f"to an R file with the .r extension, but it is {script_node}."
102+
f"to an R file with the .r or .R extension, but it is {script_node}."
100103
)
101104
raise ValueError(msg)
102105

tests/test_execute.py

+39-20
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def test_run_r_script( # noqa: PLR0913
4545
@pytask.mark.r(script="script.r", serializer="{serializer}", suffix="{suffix}")
4646
@pytask.mark.depends_on({depends_on})
4747
@pytask.mark.produces("out.txt")
48-
def task_run_r_script():
49-
pass
50-
48+
def task_run_r_script(): ...
5149
"""
5250
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(task_source))
5351
tmp_path.joinpath("in_1.txt").touch()
@@ -77,14 +75,12 @@ def test_run_r_script_w_task_decorator(
7775
runner, tmp_path, parse_config_code, serializer, suffix
7876
):
7977
task_source = f"""
80-
import pytask
81-
82-
@pytask.mark.task
83-
@pytask.mark.r(script="script.r", serializer="{serializer}", suffix="{suffix}")
84-
@pytask.mark.produces("out.txt")
85-
def run_r_script():
86-
pass
78+
from pytask import task, mark
8779
80+
@task
81+
@mark.r(script="script.r", serializer="{serializer}", suffix="{suffix}")
82+
@mark.produces("out.txt")
83+
def run_r_script(): ...
8884
"""
8985
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(task_source))
9086

@@ -113,8 +109,7 @@ def test_raise_error_if_rscript_is_not_found(
113109
114110
@pytask.mark.r(script="script.r", serializer="{serializer}", suffix="{suffix}")
115111
@pytask.mark.produces("out.txt")
116-
def task_run_r_script():
117-
pass
112+
def task_run_r_script(): ...
118113
"""
119114
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(task_source))
120115

@@ -152,8 +147,7 @@ def test_run_r_script_w_saving_workspace(
152147
suffix="{suffix}"
153148
)
154149
@pytask.mark.produces("out.txt")
155-
def task_run_r_script():
156-
pass
150+
def task_run_r_script(): ...
157151
"""
158152
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(task_source))
159153

@@ -188,8 +182,7 @@ def test_run_r_script_w_wrong_cmd_option(
188182
suffix="{suffix}"
189183
)
190184
@pytask.mark.produces("out.txt")
191-
def task_run_r_script():
192-
pass
185+
def task_run_r_script(): ...
193186
"""
194187
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(task_source))
195188

@@ -216,8 +209,7 @@ def test_run_r_script_w_custom_serializer(runner, tmp_path):
216209
217210
@pytask.mark.r(script="script.r", serializer=json.dumps, suffix=".json")
218211
@pytask.mark.produces("out.txt")
219-
def task_run_r_script():
220-
pass
212+
def task_run_r_script(): ...
221213
222214
"""
223215
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(task_source))
@@ -247,8 +239,7 @@ def test_run_r_script_fails_w_multiple_markers(runner, tmp_path):
247239
@pytask.mark.r(script="script.r")
248240
@pytask.mark.r(script="script.r")
249241
@pytask.mark.produces("out.txt")
250-
def task_run_r_script():
251-
pass
242+
def task_run_r_script(): ...
252243
"""
253244
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(task_source))
254245
tmp_path.joinpath("script.r").touch()
@@ -257,3 +248,31 @@ def task_run_r_script():
257248

258249
assert result.exit_code == ExitCode.COLLECTION_FAILED
259250
assert "has multiple @pytask.mark.r marks" in result.output
251+
252+
253+
@needs_rscript
254+
@pytest.mark.end_to_end()
255+
def test_run_r_script_with_capital_extension(runner, tmp_path):
256+
task_source = """
257+
import pytask
258+
259+
@pytask.mark.r(script="script.R")
260+
@pytask.mark.produces("out.txt")
261+
def task_run_r_script(): ...
262+
"""
263+
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(task_source))
264+
265+
r_script = """
266+
library(jsonlite)
267+
args <- commandArgs(trailingOnly=TRUE)
268+
config <- read_json(args[length(args)])
269+
file_descriptor <- file(config$produces)
270+
writeLines(c("So, so you think you can tell heaven from hell?"), file_descriptor)
271+
close(file_descriptor)
272+
"""
273+
tmp_path.joinpath("script.R").write_text(textwrap.dedent(r_script))
274+
275+
result = runner.invoke(cli, [tmp_path.as_posix()])
276+
277+
assert result.exit_code == ExitCode.OK
278+
assert tmp_path.joinpath("out.txt").exists()

0 commit comments

Comments
 (0)