Skip to content

Commit

Permalink
Add renv-lockfile-validate hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jrdnbradford committed Dec 13, 2024
1 parent 8f3f16a commit bf64c5e
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 0 deletions.
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$'
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Suggests:
docopt (>= 0.7.1),
git2r,
glue,
jsonvalidate,
knitr,
lintr,
pkgload,
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.

**Arguments**

<!-- -->

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

0 comments on commit bf64c5e

Please sign in to comment.