Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c454fb6

Browse files
move closer towards conventions in this repo
1 parent 1241c8d commit c454fb6

2 files changed

Lines changed: 93 additions & 97 deletions

File tree

pre_commit/languages/r.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import shutil
77
import tempfile
88
import textwrap
9-
import typing
109
from collections.abc import Generator
1110
from collections.abc import Sequence
11+
from typing import Any
1212

1313
from pre_commit import lang_base
1414
from pre_commit.envcontext import envcontext
@@ -23,7 +23,7 @@
2323
get_default_version = lang_base.basic_get_default_version
2424

2525

26-
def _execute_vanilla_r_code_as_script(code: str, **kwargs: typing.Any) -> str:
26+
def _execute_vanilla_r_code_as_script(code: str, **kwargs: Any) -> str:
2727
with _r_code_in_tempfile(code) as f:
2828
cmd_out = cmd_output_b(_rscript_exec(), '--vanilla', f, **kwargs)
2929
return cmd_out[1].decode().rstrip('\n')
@@ -50,13 +50,9 @@ def _write_current_r_version(envdir: str) -> None:
5050

5151

5252
def health_check(prefix: Prefix, version: str) -> str | None:
53-
# prefix: cloned hook repo .../{precommit}
54-
# env dir .../{precommit}/renv-default/
5553
envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)
5654

