avoid using hook.src in R language

this wasn't meant to be read -- hook.prefix works fine for local too
This commit is contained in:
Anthony Sottile 2023-01-01 18:14:55 -05:00
parent 017fa5c0b8
commit 8e57e8075d
2 changed files with 22 additions and 44 deletions

View file

@ -44,19 +44,11 @@ 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_non_local_file_entry( def _prefix_if_file_entry(entry: list[str], prefix: Prefix) -> Sequence[str]:
entry: Sequence[str],
prefix: Prefix,
src: str,
) -> Sequence[str]:
if entry[1] == '-e': if entry[1] == '-e':
return entry[1:] return entry[1:]
else: else:
if src == 'local': return (prefix.path(entry[1]),)
path = entry[1]
else:
path = prefix.path(entry[1])
return (path,)
def _rscript_exec() -> str: def _rscript_exec() -> str:
@ -67,7 +59,7 @@ def _rscript_exec() -> str:
return os.path.join(r_home, 'bin', win_exe('Rscript')) return os.path.join(r_home, 'bin', win_exe('Rscript'))
def _entry_validate(entry: Sequence[str]) -> None: def _entry_validate(entry: list[str]) -> None:
""" """
Allowed entries: Allowed entries:
# Rscript -e expr # Rscript -e expr
@ -91,8 +83,8 @@ def _cmd_from_hook(hook: Hook) -> tuple[str, ...]:
_entry_validate(entry) _entry_validate(entry)
return ( return (
*entry[:1], *RSCRIPT_OPTS, entry[0], *RSCRIPT_OPTS,
*_prefix_if_non_local_file_entry(entry, hook.prefix, hook.src), *_prefix_if_file_entry(entry, hook.prefix),
*hook.args, *hook.args,
) )

View file

@ -16,27 +16,18 @@ def _test_r_parsing(
tempdir_factory, tempdir_factory,
store, store,
hook_id, hook_id,
expected_hook_expr={}, expected_hook_expr=(),
expected_args={}, expected_args=(),
config={}, config=None,
expect_path_prefix=True,
): ):
repo_path = 'r_hooks_repo' repo = make_repo(tempdir_factory, 'r_hooks_repo')
path = make_repo(tempdir_factory, repo_path) config = make_config_from_repo(repo)
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_path = os.path.join(hook.prefix.prefix_dir, f'{hook_id}.R')
expected_opts = (
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
)
expected_path = os.path.join(
hook.prefix.prefix_dir if expect_path_prefix else '',
f'{hook_id}.R',
)
expected = ( expected = (
expected_cmd, 'Rscript',
*expected_opts, '--no-save', '--no-restore', '--no-site-file', '--no-environ',
*(expected_hook_expr or (expected_path,)), *(expected_hook_expr or (expected_path,)),
*expected_args, *expected_args,
) )
@ -84,9 +75,7 @@ def test_r_parsing_expr_no_opts_no_args2(tempdir_factory, store):
def test_r_parsing_expr_opts_no_args2(tempdir_factory, store): def test_r_parsing_expr_opts_no_args2(tempdir_factory, store):
with pytest.raises(ValueError) as execinfo: with pytest.raises(ValueError) as execinfo:
r._entry_validate( r._entry_validate(
[ ['Rscript', '--vanilla', '-e', '1+1', '-e', 'letters'],
'Rscript', '--vanilla', '-e', '1+1', '-e', 'letters',
],
) )
msg = execinfo.value.args msg = execinfo.value.args
assert msg == ( assert msg == (
@ -112,24 +101,21 @@ def test_r_parsing_expr_non_Rscirpt(tempdir_factory, store):
def test_r_parsing_file_local(tempdir_factory, store): def test_r_parsing_file_local(tempdir_factory, store):
path = 'path/to/script.R'
hook_id = 'local-r'
config = { config = {
'repo': 'local', 'repo': 'local',
'hooks': [{ 'hooks': [{
'id': hook_id, 'id': 'local-r',
'name': 'local-r', 'name': 'local-r',
'entry': f'Rscript {path}', 'entry': 'Rscript path/to/script.R',
'language': 'r', 'language': 'r',
}], }],
} }
_test_r_parsing( hook = _get_hook_no_install(config, store, 'local-r')
tempdir_factory, ret = r._cmd_from_hook(hook)
store, assert ret == (
hook_id=hook_id, 'Rscript',
expected_hook_expr=(path,), '--no-save', '--no-restore', '--no-site-file', '--no-environ',
config=config, hook.prefix.path('path/to/script.R'),
expect_path_prefix=False,
) )