|
6 | 6 |
|
7 | 7 | from pre_commit import envcontext |
8 | 8 | from pre_commit.languages import r |
| 9 | +from pre_commit.prefix import Prefix |
9 | 10 | 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 == ( |
29 | 16 | 'Rscript', |
30 | 17 | '--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')), |
33 | 19 | ) |
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) |
40 | 20 |
|
41 | 21 |
|
42 | | -def test_r_parsing_file_opts_no_args(tempdir_factory, store): |
| 22 | +def test_r_parsing_file_opts_no_args(): |
43 | 23 | with pytest.raises(ValueError) as excinfo: |
44 | 24 | r._entry_validate(['Rscript', '--no-init', '/path/to/file']) |
45 | 25 |
|
46 | | - msg = excinfo.value.args |
| 26 | + msg, = excinfo.value.args |
47 | 27 | 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`' |
50 | 30 | ) |
51 | 31 |
|
52 | 32 |
|
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', |
58 | 44 | ) |
59 | 45 |
|
60 | 46 |
|
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', |
65 | 53 | ) |
66 | 54 |
|
67 | 55 |
|
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: |
70 | 58 | 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.' |
73 | 61 |
|
74 | 62 |
|
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: |
77 | 65 | r._entry_validate( |
78 | 66 | ['Rscript', '--vanilla', '-e', '1+1', '-e', 'letters'], |
79 | 67 | ) |
80 | | - msg = execinfo.value.args |
| 68 | + msg, = excinfo.value.args |
81 | 69 | 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`' |
84 | 72 | ) |
85 | 73 |
|
86 | 74 |
|
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: |
89 | 77 | r._entry_validate(['Rscript', '-e', 'expr1', '--another-arg']) |
90 | 78 |
|
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.' |
93 | 81 |
|
94 | 82 |
|
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: |
97 | 85 | r._entry_validate(['AnotherScript', '-e', '{{}}']) |
98 | 86 |
|
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`.' |
120 | 89 |
|
121 | 90 |
|
122 | 91 | def test_rscript_exec_relative_to_r_home(): |
|
0 commit comments