Skip to content

Commit 966c67a

Browse files
committed
speed up R unit tests
1 parent a26b631 commit 966c67a

File tree

3 files changed

+46
-100
lines changed

3 files changed

+46
-100
lines changed

pre_commit/languages/r.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def _entry_validate(entry: list[str]) -> None:
6464
raise ValueError('You can supply at most one expression.')
6565
elif len(entry) > 2:
6666
raise ValueError(
67-
'The only valid syntax is `Rscript -e {expr}`',
67+
'The only valid syntax is `Rscript -e {expr}`'
6868
'or `Rscript path/to/hook/script`',
6969
)
7070

testing/resources/r_hooks_repo/.pre-commit-hooks.yaml

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,3 @@
1-
# parsing file
2-
- id: parse-file-no-opts-no-args
3-
name: Say hi
4-
entry: Rscript parse-file-no-opts-no-args.R
5-
language: r
6-
types: [r]
7-
- id: parse-file-no-opts-args
8-
name: Say hi
9-
entry: Rscript parse-file-no-opts-args.R
10-
args: [--no-cache]
11-
language: r
12-
types: [r]
13-
## parsing expr
14-
- id: parse-expr-no-opts-no-args-1
15-
name: Say hi
16-
entry: Rscript -e '1+1'
17-
language: r
18-
types: [r]
19-
- id: parse-expr-args-in-entry-2
20-
name: Say hi
21-
entry: Rscript -e '1+1' -e '3' --no-cache3
22-
language: r
23-
types: [r]
241
# real world
252
- id: hello-world
263
name: Say hi

tests/languages/r_test.py

Lines changed: 45 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,117 +6,86 @@
66

