mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 00:04:42 +04:00
add support for R via renv
This commit is contained in:
parent
b193d9e67b
commit
f1502119a2
15 changed files with 443 additions and 4 deletions
104
tests/languages/r_test.py
Normal file
104
tests/languages/r_test.py
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
import os.path
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit.languages import r
|
||||
from testing.fixtures import make_config_from_repo
|
||||
from testing.fixtures import make_repo
|
||||
from tests.repository_test import _get_hook_no_install
|
||||
|
||||
|
||||
def _test_r_parsing(
|
||||
tempdir_factory,
|
||||
store,
|
||||
hook_id,
|
||||
expected_hook_expr={},
|
||||
expected_args={},
|
||||
):
|
||||
repo_path = 'r_hooks_repo'
|
||||
path = make_repo(tempdir_factory, repo_path)
|
||||
config = make_config_from_repo(path)
|
||||
hook = _get_hook_no_install(config, store, hook_id)
|
||||
ret = r._cmd_from_hook(hook)
|
||||
expected_cmd = 'Rscript'
|
||||
expected_opts = (
|
||||
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
|
||||
)
|
||||
expected_path = os.path.join(
|
||||
hook.prefix.prefix_dir, '.'.join([hook_id, 'R']),
|
||||
)
|
||||
expected = (
|
||||
expected_cmd,
|
||||
*expected_opts,
|
||||
*(expected_hook_expr or (expected_path,)),
|
||||
*expected_args,
|
||||
)
|
||||
assert ret == expected
|
||||
|
||||
|
||||
def test_r_parsing_file_no_opts_no_args(tempdir_factory, store):
|
||||
hook_id = 'parse-file-no-opts-no-args'
|
||||
_test_r_parsing(tempdir_factory, store, hook_id)
|
||||
|
||||
|
||||
def test_r_parsing_file_opts_no_args(tempdir_factory, store):
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
r._entry_validate(['Rscript', '--no-init', '/path/to/file'])
|
||||
|
||||
msg = excinfo.value.args
|
||||
assert msg == (
|
||||
'The only valid syntax is `Rscript -e {expr}`',
|
||||
'or `Rscript path/to/hook/script`',
|
||||
)
|
||||
|
||||
|
||||
def test_r_parsing_file_no_opts_args(tempdir_factory, store):
|
||||
hook_id = 'parse-file-no-opts-args'
|
||||
expected_args = ['--no-cache']
|
||||
_test_r_parsing(
|
||||
tempdir_factory, store, hook_id, expected_args=expected_args,
|
||||
)
|
||||
|
||||
|
||||
def test_r_parsing_expr_no_opts_no_args1(tempdir_factory, store):
|
||||
hook_id = 'parse-expr-no-opts-no-args-1'
|
||||
_test_r_parsing(
|
||||
tempdir_factory, store, hook_id, expected_hook_expr=('-e', '1+1'),
|
||||
)
|
||||
|
||||
|
||||
def test_r_parsing_expr_no_opts_no_args2(tempdir_factory, store):
|
||||
with pytest.raises(ValueError) as execinfo:
|
||||
r._entry_validate(['Rscript', '-e', '1+1', '-e', 'letters'])
|
||||
msg = execinfo.value.args
|
||||
assert msg == ('You can supply at most one expression.',)
|
||||
|
||||
|
||||
def test_r_parsing_expr_opts_no_args2(tempdir_factory, store):
|
||||
with pytest.raises(ValueError) as execinfo:
|
||||
r._entry_validate(
|
||||
[
|
||||
'Rscript', '--vanilla', '-e', '1+1', '-e', 'letters',
|
||||
],
|
||||
)
|
||||
msg = execinfo.value.args
|
||||
assert msg == (
|
||||
'The only valid syntax is `Rscript -e {expr}`',
|
||||
'or `Rscript path/to/hook/script`',
|
||||
)
|
||||
|
||||
|
||||
def test_r_parsing_expr_args_in_entry2(tempdir_factory, store):
|
||||
with pytest.raises(ValueError) as execinfo:
|
||||
r._entry_validate(['Rscript', '-e', 'expr1', '--another-arg'])
|
||||
|
||||
msg = execinfo.value.args
|
||||
assert msg == ('You can supply at most one expression.',)
|
||||
|
||||
|
||||
def test_r_parsing_expr_non_Rscirpt(tempdir_factory, store):
|
||||
with pytest.raises(ValueError) as execinfo:
|
||||
r._entry_validate(['AnotherScript', '-e', '{{}}'])
|
||||
|
||||
msg = execinfo.value.args
|
||||
assert msg == ('entry must start with `Rscript`.',)
|
||||
|
|
@ -279,6 +279,54 @@ def test_node_hook_with_npm_userconfig_set(tempdir_factory, store, tmpdir):
|
|||
test_run_a_node_hook(tempdir_factory, store)
|
||||
|
||||
|
||||
def test_r_hook(tempdir_factory, store):
|
||||
_test_hook_repo(
|
||||
tempdir_factory, store, 'r_hooks_repo',
|
||||
'hello-world', [os.devnull],
|
||||
b'Hello, World, from R!\n',
|
||||
)
|
||||
|
||||
|
||||
def test_r_inline_hook(tempdir_factory, store):
|
||||
_test_hook_repo(
|
||||
tempdir_factory, store, 'r_hooks_repo',
|
||||
'hello-world-inline', ['some-file'],
|
||||
b'Hi-there, some-file, from R!\n',
|
||||
)
|
||||
|
||||
|
||||
def test_r_with_additional_dependencies_hook(tempdir_factory, store):
|
||||
_test_hook_repo(
|
||||
tempdir_factory, store, 'r_hooks_repo',
|
||||
'additional-deps', [os.devnull],
|
||||
b'OK\n',
|
||||
config_kwargs={
|
||||
'hooks': [{
|
||||
'id': 'additional-deps',
|
||||
'additional_dependencies': ['cachem@1.0.4'],
|
||||
}],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_r_local_with_additional_dependencies_hook(store):
|
||||
config = {
|
||||
'repo': 'local',
|
||||
'hooks': [{
|
||||
'id': 'local-r',
|
||||
'name': 'local-r',
|
||||
'entry': 'Rscript -e',
|
||||
'language': 'r',
|
||||
'args': ['if (packageVersion("R6") == "2.1.3") cat("OK\n")'],
|
||||
'additional_dependencies': ['R6@2.1.3'],
|
||||
}],
|
||||
}
|
||||
hook = _get_hook(config, store, 'local-r')
|
||||
ret, out = _hook_run(hook, (), color=False)
|
||||
assert ret == 0
|
||||
assert _norm_out(out) == b'OK\n'
|
||||
|
||||
|
||||
def test_run_a_ruby_hook(tempdir_factory, store):
|
||||
_test_hook_repo(
|
||||
tempdir_factory, store, 'ruby_hooks_repo',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue