Skip to content

Commit 328f1ed

Browse files
Merge pull request #615 from agvandervelde/add_load_package
Add --load-package to lintr
2 parents f948bc9 + 8dd0203 commit 328f1ed

File tree

7 files changed

+268
-169
lines changed

7 files changed

+268
-169
lines changed

API

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ precommit_docopt(doc, args = commandArgs(trailingOnly = TRUE), ...)
1515
robust_purl(path)
1616
roxygen_assert_additional_dependencies()
1717
roxygenize_with_cache(key, dirs)
18-
snippet_generate(snippet, open = rstudioapi::isAvailable(), root = here::here())
18+
snippet_generate(snippet = "additional-deps-roxygenize", open = rstudioapi::isAvailable(), root = here::here())
1919
uninstall_precommit(scope = "repo", ask = "user", root = here::here())
2020
update_precommit()
2121
use_ci(ci = getOption("precommit.ci", "native"), force = FALSE, open = rstudioapi::isAvailable(), root = here::here())

R/release.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ auto_release <- function() {
8181
)
8282

8383
purrr::walk(path_template_config, update_rev_in_config,
84-
new_version = paste0('v', as.character(desc_new$get_version()))
84+
new_version = paste0("v", as.character(desc_new$get_version()))
8585
)
8686
}
8787

R/setup.R

