test r language directly

This commit is contained in:
Anthony Sottile 2023-01-17 23:25:00 -05:00
parent 6014ebd2e7
commit 7512e3b7e1
9 changed files with 99 additions and 573 deletions

View file

@ -1,13 +1,16 @@
from __future__ import annotations
import os.path
import shutil
import pytest
from pre_commit import envcontext
from pre_commit.languages import r
from pre_commit.prefix import Prefix
from pre_commit.store import _make_local_repo
from pre_commit.util import win_exe
from testing.language_helpers import run_language
def test_r_parsing_file_no_opts_no_args(tmp_path):
@ -97,3 +100,99 @@ def test_rscript_exec_relative_to_r_home():
def test_path_rscript_exec_no_r_home_set():
with envcontext.envcontext((('R_HOME', envcontext.UNSET),)):
assert r._rscript_exec() == 'Rscript'
def test_r_hook(tmp_path):
renv_lock = '''\
{
"R": {
"Version": "4.0.3",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://cloud.r-project.org"
}
]
},
"Packages": {
"renv": {
"Package": "renv",
"Version": "0.12.5",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "5c0cdb37f063c58cdab3c7e9fbb8bd2c"
},
"rprojroot": {
"Package": "rprojroot",
"Version": "1.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "86704667fe0860e4fec35afdfec137f3"
}
}
}
'''
description = '''\
Package: gli.clu
Title: What the Package Does (One Line, Title Case)
Type: Package
Version: 0.0.0.9000
Authors@R:
person(given = "First",
family = "Last",
role = c("aut", "cre"),
email = "first.last@example.com",
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to
pick a license
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Imports:
rprojroot
'''
hello_world_r = '''\
stopifnot(
packageVersion('rprojroot') == '1.0',
packageVersion('gli.clu') == '0.0.0.9000'
)
cat("Hello, World, from R!\n")
'''
tmp_path.joinpath('renv.lock').write_text(renv_lock)
tmp_path.joinpath('DESCRIPTION').write_text(description)
tmp_path.joinpath('hello-world.R').write_text(hello_world_r)
renv_dir = tmp_path.joinpath('renv')
renv_dir.mkdir()
shutil.copy(
os.path.join(
os.path.dirname(__file__),
'../../pre_commit/resources/empty_template_activate.R',
),
renv_dir.joinpath('activate.R'),
)
expected = (0, b'Hello, World, from R!\n')
assert run_language(tmp_path, r, 'Rscript hello-world.R') == expected
def test_r_inline(tmp_path):
_make_local_repo(str(tmp_path))
cmd = '''\
Rscript -e '
stopifnot(packageVersion("rprojroot") == "1.0")
cat(commandArgs(trailingOnly = TRUE), "from R!\n", sep=", ")
'
'''
ret = run_language(
tmp_path,
r,
cmd,
deps=('rprojroot@1.0',),
args=('hi', 'hello'),
)
assert ret == (0, b'hi, hello, from R!\n')

View file

@ -227,54 +227,6 @@ def test_node_hook_with_npm_userconfig_set(tempdir_factory, store, tmpdir):
test_run_a_node_hook(tempdir_factory, store)
def test_r_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'r_hooks_repo',
'hello-world', [os.devnull],
b'Hello, World, from R!\n',
)
def test_r_inline_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'r_hooks_repo',
'hello-world-inline', ['some-file'],
b'Hi-there, some-file, from R!\n',
)
def test_r_with_additional_dependencies_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'r_hooks_repo',
'additional-deps', [os.devnull],
b'OK\n',
config_kwargs={
'hooks': [{
'id': 'additional-deps',
'additional_dependencies': ['cachem@1.0.4'],
}],
},
)
def test_r_local_with_additional_dependencies_hook(store):
config = {
'repo': 'local',
'hooks': [{
'id': 'local-r',
'name': 'local-r',
'entry': 'Rscript -e',
'language': 'r',
'args': ['if (packageVersion("R6") == "2.1.3") cat("OK\n")'],
'additional_dependencies': ['R6@2.1.3'],
}],
}
hook = _get_hook(config, store, 'local-r')
ret, out = _hook_run(hook, (), color=False)
assert ret == 0
assert _norm_out(out) == b'OK\n'
def test_run_a_ruby_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'ruby_hooks_repo',