Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45c721a

Browse files
authored
Merge pull request #1878 from lorenzwalthert/local-r-hooks
R local hooks should not prefix hook path
2 parents c082292 + 788aec1 commit 45c721a

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

pre_commit/languages/r.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ def _get_env_dir(prefix: Prefix, version: str) -> str:
4040
return prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
4141

4242

43-
def _prefix_if_file_entry(
43+
def _prefix_if_non_local_file_entry(
4444
entry: Sequence[str],
4545
prefix: Prefix,
46+
src: str,
4647
) -> Sequence[str]:
4748
if entry[1] == '-e':
4849
return entry[1:]
4950
else:
50-
return (prefix.path(entry[1]),)
51+
if src == 'local':
52+
path = entry[1]
53+
else:
54+
path = prefix.path(entry[1])
55+
return (path,)
5156

5257

5358
def _entry_validate(entry: Sequence[str]) -> None:
@@ -75,7 +80,7 @@ def _cmd_from_hook(hook: Hook) -> Tuple[str, ...]:
7580

7681
return (
7782
*entry[:1], *RSCRIPT_OPTS,
78-
*_prefix_if_file_entry(entry, hook.prefix),
83+
*_prefix_if_non_local_file_entry(entry, hook.prefix, hook.src),
7984
*hook.args,
8085
)
8186

tests/languages/r_test.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ def _test_r_parsing(
1414
hook_id,
1515
expected_hook_expr={},
1616
expected_args={},
17+
config={},
18+
expect_path_prefix=True,
1719
):
1820
repo_path = 'r_hooks_repo'
1921
path = make_repo(tempdir_factory, repo_path)
20-
config = make_config_from_repo(path)
22+
config = config or make_config_from_repo(path)
2123
hook = _get_hook_no_install(config, store, hook_id)
2224
ret = r._cmd_from_hook(hook)
2325
expected_cmd = 'Rscript'
2426
expected_opts = (
2527
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
2628
)
2729
expected_path = os.path.join(
28-
hook.prefix.prefix_dir, '.'.join([hook_id, 'R']),
30+
hook.prefix.prefix_dir if expect_path_prefix else '',
31+
f'{hook_id}.R',
2932
)
3033
expected = (
3134
expected_cmd,
@@ -102,3 +105,25 @@ def test_r_parsing_expr_non_Rscirpt(tempdir_factory, store):
102105

103106
msg = execinfo.value.args
104107
assert msg == ('entry must start with `Rscript`.',)
108+
109+
110+
def test_r_parsing_file_local(tempdir_factory, store):
111+
path = 'path/to/script.R'
112+
hook_id = 'local-r'
113+
config = {
114+
'repo': 'local',
115+
'hooks': [{
116+
'id': hook_id,
117+
'name': 'local-r',
118+
'entry': f'Rscript {path}',
119+
'language': 'r',
120+
}],
121+
}
122+
_test_r_parsing(
123+
tempdir_factory,
124+
store,
125+
hook_id=hook_id,
126+
expected_hook_expr=(path,),
127+
config=config,
128+
expect_path_prefix=False,
129+
)

0 commit comments

Comments
 (0)