Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add renv.lock validation hook #558

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,10 @@
language: script
minimum_pre_commit_version: "2.13.0"
files: '^man/|_pkgdown\.yml'
- id: renv-lockfile-validate
name: renv-lockfile-validate
description: Validate that your `renv.lock` file is valid json and fits the default or provided schema
entry: Rscript inst/hooks/exported/renv-lockfile-validate.R
language: r
minimum_pre_commit_version: "2.13.0"
files: '^renv\.lock$'
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ Suggests:
docopt (>= 0.7.1),
git2r,
glue,
jsonvalidate,
knitr,
lintr,
pkgload,
pkgdown,
reticulate (>= 1.16),
renv (>= 1.0.8),
rmarkdown,
roxygen2,
rstudioapi,
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ issuehunt
isTRUE
jpeg
json
jsonvalidate
KernSmooth
knitr
Ko
Expand Down
38 changes: 38 additions & 0 deletions inst/hooks/exported/renv-lockfile-validate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env Rscript

"Validate renv lockfiles
See `?renv::lockfile_validate()`.
Usage:
lockfile_validate [--schema=<schema>] [--greedy --error --verbose --strict] <files>...
Options:
--schema Path. Path to a custom schema.
--greedy Continue after first error?
--error Throw an error on parse failure?
--verbose If `TRUE`, then an attribute `errors` will list validation failures as a `data.frame`.
--strict Set whether the schema should be parsed strictly or not.
" -> doc

if (!require(renv, quietly = TRUE)) {
stop("{renv} could not be loaded, please install it.")
}
if (packageVersion("renv") < package_version("1.0.8")) {
rlang::abort("You need at least version 1.0.8 of {renv} to run this hook.")
}
if (!require(jsonvalidate, quietly = TRUE)) {
stop("{jsonvalidate} could not be loaded, please install it.")
}

arguments <- precommit::precommit_docopt(doc)
arguments$files <- normalizePath(arguments$files)
if (!is.null(arguments$schema)) {
arguments$schema <- normalizePath(arguments$schema)
}

renv::lockfile_validate(
lockfile = arguments$files,
schema = arguments$schema,
greedy = arguments$greedy,
error = arguments$error,
verbose = arguments$verbose,
strict = arguments$strict
)
7 changes: 7 additions & 0 deletions inst/pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,10 @@
language: script
minimum_pre_commit_version: "2.13.0"
files: '^man/|_pkgdown\.yml'
- id: renv-lockfile-validate
name: renv-lockfile-validate
description: Validate that your `renv.lock` file is valid json and fits the default or provided schema
entry: Rscript inst/hooks/exported/renv-lockfile-validate.R
language: r
minimum_pre_commit_version: "2.13.0"
files: '^renv\.lock$'
20 changes: 20 additions & 0 deletions tests/testthat/in/renv-fail.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"R": {
"Version": "4.2.3",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://cloud.r-project.org"
}
]
},
"Packages": {
"markdown": {
"Package": "markdown",
"Version": "1.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "2324"
}
}
}
19 changes: 19 additions & 0 deletions tests/testthat/in/renv-success.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"R": {
"Version": "4.2.3",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://cloud.r-project.org"
}
]
},
"Packages": {
"markdown": {
"Package": "markdown",
"Version": "1.0",
"Source": "Repository",
"Repository": "CRAN"
}
}
}
12 changes: 12 additions & 0 deletions tests/testthat/test-hook-renv-lockfile-validate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# success
run_test("renv-lockfile-validate",
file_name = "renv-success",
suffix = ".lock", cmd_args = c("--error"),
std_err = NULL
)
# fail
run_test("renv-lockfile-validate",
file_name = "renv-fail",
suffix = ".lock", cmd_args = c("--error"),
std_err = "error validating json"
)
18 changes: 18 additions & 0 deletions vignettes/available-hooks.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,21 @@ development dependencies of the package you want to run this hook for to be
installed, as well as {pkgdown} (without its dependencies).

This hook does not modify files. Added in version 0.3.2.9003.


## `renv-lockfile-validate`

Guarantees you that you don't accidentally commit an invalid renv.lock file.
The below config that uses only `--error` should suffice for most users.

id: renv-lockfile-validate
args: [--error]

This hook does not modify files.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add the version of {precommit} this hook was added to (similar to other hooks)?


**Arguments**

<!-- -->

id: renv-lockfile-validate
args: [--schema=<schema>] [--greedy --error --verbose --strict]
Copy link
Owner

@lorenzwalthert lorenzwalthert Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please link (with hyper-text-link) to docs for env::lockfile_validate() since you just pass arguments there?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's wait for rstudio/renv#2048?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

Loading