5755
r_version_installation = _read_installed_version(envdir=envdir)
58-
# TODO what if none set? -> record current?
59-
# TODO how is this affecting old hooks environments
6056
r_version_current_executable = _read_executable_version(envdir=envdir)
6157
if r_version_installation in {'NULL', ''}:
6258
return (
@@ -180,6 +176,7 @@ def install_environment(
180176
'setwd(old); ',
181177
'renv::load("', getwd(), '");}})'
182178
)
179+
writeLines(activate_statement, 'activate.R')
183180
is_package <- tryCatch(
184181
{{
185182
path_desc <- file.path(prefix_dir, 'DESCRIPTION')
@@ -191,7 +188,6 @@ def install_environment(
191188
if (is_package) {{
192189
renv::install(prefix_dir)
193190
}}
194-
writeLines(activate_statement, 'activate.R')
195191
"""
196192

197193
with _r_code_in_tempfile(r_code_inst_environment) as f:

tests/languages/r_test.py

Lines changed: 90 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -16,96 +16,6 @@
1616
from testing.language_helpers import run_language
1717

1818

19-
@pytest.fixture
20-
def renv_lock_file(tmp_path):
21-
renv_lock = '''\
22-
{
23-
"R": {
24-
"Version": "4.0.3",
25-
"Repositories": [
26-
{
27-
"Name": "CRAN",
28-
"URL": "https://cloud.r-project.org"
29-
}
30-
]
31-
},
32-
"Packages": {
33-
"renv": {
34-
"Package": "renv",
35-
"Version": "0.12.5",
36-
"Source": "Repository",
37-
"Repository": "CRAN",
38-
"Hash": "5c0cdb37f063c58cdab3c7e9fbb8bd2c"
39-
},
40-
"rprojroot": {
41-
"Package": "rprojroot",
42-
"Version": "1.0",
43-
"Source": "Repository",
44-
"Repository": "CRAN",
45-
"Hash": "86704667fe0860e4fec35afdfec137f3"
46-
}
47-
}
48-
}
49-
'''
50-
tmp_path.joinpath('renv.lock').write_text(renv_lock)
51-
yield
52-
53-
54-
@pytest.fixture
55-
def description_file(tmp_path):
56-
description = '''\
57-
Package: gli.clu
58-
Title: What the Package Does (One Line, Title Case)
59-
Type: Package
60-
Version: 0.0.0.9000
61-
Authors@R:
62-
person(given = "First",
63-
family = "Last",
64-
role = c("aut", "cre"),
65-
email = "[email protected]",
66-
comment = c(ORCID = "YOUR-ORCID-ID"))
67-
Description: What the package does (one paragraph).
68-
License: `use_mit_license()`, `use_gpl3_license()` or friends to
69-
pick a license
70-
Encoding: UTF-8
71-
LazyData: true
72-
Roxygen: list(markdown = TRUE)
73-
RoxygenNote: 7.1.1
74-
Imports:
75-
rprojroot
76-
'''
77-
tmp_path.joinpath('DESCRIPTION').write_text(description)
78-
yield
79-
80-
81-
@pytest.fixture
82-
def hello_world_file(tmp_path):
83-
hello_world = '''\
84-
stopifnot(
85-
packageVersion('rprojroot') == '1.0',
86-
packageVersion('gli.clu') == '0.0.0.9000'
87-
)
88-
cat("Hello, World, from R!\n")
89-
'''
90-
tmp_path.joinpath('hello-world.R').write_text(hello_world)
91-
yield
92-
93-
94-
@pytest.fixture
95-
def renv_folder(tmp_path):
96-
97-
renv_dir = tmp_path.joinpath('renv')
98-
renv_dir.mkdir()
99-
shutil.copy(
100-
os.path.join(
101-
os.path.dirname(__file__),
102-
'../../pre_commit/resources/empty_template_activate.R',
103-
),
104-
renv_dir.joinpath('activate.R'),
105-
)
106-
yield
107-
108-
10919
def test_r_parsing_file_no_opts_no_args(tmp_path):
11020
cmd = r._cmd_from_hook(
11121
Prefix(str(tmp_path)),
@@ -220,6 +130,96 @@ def test_path_rscript_exec_no_r_home_set():
220130
assert r._rscript_exec() == 'Rscript'
221131

222132

133+
@pytest.fixture
134+
def renv_lock_file(tmp_path):
135+
renv_lock = '''\
136+
{
137+
"R": {
138+
"Version": "4.0.3",
139+
"Repositories": [
140+
{
141+
"Name": "CRAN",
142+
"URL": "https://cloud.r-project.org"
143+
}
144+
]
145+
},
146+
"Packages": {
147+
"renv": {
148+
"Package": "renv",
149+
"Version": "0.12.5",
150+
"Source": "Repository",
151+
"Repository": "CRAN",
152+
"Hash": "5c0cdb37f063c58cdab3c7e9fbb8bd2c"
153+
},
154+
"rprojroot": {
155+
"Package": "rprojroot",
156+
"Version": "1.0",
157+
"Source": "Repository",
158+
"Repository": "CRAN",
159+
"Hash": "86704667fe0860e4fec35afdfec137f3"
160+
}
161+
}
162+
}
163+
'''
164+
tmp_path.joinpath('renv.lock').write_text(renv_lock)
165+
yield
166+
167+
168+
@pytest.fixture
169+
def description_file(tmp_path):
170+
description = '''\
171+
Package: gli.clu
172+
Title: What the Package Does (One Line, Title Case)
173+
Type: Package
174+
Version: 0.0.0.9000
175+
Authors@R:
176+
person(given = "First",
177+
family = "Last",
178+
role = c("aut", "cre"),
179+
email = "[email protected]",
180+
comment = c(ORCID = "YOUR-ORCID-ID"))
181+
Description: What the package does (one paragraph).
182+
License: `use_mit_license()`, `use_gpl3_license()` or friends to
183+
pick a license
184+
Encoding: UTF-8
185+
LazyData: true
186+
Roxygen: list(markdown = TRUE)
187+
RoxygenNote: 7.1.1
188+
Imports:
189+
rprojroot
190+
'''
191+
tmp_path.joinpath('DESCRIPTION').write_text(description)
192+
yield
193+
194+
195+
@pytest.fixture
196+
def hello_world_file(tmp_path):
197+
hello_world = '''\
198+
stopifnot(
199+
packageVersion('rprojroot') == '1.0',
200+
packageVersion('gli.clu') == '0.0.0.9000'
201+
)
202+
cat("Hello, World, from R!\n")
203+
'''
204+
tmp_path.joinpath('hello-world.R').write_text(hello_world)
205+
yield
206+
207+
208+
@pytest.fixture
209+
def renv_folder(tmp_path):
210+
211+
renv_dir = tmp_path.joinpath('renv')
212+
renv_dir.mkdir()
213+
shutil.copy(
214+
os.path.join(
215+
os.path.dirname(__file__),
216+
'../../pre_commit/resources/empty_template_activate.R',
217+
),
218+
renv_dir.joinpath('activate.R'),
219+
)
220+
yield
221+
222+
223223
def test_r_hook(
224224
tmp_path,
225225
renv_lock_file,

0 commit comments

Comments
 (0)