mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-16 14:00:20 -06:00
Merge pull request #2728 from pre-commit/fix-r-local-hooks
fix r local hooks
This commit is contained in:
@@ -195,6 +195,7 @@ def _run_single_hook(
|
||||
hook.entry,
|
||||
hook.args,
|
||||
filenames,
|
||||
is_local=hook.src == 'local',
|
||||
require_serial=hook.require_serial,
|
||||
color=use_color,
|
||||
)
|
||||
|
||||
@@ -66,6 +66,7 @@ class Language(Protocol):
|
||||
args: Sequence[str],
|
||||
file_args: Sequence[str],
|
||||
*,
|
||||
is_local: bool,
|
||||
require_serial: bool,
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
|
||||
@@ -127,6 +127,7 @@ def run_hook(
|
||||
args: Sequence[str],
|
||||
file_args: Sequence[str],
|
||||
*,
|
||||
is_local: bool,
|
||||
require_serial: bool,
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]: # pragma: win32 no cover
|
||||
|
||||
@@ -19,6 +19,7 @@ def run_hook(
|
||||
args: Sequence[str],
|
||||
file_args: Sequence[str],
|
||||
*,
|
||||
is_local: bool,
|
||||
require_serial: bool,
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]: # pragma: win32 no cover
|
||||
|
||||
@@ -18,6 +18,7 @@ def run_hook(
|
||||
args: Sequence[str],
|
||||
file_args: Sequence[str],
|
||||
*,
|
||||
is_local: bool,
|
||||
require_serial: bool,
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
|
||||
@@ -146,6 +146,7 @@ def basic_run_hook(
|
||||
args: Sequence[str],
|
||||
file_args: Sequence[str],
|
||||
*,
|
||||
is_local: bool,
|
||||
require_serial: bool,
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
|
||||
@@ -93,6 +93,7 @@ def run_hook(
|
||||
args: Sequence[str],
|
||||
file_args: Sequence[str],
|
||||
*,
|
||||
is_local: bool,
|
||||
require_serial: bool,
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
|
||||
@@ -35,8 +35,13 @@ def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
yield
|
||||
|
||||
|
||||
def _prefix_if_file_entry(entry: list[str], prefix: Prefix) -> Sequence[str]:
|
||||
if entry[1] == '-e':
|
||||
def _prefix_if_file_entry(
|
||||
entry: list[str],
|
||||
prefix: Prefix,
|
||||
*,
|
||||
is_local: bool,
|
||||
) -> Sequence[str]:
|
||||
if entry[1] == '-e' or is_local:
|
||||
return entry[1:]
|
||||
else:
|
||||
return (prefix.path(entry[1]),)
|
||||
@@ -73,11 +78,14 @@ def _cmd_from_hook(
|
||||
prefix: Prefix,
|
||||
entry: str,
|
||||
args: Sequence[str],
|
||||
*,
|
||||
is_local: bool,
|
||||
) -> tuple[str, ...]:
|
||||
cmd = shlex.split(entry)
|
||||
_entry_validate(cmd)
|
||||
|
||||
return (cmd[0], *RSCRIPT_OPTS, *_prefix_if_file_entry(cmd, prefix), *args)
|
||||
cmd_part = _prefix_if_file_entry(cmd, prefix, is_local=is_local)
|
||||
return (cmd[0], *RSCRIPT_OPTS, *cmd_part, *args)
|
||||
|
||||
|
||||
def install_environment(
|
||||
@@ -153,10 +161,11 @@ def run_hook(
|
||||
args: Sequence[str],
|
||||
file_args: Sequence[str],
|
||||
*,
|
||||
is_local: bool,
|
||||
require_serial: bool,
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
cmd = _cmd_from_hook(prefix, entry, args)
|
||||
cmd = _cmd_from_hook(prefix, entry, args, is_local=is_local)
|
||||
return helpers.run_xargs(
|
||||
cmd,
|
||||
file_args,
|
||||
|
||||
@@ -18,6 +18,7 @@ def run_hook(
|
||||
args: Sequence[str],
|
||||
file_args: Sequence[str],
|
||||
*,
|
||||
is_local: bool,
|
||||
require_serial: bool,
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
|
||||
@@ -16,6 +16,7 @@ def run_language(
|
||||
file_args: Sequence[str] = (),
|
||||
version: str = C.DEFAULT,
|
||||
deps: Sequence[str] = (),
|
||||
is_local: bool = False,
|
||||
) -> tuple[int, bytes]:
|
||||
prefix = Prefix(str(path))
|
||||
|
||||
@@ -26,6 +27,7 @@ def run_language(
|
||||
exe,
|
||||
args,
|
||||
file_args,
|
||||
is_local=is_local,
|
||||
require_serial=True,
|
||||
color=False,
|
||||
)
|
||||
|
||||
@@ -14,7 +14,12 @@ from testing.language_helpers import run_language
|
||||
|
||||
|
||||
def test_r_parsing_file_no_opts_no_args(tmp_path):
|
||||
cmd = r._cmd_from_hook(Prefix(str(tmp_path)), 'Rscript some-script.R', ())
|
||||
cmd = r._cmd_from_hook(
|
||||
Prefix(str(tmp_path)),
|
||||
'Rscript some-script.R',
|
||||
(),
|
||||
is_local=False,
|
||||
)
|
||||
assert cmd == (
|
||||
'Rscript',
|
||||
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
|
||||
@@ -38,6 +43,7 @@ def test_r_parsing_file_no_opts_args(tmp_path):
|
||||
Prefix(str(tmp_path)),
|
||||
'Rscript some-script.R',
|
||||
('--no-cache',),
|
||||
is_local=False,
|
||||
)
|
||||
assert cmd == (
|
||||
'Rscript',
|
||||
@@ -48,7 +54,12 @@ def test_r_parsing_file_no_opts_args(tmp_path):
|
||||
|
||||
|
||||
def test_r_parsing_expr_no_opts_no_args1(tmp_path):
|
||||
cmd = r._cmd_from_hook(Prefix(str(tmp_path)), "Rscript -e '1+1'", ())
|
||||
cmd = r._cmd_from_hook(
|
||||
Prefix(str(tmp_path)),
|
||||
"Rscript -e '1+1'",
|
||||
(),
|
||||
is_local=False,
|
||||
)
|
||||
assert cmd == (
|
||||
'Rscript',
|
||||
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
|
||||
@@ -56,6 +67,20 @@ def test_r_parsing_expr_no_opts_no_args1(tmp_path):
|
||||
)
|
||||
|
||||
|
||||
def test_r_parsing_local_hook_path_is_not_expanded(tmp_path):
|
||||
cmd = r._cmd_from_hook(
|
||||
Prefix(str(tmp_path)),
|
||||
'Rscript path/to/thing.R',
|
||||
(),
|
||||
is_local=True,
|
||||
)
|
||||
assert cmd == (
|
||||
'Rscript',
|
||||
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
|
||||
'path/to/thing.R',
|
||||
)
|
||||
|
||||
|
||||
def test_r_parsing_expr_no_opts_no_args2():
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
r._entry_validate(['Rscript', '-e', '1+1', '-e', 'letters'])
|
||||
|
||||
@@ -48,6 +48,7 @@ def _hook_run(hook, filenames, color):
|
||||
hook.entry,
|
||||
hook.args,
|
||||
filenames,
|
||||
is_local=hook.src == 'local',
|
||||
require_serial=hook.require_serial,
|
||||
color=color,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user