Skip to content

Commit 8e57e80

Browse files
committed
avoid using hook.src in R language
this wasn't meant to be read -- hook.prefix works fine for local too
1 parent 017fa5c commit 8e57e80

File tree

2 files changed

+22
-44
lines changed

2 files changed

+22
-44
lines changed

pre_commit/languages/r.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,11 @@ def _get_env_dir(prefix: Prefix, version: str) -> str:
4444
return prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
4545

4646

47-
def _prefix_if_non_local_file_entry(
48-
entry: Sequence[str],
49-
prefix: Prefix,
50-
src: str,
51-
) -> Sequence[str]:
47+
def _prefix_if_file_entry(entry: list[str], prefix: Prefix) -> Sequence[str]:
5248
if entry[1] == '-e':
5349
return entry[1:]
5450
else:
55-
if src == 'local':
56-
path = entry[1]
57-
else:
58-
path = prefix.path(entry[1])
59-
return (path,)
51+
return (prefix.path(entry[1]),)
6052

6153

6254
def _rscript_exec() -> str:
@@ -67,7 +59,7 @@ def _rscript_exec() -> str:
6759
return os.path.join(r_home, 'bin', win_exe('Rscript'))
6860

6961

70-
def _entry_validate(entry: Sequence[str]) -> None:
62+
def _entry_validate(entry: list[str]) -> None:
7163
"""
7264
Allowed entries:
7365
# Rscript -e expr
@@ -91,8 +83,8 @@ def _cmd_from_hook(hook: Hook) -> tuple[str, ...]:
9183
_entry_validate(entry)
9284

9385
return (
94-
*entry[:1], *RSCRIPT_OPTS,
95-
*_prefix_if_non_local_file_entry(entry, hook.prefix, hook.src),
86+
entry[0], *RSCRIPT_OPTS,
87+
*_prefix_if_file_entry(entry, hook.prefix),
9688
*hook.args,
9789
)
9890

tests/languages/r_test.py

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,18 @@ def _test_r_parsing(
1616
tempdir_factory,
1717
store,
1818
hook_id,
19-
expected_hook_expr={},
20-
expected_args={},
21-
config={},
22-
expect_path_prefix=True,
19+
expected_hook_expr=(),
20+
expected_args=(),
21+
config=None,
2322
):
24-
repo_path = 'r_hooks_repo'
25-
path = make_repo(tempdir_factory, repo_path)
26-
config = config or make_config_from_repo(path)
23+
repo = make_repo(tempdir_factory, 'r_hooks_repo')
24+
config = make_config_from_repo(repo)
2725
hook = _get_hook_no_install(config, store, hook_id)
2826
ret = r._cmd_from_hook(hook)
29-
expected_cmd = 'Rscript'
30-
expected_opts = (
31-
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
32-
)
33-
expected_path = os.path.join(
34-
hook.prefix.prefix_dir if expect_path_prefix else '',
35-
f'{hook_id}.R',
36-
)
27+
expected_path = os.path.join(hook.prefix.prefix_dir, f'{hook_id}.R')
3728
expected = (
38-
expected_cmd,
39-
*expected_opts,
29+
'Rscript',
30+
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
4031
*(expected_hook_expr or (expected_path,)),
4132
*expected_args,
4233
)
@@ -84,9 +75,7 @@ def test_r_parsing_expr_no_opts_no_args2(tempdir_factory, store):
8475
def test_r_parsing_expr_opts_no_args2(tempdir_factory, store):
8576
with pytest.raises(ValueError) as execinfo:
8677
r._entry_validate(
87-
[
88-
'Rscript', '--vanilla', '-e', '1+1', '-e', 'letters',
89-
],
78+
['Rscript', '--vanilla', '-e', '1+1', '-e', 'letters'],
9079
)
9180
msg = execinfo.value.args
9281
assert msg == (
@@ -112,24 +101,21 @@ def test_r_parsing_expr_non_Rscirpt(tempdir_factory, store):
112101

113102

114103
def test_r_parsing_file_local(tempdir_factory, store):
115-
path = 'path/to/script.R'
116-
hook_id = 'local-r'
117104
config = {
118105
'repo': 'local',
119106
'hooks': [{
120-
'id': hook_id,
107+
'id': 'local-r',
121108
'name': 'local-r',
122-
'entry': f'Rscript {path}',
109+
'entry': 'Rscript path/to/script.R',
123110
'language': 'r',
124111
}],
125112
}
126-
_test_r_parsing(
127-
tempdir_factory,
128-
store,
129-
hook_id=hook_id,
130-
expected_hook_expr=(path,),
131-
config=config,
132-
expect_path_prefix=False,
113+
hook = _get_hook_no_install(config, store, 'local-r')
114+
ret = r._cmd_from_hook(hook)
115+
assert ret == (
116+
'Rscript',
117+
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
118+
hook.prefix.path('path/to/script.R'),
133119
)
134120

135121

0 commit comments

Comments
 (0)