77
from pre_commit import envcontext
88
from pre_commit.languages import r
9+
from pre_commit.prefix import Prefix
910
from pre_commit.util import win_exe
10-
from testing.fixtures import make_config_from_repo
11-
from testing.fixtures import make_repo
12-
from tests.repository_test import _get_hook_no_install
13-
14-
15-
def _test_r_parsing(
16-
tempdir_factory,
17-
store,
18-
hook_id,
19-
expected_hook_expr=(),
20-
expected_args=(),
21-
config=None,
22-
):
23-
repo = make_repo(tempdir_factory, 'r_hooks_repo')
24-
config = make_config_from_repo(repo)
25-
hook = _get_hook_no_install(config, store, hook_id)
26-
ret = r._cmd_from_hook(hook.prefix, hook.entry, hook.args)
27-
expected_path = os.path.join(hook.prefix.prefix_dir, f'{hook_id}.R')
28-
expected = (
11+
12+
13+
def test_r_parsing_file_no_opts_no_args(tmp_path):
14+
cmd = r._cmd_from_hook(Prefix(str(tmp_path)), 'Rscript some-script.R', ())
15+
assert cmd == (
2916
'Rscript',
3017
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
31-
*(expected_hook_expr or (expected_path,)),
32-
*expected_args,
18+
str(tmp_path.joinpath('some-script.R')),
3319
)
34-
assert ret == expected
35-
36-
37-
def test_r_parsing_file_no_opts_no_args(tempdir_factory, store):
38-
hook_id = 'parse-file-no-opts-no-args'
39-
_test_r_parsing(tempdir_factory, store, hook_id)
4020

4121

42-
def test_r_parsing_file_opts_no_args(tempdir_factory, store):
22+
def test_r_parsing_file_opts_no_args():
4323
with pytest.raises(ValueError) as excinfo:
4424
r._entry_validate(['Rscript', '--no-init', '/path/to/file'])
4525

46-
msg = excinfo.value.args
26+
msg, = excinfo.value.args
4727
assert msg == (
48-
'The only valid syntax is `Rscript -e {expr}`',
49-
'or `Rscript path/to/hook/script`',
28+
'The only valid syntax is `Rscript -e {expr}`'
29+
'or `Rscript path/to/hook/script`'
5030
)
5131

5232

53-
def test_r_parsing_file_no_opts_args(tempdir_factory, store):
54-
hook_id = 'parse-file-no-opts-args'
55-
expected_args = ['--no-cache']
56-
_test_r_parsing(
57-
tempdir_factory, store, hook_id, expected_args=expected_args,
33+
def test_r_parsing_file_no_opts_args(tmp_path):
34+
cmd = r._cmd_from_hook(
35+
Prefix(str(tmp_path)),
36+
'Rscript some-script.R',
37+
('--no-cache',),
38+
)
39+
assert cmd == (
40+
'Rscript',
41+
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
42+
str(tmp_path.joinpath('some-script.R')),
43+
'--no-cache',
5844
)
5945

6046

61-
def test_r_parsing_expr_no_opts_no_args1(tempdir_factory, store):
62-
hook_id = 'parse-expr-no-opts-no-args-1'
63-
_test_r_parsing(
64-
tempdir_factory, store, hook_id, expected_hook_expr=('-e', '1+1'),
47+
def test_r_parsing_expr_no_opts_no_args1(tmp_path):
48+
cmd = r._cmd_from_hook(Prefix(str(tmp_path)), "Rscript -e '1+1'", ())
49+
assert cmd == (
50+
'Rscript',
51+
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
52+
'-e', '1+1',
6553
)
6654

6755

68-
def test_r_parsing_expr_no_opts_no_args2(tempdir_factory, store):
69-
with pytest.raises(ValueError) as execinfo:
56+
def test_r_parsing_expr_no_opts_no_args2():
57+
with pytest.raises(ValueError) as excinfo:
7058
r._entry_validate(['Rscript', '-e', '1+1', '-e', 'letters'])
71-
msg = execinfo.value.args
72-
assert msg == ('You can supply at most one expression.',)
59+
msg, = excinfo.value.args
60+
assert msg == 'You can supply at most one expression.'
7361

7462

75-
def test_r_parsing_expr_opts_no_args2(tempdir_factory, store):
76-
with pytest.raises(ValueError) as execinfo:
63+
def test_r_parsing_expr_opts_no_args2():
64+
with pytest.raises(ValueError) as excinfo:
7765
r._entry_validate(
7866
['Rscript', '--vanilla', '-e', '1+1', '-e', 'letters'],
7967
)
80-
msg = execinfo.value.args
68+
msg, = excinfo.value.args
8169
assert msg == (
82-
'The only valid syntax is `Rscript -e {expr}`',
83-
'or `Rscript path/to/hook/script`',
70+
'The only valid syntax is `Rscript -e {expr}`'
71+
'or `Rscript path/to/hook/script`'
8472
)
8573

8674

87-
def test_r_parsing_expr_args_in_entry2(tempdir_factory, store):
88-
with pytest.raises(ValueError) as execinfo:
75+
def test_r_parsing_expr_args_in_entry2():
76+
with pytest.raises(ValueError) as excinfo:
8977
r._entry_validate(['Rscript', '-e', 'expr1', '--another-arg'])
9078

91-
msg = execinfo.value.args
92-
assert msg == ('You can supply at most one expression.',)
79+
msg, = excinfo.value.args
80+
assert msg == 'You can supply at most one expression.'
9381

9482

95-
def test_r_parsing_expr_non_Rscirpt(tempdir_factory, store):
96-
with pytest.raises(ValueError) as execinfo:
83+
def test_r_parsing_expr_non_Rscirpt():
84+
with pytest.raises(ValueError) as excinfo:
9785
r._entry_validate(['AnotherScript', '-e', '{{}}'])
9886

99-
msg = execinfo.value.args
100-
assert msg == ('entry must start with `Rscript`.',)
101-
102-
103-
def test_r_parsing_file_local(tempdir_factory, store):
104-
config = {
105-
'repo': 'local',
106-
'hooks': [{
107-
'id': 'local-r',
108-
'name': 'local-r',
109-
'entry': 'Rscript path/to/script.R',
110-
'language': 'r',
111-
}],
112-
}
113-
hook = _get_hook_no_install(config, store, 'local-r')
114-
ret = r._cmd_from_hook(hook.prefix, hook.entry, hook.args)
115-
assert ret == (
116-
'Rscript',
117-
'--no-save', '--no-restore', '--no-site-file', '--no-environ',
118-
hook.prefix.path('path/to/script.R'),
119-
)
87+
msg, = excinfo.value.args
88+
assert msg == 'entry must start with `Rscript`.'
12089

12190

12291
def test_rscript_exec_relative_to_r_home():

0 commit comments

Comments
 (0)