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

Skip to content

Commit 7667ea1

Browse files
refactor r tests with fixtures
1 parent faa6f8c commit 7667ea1

1 file changed

Lines changed: 97 additions & 71 deletions

File tree

tests/languages/r_test.py

Lines changed: 97 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,96 @@
1313
from testing.language_helpers import run_language
1414

1515

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

129219

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

202228
expected = (0, b'Hello, World, from R!\n')
203229
assert run_language(tmp_path, r, 'Rscript hello-world.R') == expected

0 commit comments

Comments
 (0)