Skip to content

Commit bf64c5e

Browse files
committed
Add renv-lockfile-validate hook
1 parent 8f3f16a commit bf64c5e

File tree

8 files changed

+122
-0
lines changed

8 files changed

+122
-0
lines changed

.pre-commit-hooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,10 @@
127127
language: script
128128
minimum_pre_commit_version: "2.13.0"
129129
files: '^man/|_pkgdown\.yml'
130+
- id: renv-lockfile-validate
131+
name: renv-lockfile-validate
132+
description: Validate that your `renv.lock` file is valid json and fits the default or provided schema
133+
entry: Rscript inst/hooks/exported/renv-lockfile-validate.R
134+
language: r
135+
minimum_pre_commit_version: "2.13.0"
136+
files: '^renv\.lock$'

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Suggests:
2727
docopt (>= 0.7.1),
2828
git2r,
2929
glue,
30+
jsonvalidate,
3031
knitr,
3132
lintr,
3233
pkgload,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env Rscript
2+
3+
"Validate renv lockfiles
4+
See `?renv::lockfile_validate()`.
5+
Usage:
6+
lockfile_validate [--schema=<schema>] [--greedy --error --verbose --strict] <files>...
7+
Options:
8+
--schema Path. Path to a custom schema.
9+
--greedy Continue after first error?
10+
--error Throw an error on parse failure?
11+
--verbose If `TRUE`, then an attribute `errors` will list validation failures as a `data.frame`.
12+
--strict Set whether the schema should be parsed strictly or not.
13+
" -> doc
14+
15+
if (!require(renv, quietly = TRUE)) {
16+
stop("{renv} could not be loaded, please install it.")
17+
}
18+
if (packageVersion("renv") < package_version("1.0.8")) {
19+
rlang::abort("You need at least version 1.0.8 of {renv} to run this hook.")
20+
}
21+
if (!require(jsonvalidate, quietly = TRUE)) {
22+
stop("{jsonvalidate} could not be loaded, please install it.")
23+
}
24+
25+
arguments <- precommit::precommit_docopt(doc)
26+
arguments$files <- normalizePath(arguments$files)
27+
if (!is.null(arguments$schema)) {
28+
arguments$schema <- normalizePath(arguments$schema)
29+
}
30+
31+
renv::lockfile_validate(
32+
lockfile = arguments$files,
33+
schema = arguments$schema,
34+
greedy = arguments$greedy,
35+
error = arguments$error,
36+
verbose = arguments$verbose,
37+
strict = arguments$strict
38+
)

inst/pre-commit-hooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,10 @@
127127
language: script
128128
minimum_pre_commit_version: "2.13.0"
129129
files: '^man/|_pkgdown\.yml'
130+
- id: renv-lockfile-validate
131+
name: renv-lockfile-validate
132+
description: Validate that your `renv.lock` file is valid json and fits the default or provided schema
133+
entry: Rscript inst/hooks/exported/renv-lockfile-validate.R
134+
language: r
135+
minimum_pre_commit_version: "2.13.0"
136+
files: '^renv\.lock$'

tests/testthat/in/renv-fail.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"R": {
3+
"Version": "4.2.3",
4+
"Repositories": [
5+
{
6+
"Name": "CRAN",
7+
"URL": "https://cloud.r-project.org"
8+
}
9+
]
10+
},
11+
"Packages": {
12+
"markdown": {
13+
"Package": "markdown",
14+
"Version": "1.0",
15+
"Source": "Repository",
16+
"Repository": "CRAN",
17+
"Hash": "2324"
18+
}
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"R": {
3+
"Version": "4.2.3",
4+
"Repositories": [
5+
{
6+
"Name": "CRAN",
7+
"URL": "https://cloud.r-project.org"
8+
}
9+
]
10+
},
11+
"Packages": {
12+
"markdown": {
13+
"Package": "markdown",
14+
"Version": "1.0",
15+
"Source": "Repository",
16+
"Repository": "CRAN"
17+
}
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# success
2+
run_test("renv-lockfile-validate",
3+
file_name = "renv-success",
4+
suffix = ".lock", cmd_args = c("--error"),
5+
std_err = NULL
6+
)
7+
# fail
8+
run_test("renv-lockfile-validate",
9+
file_name = "renv-fail",
10+
suffix = ".lock", cmd_args = c("--error"),
11+
std_err = "error validating json"
12+
)

vignettes/available-hooks.Rmd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,21 @@ development dependencies of the package you want to run this hook for to be
386386
installed, as well as {pkgdown} (without its dependencies).
387387

388388
This hook does not modify files. Added in version 0.3.2.9003.
389+
390+
391+
## `renv-lockfile-validate`
392+
393+
Guarantees you that you don't accidentally commit an invalid renv.lock file.
394+
The below config that uses only `--error` should suffice for most users.
395+
396+
id: renv-lockfile-validate
397+
args: [--error]
398+
399+
This hook does not modify files.
400+
401+
**Arguments**
402+
403+
<!-- -->
404+
405+
id: renv-lockfile-validate
406+
args: [--schema=<schema>] [--greedy --error --verbose --strict]

0 commit comments

Comments
 (0)