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 --load-package to lintr #615

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion API
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ precommit_docopt(doc, args = commandArgs(trailingOnly = TRUE), ...)
robust_purl(path)
roxygen_assert_additional_dependencies()
roxygenize_with_cache(key, dirs)
snippet_generate(snippet, open = rstudioapi::isAvailable(), root = here::here())
snippet_generate(snippet = "additional-deps-roxygenize", open = rstudioapi::isAvailable(), root = here::here())
uninstall_precommit(scope = "repo", ask = "user", root = here::here())
update_precommit()
use_ci(ci = getOption("precommit.ci", "native"), force = FALSE, open = rstudioapi::isAvailable(), root = here::here())
Expand Down
115 changes: 67 additions & 48 deletions R/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,71 +237,79 @@ upstream_repo_url_is_outdated <- function() {
#' * additional-deps-roxygenize: Code to paste into
#' `.pre-commit-config.yaml` for the additional dependencies required by
#' the roxygenize hook.
#' * additional-deps-lintr: Code to paste into
#' `.pre-commit-config.yaml` for the additional dependencies required by
#' the lintr hook if you use `--load-package`.
#' @param snippet Name of the snippet.
#' @param open Whether or not to open the .pre-commit-config.yaml. The default
#' is `TRUE` when working in RStudio. Otherwise, we recommend manually opening
#' the file.
#' @inheritParams fallback_doc
#' @export
snippet_generate <- function(snippet = "",
snippet_generate <- function(snippet = "additional-deps-roxygenize",
open = rstudioapi::isAvailable(),
root = here::here()) {
rlang::arg_match(snippet, c("additional-deps-roxygenize"))
if (snippet == "additional-deps-roxygenize") {
rlang::inform(paste(
"Generating snippet using CRAN versions. If you need another source,",
"specify with syntax that `renv::install()` understands (see examples in",
"help file).",
"\n"
))
deps <- desc::desc_get_deps()
hard_dependencies <- deps[(deps$type %in% c("Depends", "Imports")), "package"] %>%
setdiff("R")
if (length(hard_dependencies) < 1) {
cli::cli_alert_success(paste0(
"According to {.code DESCRIPTION}`, there are no hard dependencies of ",
"your package. You are set."
))
return()
}
hard_dependencies %>%
snippet_generate_impl_additional_deps_roxygenize() %>%
cat(sep = "")
cat("\n")
cli::cli_ul(paste0(
"Replace the `id: roxygenize` key in `.pre-commit-config.yaml` with the ",
"above code."
))
cli::cli_alert_info(paste0(
"Note that CI services like {.url pre-commit.ci} have build-time ",
"restrictions and installing the above dependencies may exceed those, ",
"resulting in a timeout. In addition, system dependencies are not ",
"supported for {.url pre-commit.ci}. See ",
'{.code vignette("ci", package = "precommit")} for details and solutions.'
snippet_generator <- if (snippet == "additional-deps-roxygenize") {
snippet_generate_impl_additional_deps_roxygenize
} else if (snippet == "additional-deps-lintr") {
snippet_generate_impl_additional_deps_lintr
} else {
rlang::abort(paste0('Snippet "', snippet, '" not supported'))
}
rlang::inform(paste(
"Generating snippet using CRAN versions. If you need another source,",
"specify with syntax that `renv::install()` understands (see examples in",
"help file).",
"\n"
))
deps <- desc::desc_get_deps()
hard_dependencies <- deps[(deps$type %in% c("Depends", "Imports")), "package"] %>%
setdiff("R")
if (length(hard_dependencies) < 1) {
cli::cli_alert_success(paste0(
"According to {.code DESCRIPTION}`, there are no hard dependencies of ",
"your package. You are set."
))
remote_deps <- rlang::try_fetch(
desc::desc_get_field("Remotes"),
error = function(e) character()
)
if (length(remote_deps) > 0) {
rlang::warn(paste0(
"It seems you have remote dependencies in your `DESCRIPTION`. You ",
"need to edit the above list manually to match the syntax `renv::install()` ",
"understands, i.e. if you have in your `DESCRIPTION`
return()
}

hard_dependencies %>%
snippet_generator() %>%
cat(sep = "")
cat("\n")
cli::cli_ul(paste0(
"Replace the `id: roxygenize` key in `.pre-commit-config.yaml` with the ",
"above code."
))
cli::cli_alert_info(paste0(
"Note that CI services like {.url pre-commit.ci} have build-time ",
"restrictions and installing the above dependencies may exceed those, ",
"resulting in a timeout. In addition, system dependencies are not ",
"supported for {.url pre-commit.ci}. See ",
'{.code vignette("ci", package = "precommit")} for details and solutions.'
))
remote_deps <- rlang::try_fetch(
desc::desc_get_field("Remotes"),
error = function(e) character()
)
if (length(remote_deps) > 0) {
rlang::warn(paste0(
"It seems you have remote dependencies in your `DESCRIPTION`. You ",
"need to edit the above list manually to match the syntax `renv::install()` ",
"understands, i.e. if you have in your `DESCRIPTION`

Imports:
tidyr
tidyr
Remotes:
tidyverse/tidyr@2fd80d5
tidyverse/tidyr@2fd80d5

You need in your `.pre-commit-config.yaml`

additional_dependencies:
- tidyverse/tidyr@2fd80d5
additional_dependencies:
- tidyverse/tidyr@2fd80d5

"
))
}
))
}
if (open) {
precommit::open_config(root)
Expand All @@ -318,3 +326,14 @@ snippet_generate_impl_additional_deps_roxygenize <- function(packages, with_vers
# roxygen requires loading pkg -> add dependencies from DESCRIPTION
additional_dependencies:\n", out)
}

snippet_generate_impl_additional_deps_lintr <- function(packages, with_version = FALSE) {
out <- paste0(
" - ", packages, "\n",
collapse = ""
) %>%
sort()
paste0(" - id: lintr
# lintr requires loading pkg -> add dependencies from DESCRIPTION
additional_dependencies:\n", out)
}
17 changes: 13 additions & 4 deletions inst/hooks/exported/lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

"Run lintr on R files during a precommit.
Usage:
lintr [--warn_only] <files>...
lintr [options] <files>...

Options:
--warn_only Print lint warnings instead of blocking the commit. Should be
used with `verbose: True` in `.pre-commit-config.yaml`.
Otherwise, lints will never be shown to the user.
--warn_only Print lint warnings instead of blocking the commit. Should be
used with `verbose: True` in `.pre-commit-config.yaml`.
Otherwise, lints will never be shown to the user.
--load_package Use `pkgload::load_all()` to load subject package prior to
running lintr.

" -> doc

arguments <- precommit::precommit_docopt(doc)
Expand All @@ -23,6 +27,11 @@ if (any(lintr_staged)) {
)
}

if (arguments$load_package) {
cat("Attempting to load package\n")
pkgload::load_all()
}

for (path in arguments$files) {
lints <- lintr::lint(path)
if (length(lints) > 0) {
Expand Down
5 changes: 4 additions & 1 deletion man/snippet_generate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion tests/testthat/test-setup.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("snippet generation works", {
test_that("snippet generation works for roxygen", {
local_test_setup(
git = FALSE, use_precommit = FALSE, package = TRUE, install_hooks = FALSE
)
Expand Down Expand Up @@ -27,6 +27,36 @@ test_that("snippet generation works", {
)
})


test_that("snippet generation works for lintr", {
local_test_setup(
git = FALSE, use_precommit = FALSE, package = TRUE, install_hooks = FALSE
)
usethis::use_package("R", "Depends", "3.6.0")
expect_error(
out <- capture_output(snippet_generate("additional-deps-lintr")),
NA,
)
expect_equal(out, "")
usethis::use_package("styler")
expect_error(
out <- capture_output(snippet_generate("additional-deps-lintr")),
NA,
)

expect_match(
out, " - id: lintr\n.* - styler\n$",
)
desc::desc_set("Remotes", "r-lib/styler")
expect_warning(
out <- capture_output(snippet_generate("additional-deps-roxygenize")),
"you have remote dependencies "
)
expect_match(
out, " - id: roxygenize\n.* - styler\n$",
)
})

test_that("snippet generation only includes hard dependencies", {
local_test_setup(
git = FALSE, use_precommit = FALSE, package = TRUE,
Expand Down
Loading
Loading