Lines changed: 67 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -237,71 +237,79 @@ upstream_repo_url_is_outdated <- function() {
237237
#' * additional-deps-roxygenize: Code to paste into
238238
#' `.pre-commit-config.yaml` for the additional dependencies required by
239239
#' the roxygenize hook.
240+
#' * additional-deps-lintr: Code to paste into
241+
#' `.pre-commit-config.yaml` for the additional dependencies required by
242+
#' the lintr hook if you use `--load-package`.
240243
#' @param snippet Name of the snippet.
241244
#' @param open Whether or not to open the .pre-commit-config.yaml. The default
242245
#' is `TRUE` when working in RStudio. Otherwise, we recommend manually opening
243246
#' the file.
244247
#' @inheritParams fallback_doc
245248
#' @export
246-
snippet_generate <- function(snippet = "",
249+
snippet_generate <- function(snippet = "additional-deps-roxygenize",
247250
open = rstudioapi::isAvailable(),
248251
root = here::here()) {
249-
rlang::arg_match(snippet, c("additional-deps-roxygenize"))
250-
if (snippet == "additional-deps-roxygenize") {
251-
rlang::inform(paste(
252-
"Generating snippet using CRAN versions. If you need another source,",
253-
"specify with syntax that `renv::install()` understands (see examples in",
254-
"help file).",
255-
"\n"
256-
))
257-
deps <- desc::desc_get_deps()
258-
hard_dependencies <- deps[(deps$type %in% c("Depends", "Imports")), "package"] %>%
259-
setdiff("R")
260-
if (length(hard_dependencies) < 1) {
261-
cli::cli_alert_success(paste0(
262-
"According to {.code DESCRIPTION}`, there are no hard dependencies of ",
263-
"your package. You are set."
264-
))
265-
return()
266-
}
267-
hard_dependencies %>%
268-
snippet_generate_impl_additional_deps_roxygenize() %>%
269-
cat(sep = "")
270-
cat("\n")
271-
cli::cli_ul(paste0(
272-
"Replace the `id: roxygenize` key in `.pre-commit-config.yaml` with the ",
273-
"above code."
274-
))
275-
cli::cli_alert_info(paste0(
276-
"Note that CI services like {.url pre-commit.ci} have build-time ",
277-
"restrictions and installing the above dependencies may exceed those, ",
278-
"resulting in a timeout. In addition, system dependencies are not ",
279-
"supported for {.url pre-commit.ci}. See ",
280-
'{.code vignette("ci", package = "precommit")} for details and solutions.'
252+
snippet_generator <- if (snippet == "additional-deps-roxygenize") {
253+
snippet_generate_impl_additional_deps_roxygenize
254+
} else if (snippet == "additional-deps-lintr") {
255+
snippet_generate_impl_additional_deps_lintr
256+
} else {
257+
rlang::abort(paste0('Snippet "', snippet, '" not supported'))
258+
}
259+
rlang::inform(paste(
260+
"Generating snippet using CRAN versions. If you need another source,",
261+
"specify with syntax that `renv::install()` understands (see examples in",
262+
"help file).",
263+
"\n"
264+
))
265+
deps <- desc::desc_get_deps()
266+
hard_dependencies <- deps[(deps$type %in% c("Depends", "Imports")), "package"] %>%
267+
setdiff("R")
268+
if (length(hard_dependencies) < 1) {
269+
cli::cli_alert_success(paste0(
270+
"According to {.code DESCRIPTION}`, there are no hard dependencies of ",
271+
"your package. You are set."
281272
))
282-
remote_deps <- rlang::try_fetch(
283-
desc::desc_get_field("Remotes"),
284-
error = function(e) character()
285-
)
286-
if (length(remote_deps) > 0) {
287-
rlang::warn(paste0(
288-
"It seems you have remote dependencies in your `DESCRIPTION`. You ",
289-
"need to edit the above list manually to match the syntax `renv::install()` ",
290-
"understands, i.e. if you have in your `DESCRIPTION`
273+
return()
274+
}
275+
276+
hard_dependencies %>%
277+
snippet_generator() %>%
278+
cat(sep = "")
279+
cat("\n")
280+
cli::cli_ul(paste0(
281+
"Replace the `id: roxygenize` key in `.pre-commit-config.yaml` with the ",
282+
"above code."
283+
))
284+
cli::cli_alert_info(paste0(
285+
"Note that CI services like {.url pre-commit.ci} have build-time ",
286+
"restrictions and installing the above dependencies may exceed those, ",
287+
"resulting in a timeout. In addition, system dependencies are not ",
288+
"supported for {.url pre-commit.ci}. See ",
289+
'{.code vignette("ci", package = "precommit")} for details and solutions.'
290+
))
291+
remote_deps <- rlang::try_fetch(
292+
desc::desc_get_field("Remotes"),
293+
error = function(e) character()
294+
)
295+
if (length(remote_deps) > 0) {
296+
rlang::warn(paste0(
297+
"It seems you have remote dependencies in your `DESCRIPTION`. You ",
298+
"need to edit the above list manually to match the syntax `renv::install()` ",
299+
"understands, i.e. if you have in your `DESCRIPTION`
291300
292301
Imports:
293-
tidyr
302+
tidyr
294303
Remotes:
295-
tidyverse/tidyr@2fd80d5
304+
tidyverse/tidyr@2fd80d5
296305
297306
You need in your `.pre-commit-config.yaml`
298307
299-
additional_dependencies:
300-
- tidyverse/tidyr@2fd80d5
308+
additional_dependencies:
309+
- tidyverse/tidyr@2fd80d5
301310
302311
"
303-
))
304-
}
312+
))
305313
}
306314
if (open) {
307315
precommit::open_config(root)
@@ -318,3 +326,14 @@ snippet_generate_impl_additional_deps_roxygenize <- function(packages, with_vers
318326
# roxygen requires loading pkg -> add dependencies from DESCRIPTION
319327
additional_dependencies:\n", out)
320328
}
329+
330+
snippet_generate_impl_additional_deps_lintr <- function(packages, with_version = FALSE) {
331+
out <- paste0(
332+
" - ", packages, "\n",
333+
collapse = ""
334+
) %>%
335+
sort()
336+
paste0(" - id: lintr
337+
# lintr requires loading pkg -> add dependencies from DESCRIPTION
338+
additional_dependencies:\n", out)
339+
}

inst/hooks/exported/lintr.R

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33

44
"Run lintr on R files during a precommit.
55
Usage:
6-
lintr [--warn_only] <files>...
6+
lintr [options] <files>...
7+
78
Options:
8-
--warn_only Print lint warnings instead of blocking the commit. Should be
9-
used with `verbose: True` in `.pre-commit-config.yaml`.
10-
Otherwise, lints will never be shown to the user.
9+
--warn_only Print lint warnings instead of blocking the commit. Should be
10+
used with `verbose: True` in `.pre-commit-config.yaml`.
11+
Otherwise, lints will never be shown to the user.
12+
--load_package Use `pkgload::load_all()` to load subject package prior to
13+
running lintr.
14+
1115
" -> doc
1216

1317
arguments <- precommit::precommit_docopt(doc)
@@ -23,6 +27,11 @@ if (any(lintr_staged)) {
2327
)
2428
}
2529

30+
if (arguments$load_package) {
31+
cat("Attempting to load package\n")
32+
pkgload::load_all()
33+
}
34+
2635
for (path in arguments$files) {
2736
lints <- lintr::lint(path)
2837
if (length(lints) > 0) {

man/snippet_generate.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-setup.R

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
test_that("snippet generation works", {
1+
test_that("snippet generation works for roxygen", {
22
local_test_setup(
33
git = FALSE, use_precommit = FALSE, package = TRUE, install_hooks = FALSE
44
)
@@ -27,6 +27,36 @@ test_that("snippet generation works", {
2727
)
2828
})
2929

30+
31+
test_that("snippet generation works for lintr", {
32+
local_test_setup(
33+
git = FALSE, use_precommit = FALSE, package = TRUE, install_hooks = FALSE
34+
)
35+
usethis::use_package("R", "Depends", "3.6.0")
36+
expect_error(
37+
out <- capture_output(snippet_generate("additional-deps-lintr")),
38+
NA,
39+
)
40+
expect_equal(out, "")
41+
usethis::use_package("styler")
42+
expect_error(
43+
out <- capture_output(snippet_generate("additional-deps-lintr")),
44+
NA,
45+
)
46+
47+
expect_match(
48+
out, " - id: lintr\n.* - styler\n$",
49+
)
50+
desc::desc_set("Remotes", "r-lib/styler")
51+
expect_warning(
52+
out <- capture_output(snippet_generate("additional-deps-roxygenize")),
53+
"you have remote dependencies "
54+
)
55+
expect_match(
56+
out, " - id: roxygenize\n.* - styler\n$",
57+
)
58+
})
59+
3060
test_that("snippet generation only includes hard dependencies", {
3161
local_test_setup(
3262
git = FALSE, use_precommit = FALSE, package = TRUE,

0 commit comments

Comments
 (0)