Merge pull request #2728 from pre-commit/fix-r-local-hooks

fix r local hooks
This commit is contained in:
Anthony Sottile 2023-01-29 18:26:01 -05:00 committed by GitHub
commit 2e1cfa8f05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 51 additions and 6 deletions

View file

@ -195,6 +195,7 @@ def _run_single_hook(
hook.entry, hook.entry,
hook.args, hook.args,
filenames, filenames,
is_local=hook.src == 'local',
require_serial=hook.require_serial, require_serial=hook.require_serial,
color=use_color, color=use_color,
) )

View file

@ -66,6 +66,7 @@ class Language(Protocol):
args: Sequence[str], args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*, *,
is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:

View file

@ -127,6 +127,7 @@ def run_hook(
args: Sequence[str], args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*, *,
is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: # pragma: win32 no cover ) -> tuple[int, bytes]: # pragma: win32 no cover

View file

@ -19,6 +19,7 @@ def run_hook(
args: Sequence[str], args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*, *,
is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: # pragma: win32 no cover ) -> tuple[int, bytes]: # pragma: win32 no cover

View file

@ -18,6 +18,7 @@ def run_hook(
args: Sequence[str], args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*, *,
is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:

View file

@ -146,6 +146,7 @@ def basic_run_hook(
args: Sequence[str], args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*, *,
is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:

View file

@ -93,6 +93,7 @@ def run_hook(
args: Sequence[str], args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*, *,
is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:

View file

@ -35,8 +35,13 @@ def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
yield yield
def _prefix_if_file_entry(entry: list[str], prefix: Prefix) -> Sequence[str]: def _prefix_if_file_entry(
if entry[1] == '-e': entry: list[str],
prefix: Prefix,
*,
is_local: bool,
) -> Sequence[str]:
if entry[1] == '-e' or is_local:
return entry[1:] return entry[1:]
else: else:
return (prefix.path(entry[1]),) return (prefix.path(entry[1]),)
@ -73,11 +78,14 @@ def _cmd_from_hook(
prefix: Prefix, prefix: Prefix,
entry: str, entry: str,
args: Sequence[str], args: Sequence[str],
*,
is_local: bool,
) -> tuple[str, ...]: ) -> tuple[str, ...]:
cmd = shlex.split(entry) cmd = shlex.split(entry)
_entry_validate(cmd) _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( def install_environment(
@ -153,10 +161,11 @@ def run_hook(
args: Sequence[str], args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*, *,
is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> 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( return helpers.run_xargs(
cmd, cmd,
file_args, file_args,

View file

@ -18,6 +18,7 @@ def run_hook(
args: Sequence[str], args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*, *,
is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:

View file

@ -16,6 +16,7 @@ def run_language(
file_args: Sequence[str] = (), file_args: Sequence[str] = (),
version: str = C.DEFAULT, version: str = C.DEFAULT,
deps: Sequence[str] = (), deps: Sequence[str] = (),
is_local: bool = False,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
prefix = Prefix(str(path)) prefix = Prefix(str(path))
@ -26,6 +27,7 @@ def run_language(
exe, exe,
args, args,
file_args, file_args,
is_local=is_local,
require_serial=True, require_serial=True,
color=False, color=False,
) )

View file

@ -14,7 +14,12 @@ from testing.language_helpers import run_language
def test_r_parsing_file_no_opts_no_args(tmp_path): 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 == ( assert cmd == (
'Rscript', 'Rscript',
'--no-save', '--no-restore', '--no-site-file', '--no-environ', '--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)), Prefix(str(tmp_path)),
'Rscript some-script.R', 'Rscript some-script.R',
('--no-cache',), ('--no-cache',),
is_local=False,
) )
assert cmd == ( assert cmd == (
'Rscript', '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): 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 == ( assert cmd == (
'Rscript', 'Rscript',
'--no-save', '--no-restore', '--no-site-file', '--no-environ', '--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(): def test_r_parsing_expr_no_opts_no_args2():
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
r._entry_validate(['Rscript', '-e', '1+1', '-e', 'letters']) r._entry_validate(['Rscript', '-e', '1+1', '-e', 'letters'])

View file

@ -48,6 +48,7 @@ def _hook_run(hook, filenames, color):
hook.entry, hook.entry,
hook.args, hook.args,
filenames, filenames,
is_local=hook.src == 'local',
require_serial=hook.require_serial, require_serial=hook.require_serial,
color=color, color=color,
) )