mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-20 01:24:42 +04:00
Merge pull request #1878 from lorenzwalthert/local-r-hooks
R local hooks should not prefix hook path
This commit is contained in:
commit
45c721a2ca
2 changed files with 35 additions and 5 deletions
|
|
@ -40,14 +40,19 @@ def _get_env_dir(prefix: Prefix, version: str) -> str:
|
||||||
return prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
|
return prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
|
||||||
|
|
||||||
|
|
||||||
def _prefix_if_file_entry(
|
def _prefix_if_non_local_file_entry(
|
||||||
entry: Sequence[str],
|
entry: Sequence[str],
|
||||||
prefix: Prefix,
|
prefix: Prefix,
|
||||||
|
src: str,
|
||||||
) -> Sequence[str]:
|
) -> Sequence[str]:
|
||||||
if entry[1] == '-e':
|
if entry[1] == '-e':
|
||||||
return entry[1:]
|
return entry[1:]
|
||||||
else:
|
else:
|
||||||
return (prefix.path(entry[1]),)
|
if src == 'local':
|
||||||
|
path = entry[1]
|
||||||
|
else:
|
||||||
|
path = prefix.path(entry[1])
|
||||||
|
return (path,)
|
||||||
|
|
||||||
|
|
||||||
def _entry_validate(entry: Sequence[str]) -> None:
|
def _entry_validate(entry: Sequence[str]) -> None:
|
||||||
|
|
@ -75,7 +80,7 @@ def _cmd_from_hook(hook: Hook) -> Tuple[str, ...]:
|
||||||
|
|
||||||
return (
|
return (
|
||||||
*entry[:1], *RSCRIPT_OPTS,
|
*entry[:1], *RSCRIPT_OPTS,
|
||||||
*_prefix_if_file_entry(entry, hook.prefix),
|
*_prefix_if_non_local_file_entry(entry, hook.prefix, hook.src),
|
||||||
*hook.args,
|
*hook.args,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,10 +14,12 @@ def _test_r_parsing(
|
||||||
hook_id,
|
hook_id,
|
||||||
expected_hook_expr={},
|
expected_hook_expr={},
|
||||||
expected_args={},
|
expected_args={},
|
||||||
|
config={},
|
||||||
|
expect_path_prefix=True,
|
||||||
):
|
):
|
||||||
repo_path = 'r_hooks_repo'
|
repo_path = 'r_hooks_repo'
|
||||||
path = make_repo(tempdir_factory, repo_path)
|
path = make_repo(tempdir_factory, repo_path)
|
||||||
config = make_config_from_repo(path)
|
config = config or make_config_from_repo(path)
|
||||||
hook = _get_hook_no_install(config, store, hook_id)
|
hook = _get_hook_no_install(config, store, hook_id)
|
||||||
ret = r._cmd_from_hook(hook)
|
ret = r._cmd_from_hook(hook)
|
||||||
expected_cmd = 'Rscript'
|
expected_cmd = 'Rscript'
|
||||||
|
|
@ -25,7 +27,8 @@ def _test_r_parsing(
|
||||||
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
|
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
|
||||||
)
|
)
|
||||||
expected_path = os.path.join(
|
expected_path = os.path.join(
|
||||||
hook.prefix.prefix_dir, '.'.join([hook_id, 'R']),
|
hook.prefix.prefix_dir if expect_path_prefix else '',
|
||||||
|
f'{hook_id}.R',
|
||||||
)
|
)
|
||||||
expected = (
|
expected = (
|
||||||
expected_cmd,
|
expected_cmd,
|
||||||
|
|
@ -102,3 +105,25 @@ def test_r_parsing_expr_non_Rscirpt(tempdir_factory, store):
|
||||||
|
|
||||||
msg = execinfo.value.args
|
msg = execinfo.value.args
|
||||||
assert msg == ('entry must start with `Rscript`.',)
|
assert msg == ('entry must start with `Rscript`.',)
|
||||||
|
|
||||||
|
|
||||||
|
def test_r_parsing_file_local(tempdir_factory, store):
|
||||||
|
path = 'path/to/script.R'
|
||||||
|
hook_id = 'local-r'
|
||||||
|
config = {
|
||||||
|
'repo': 'local',
|
||||||
|
'hooks': [{
|
||||||
|
'id': hook_id,
|
||||||
|
'name': 'local-r',
|
||||||
|
'entry': f'Rscript {path}',
|
||||||
|
'language': 'r',
|
||||||
|
}],
|
||||||
|
}
|
||||||
|
_test_r_parsing(
|
||||||
|
tempdir_factory,
|
||||||
|
store,
|
||||||
|
hook_id=hook_id,
|
||||||
|
expected_hook_expr=(path,),
|
||||||
|
config=config,
|
||||||
|
expect_path_prefix=False,
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue