Merge pull request #2705 from pre-commit/speed-up-r-tests

speed up R unit tests
This commit is contained in:
Anthony Sottile 2023-01-17 14:51:00 -05:00 committed by GitHub
commit 9a56f8dca0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 98 deletions

View file

@ -64,7 +64,7 @@ def _entry_validate(entry: list[str]) -> None:
raise ValueError('You can supply at most one expression.') raise ValueError('You can supply at most one expression.')
elif len(entry) > 2: elif len(entry) > 2:
raise ValueError( raise ValueError(
'The only valid syntax is `Rscript -e {expr}`', 'The only valid syntax is `Rscript -e {expr}`'
'or `Rscript path/to/hook/script`', 'or `Rscript path/to/hook/script`',
) )

View file

@ -1,26 +1,3 @@
# parsing file
- id: parse-file-no-opts-no-args
name: Say hi
entry: Rscript parse-file-no-opts-no-args.R
language: r
types: [r]
- id: parse-file-no-opts-args
name: Say hi
entry: Rscript parse-file-no-opts-args.R
args: [--no-cache]
language: r
types: [r]
## parsing expr
- id: parse-expr-no-opts-no-args-1
name: Say hi
entry: Rscript -e '1+1'
language: r
types: [r]
- id: parse-expr-args-in-entry-2
name: Say hi
entry: Rscript -e '1+1' -e '3' --no-cache3
language: r
types: [r]
# real world # real world
- id: hello-world - id: hello-world
name: Say hi name: Say hi

View file

@ -6,117 +6,86 @@ import pytest
from pre_commit import envcontext from pre_commit import envcontext
from pre_commit.languages import r from pre_commit.languages import r
from pre_commit.prefix import Prefix
from pre_commit.util import win_exe from pre_commit.util import win_exe
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( def test_r_parsing_file_no_opts_no_args(tmp_path):
tempdir_factory, cmd = r._cmd_from_hook(Prefix(str(tmp_path)), 'Rscript some-script.R', ())
store, assert cmd == (
hook_id,
expected_hook_expr=(),
expected_args=(),
config=None,
):
repo = make_repo(tempdir_factory, 'r_hooks_repo')
config = make_config_from_repo(repo)
hook = _get_hook_no_install(config, store, hook_id)
ret = r._cmd_from_hook(hook.prefix, hook.entry, hook.args)
expected_path = os.path.join(hook.prefix.prefix_dir, f'{hook_id}.R')
expected = (
'Rscript', 'Rscript',
'--no-save', '--no-restore', '--no-site-file', '--no-environ', '--no-save', '--no-restore', '--no-site-file', '--no-environ',
*(expected_hook_expr or (expected_path,)), str(tmp_path.joinpath('some-script.R')),
*expected_args,
) )
assert ret == expected
def test_r_parsing_file_no_opts_no_args(tempdir_factory, store): def test_r_parsing_file_opts_no_args():
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: with pytest.raises(ValueError) as excinfo:
r._entry_validate(['Rscript', '--no-init', '/path/to/file']) r._entry_validate(['Rscript', '--no-init', '/path/to/file'])
msg = excinfo.value.args msg, = excinfo.value.args
assert msg == ( assert msg == (
'The only valid syntax is `Rscript -e {expr}`', 'The only valid syntax is `Rscript -e {expr}`'
'or `Rscript path/to/hook/script`', 'or `Rscript path/to/hook/script`'
) )
def test_r_parsing_file_no_opts_args(tempdir_factory, store): def test_r_parsing_file_no_opts_args(tmp_path):
hook_id = 'parse-file-no-opts-args' cmd = r._cmd_from_hook(
expected_args = ['--no-cache'] Prefix(str(tmp_path)),
_test_r_parsing( 'Rscript some-script.R',
tempdir_factory, store, hook_id, expected_args=expected_args, ('--no-cache',),
)
assert cmd == (
'Rscript',
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
str(tmp_path.joinpath('some-script.R')),
'--no-cache',
) )
def test_r_parsing_expr_no_opts_no_args1(tempdir_factory, store): def test_r_parsing_expr_no_opts_no_args1(tmp_path):
hook_id = 'parse-expr-no-opts-no-args-1' cmd = r._cmd_from_hook(Prefix(str(tmp_path)), "Rscript -e '1+1'", ())
_test_r_parsing( assert cmd == (
tempdir_factory, store, hook_id, expected_hook_expr=('-e', '1+1'), 'Rscript',
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
'-e', '1+1',
) )
def test_r_parsing_expr_no_opts_no_args2(tempdir_factory, store): def test_r_parsing_expr_no_opts_no_args2():
with pytest.raises(ValueError) as execinfo: with pytest.raises(ValueError) as excinfo:
r._entry_validate(['Rscript', '-e', '1+1', '-e', 'letters']) r._entry_validate(['Rscript', '-e', '1+1', '-e', 'letters'])
msg = execinfo.value.args msg, = excinfo.value.args
assert msg == ('You can supply at most one expression.',) assert msg == 'You can supply at most one expression.'
def test_r_parsing_expr_opts_no_args2(tempdir_factory, store): def test_r_parsing_expr_opts_no_args2():
with pytest.raises(ValueError) as execinfo: with pytest.raises(ValueError) as excinfo:
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, = excinfo.value.args
assert msg == ( assert msg == (
'The only valid syntax is `Rscript -e {expr}`', 'The only valid syntax is `Rscript -e {expr}`'
'or `Rscript path/to/hook/script`', 'or `Rscript path/to/hook/script`'
) )
def test_r_parsing_expr_args_in_entry2(tempdir_factory, store): def test_r_parsing_expr_args_in_entry2():
with pytest.raises(ValueError) as execinfo: with pytest.raises(ValueError) as excinfo:
r._entry_validate(['Rscript', '-e', 'expr1', '--another-arg']) r._entry_validate(['Rscript', '-e', 'expr1', '--another-arg'])
msg = execinfo.value.args msg, = excinfo.value.args
assert msg == ('You can supply at most one expression.',) assert msg == 'You can supply at most one expression.'
def test_r_parsing_expr_non_Rscirpt(tempdir_factory, store): def test_r_parsing_expr_non_Rscirpt():
with pytest.raises(ValueError) as execinfo: with pytest.raises(ValueError) as excinfo:
r._entry_validate(['AnotherScript', '-e', '{{}}']) r._entry_validate(['AnotherScript', '-e', '{{}}'])
msg = execinfo.value.args msg, = excinfo.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):
config = {
'repo': 'local',
'hooks': [{
'id': 'local-r',
'name': 'local-r',
'entry': 'Rscript path/to/script.R',
'language': 'r',
}],
}
hook = _get_hook_no_install(config, store, 'local-r')
ret = r._cmd_from_hook(hook.prefix, hook.entry, hook.args)
assert ret == (
'Rscript',
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
hook.prefix.path('path/to/script.R'),
)
def test_rscript_exec_relative_to_r_home(): def test_rscript_exec_relative_to_r_home():