Skip to content

Commit

Permalink
add additional dependency test, document, clean
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Feb 15, 2021
1 parent c3c3855 commit 476fb44
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 25 deletions.
55 changes: 33 additions & 22 deletions pre_commit/languages/renv.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,16 @@ def in_env(
language_version: str,
) -> Generator[None, None, None]:
# probably not needed as no env have to be set.
# Remove this and get_env_patch altogether or keep
# for consistency with other languages?
envdir = prefix.prefix_dir
with envcontext(get_env_patch(envdir)):
yield


def get_env_patch(venv: str) -> PatchesT:
# probably not needed as .Rprofile is sourced.
# _, R_LIBS_USER, _ = cmd_output(
# 'Rscript', '-e', 'Sys.getenv("R_LIBS_USER")',
# cwd=venv
# )
return (
('PYTHONHOME', UNSET), # R_HOME?
# ('R_LIBS_USER', R_LIBS_USER)
# ('VIRTUAL_ENV', venv)
)
# Assumption: location of Rscript on PATH
return (('R_HOME', UNSET),)


def install_environment(
Expand All @@ -48,23 +42,40 @@ def install_environment(
additional_dependencies: Sequence[str],
) -> None:
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
env_dir = 'renv' # virtual env is always installed at renv
# installation location currently hardcoded
# https://github.com/rstudio/renv/issues/472
env_dir = prefix.path('renv')
# env_dir = prefix.path(directory)
os.mkdir(prefix.path(directory)) # directory needed in _write_state()
with clean_path_on_failure(env_dir):
# for now install the renv in the cloned directory. Alternative:
# copy DSECRIPTION over to new directory and init there.

# potentially need to install renv.
# TODO maybe use renv::restore() and carry renv.lock in {precommit}
# root.
cmd_output_b(
'Rscript', '-e', 'renv::init()', # TODO maybe vanilla
'Rscript', '--vanilla', '-e',
"""
missing_pkgs <- setdiff(
"renv", unname(installed.packages()[, "Package"])
)
install.packages(missing_pkgs)
if (file.exists('renv.lock')) {
renv::restore()
} else {
renv::init()
}
# TODO remove this workaround when renv > 0.12.5 is on CRAN and
# check for minimal version requirement:
# https://github.com/rstudio/renv/issues/634
install.packages("remotes")
remotes::install_local()
""",
cwd=prefix.prefix_dir,
)
if additional_dependencies:
# TODO
pass
cmd_output_b(
'Rscript', '-e',
"""
renv::install(commandArgs(trailingOnly = TRUE))
""", *additional_dependencies,
cwd=prefix.prefix_dir,
)


def run_hook(
Expand All @@ -73,8 +84,8 @@ def run_hook(
color: bool,
) -> Tuple[int, bytes]:
with in_env(hook.prefix, hook.language_version):
# cmd = "Rscript" + '-e' + hook.cmd
# calls .Rprofile -> activates renv.
# activates renv through .Rprofile in
# hook.prefix.prefix_dir
# expose entry to the user for maximal control
return helpers.run_xargs(
hook, hook.cmd, file_args,
Expand Down
6 changes: 6 additions & 0 deletions testing/resources/renv_hooks_repo/.pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
entry: Rscript hello-world.R
language: renv
files: \.[Rr]$
- id: additional-deps
name: Check additional deps
entry: Rscript -e
language: renv
files: \.[Rr]$

# TODO maybe additional deps
23 changes: 20 additions & 3 deletions tests/repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,30 @@ def test_renv_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'renv_hooks_repo',
'hello-world', [os.devnull],
# TODO i'd like to test with a file input I prepared in
# testing/resources_renv_hooks_repo instead os.devnull.
# How?
b'Hello, World, from R!\n',
)


def test_renv_with_additional_dependencies_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'renv_hooks_repo',
'additional-deps', [os.devnull],
b'OK\n',
config_kwargs={
'hooks': [{
'id': 'additional-deps',
'args': [
"""
suppressPackageStartupMessages(library("here"))
cat("OK\n")
""",
],
'additional_dependencies': ['here'],
}],
},
)


def test_run_a_ruby_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'ruby_hooks_repo',
Expand Down

0 comments on commit 476fb44

Please sign in to comment.