From 0b471833861359fba69871aaad3cdbaacb267cd2 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Wed, 5 May 2021 15:08:56 +0200 Subject: [PATCH 01/36] make hooks repo a variable --- R/aaa.R | 1 + R/assert.R | 2 +- R/config.R | 2 +- R/release.R | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 R/aaa.R diff --git a/R/aaa.R b/R/aaa.R new file mode 100644 index 000000000..330837885 --- /dev/null +++ b/R/aaa.R @@ -0,0 +1 @@ +hooks_repo <- "https://github.com/lorenzwalthert/precommit" diff --git a/R/assert.R b/R/assert.R index de0d41786..fc53003cf 100644 --- a/R/assert.R +++ b/R/assert.R @@ -35,7 +35,7 @@ assert_correct_upstream_repo_url <- function() { if (upstream_repo_url_is_outdated()) { usethis::ui_info(c( "The repo https://github.com/lorenzwalthert/pre-commit-hooks ", - "has moved to https://github.com/lorenzwalthert/precommit. ", + "has moved to ", hooks_repo, ". ", "Please fix the URL in .pre-commit-config.yaml, ", "most confortably with `precommit::open_config()`." )) diff --git a/R/config.R b/R/config.R index f6a771123..675540eed 100644 --- a/R/config.R +++ b/R/config.R @@ -63,7 +63,7 @@ use_precommit_config <- function(config_source = getOption("precommit.config_sou "All available hooks: ", "https://pre-commit.com/hooks.html", "R specific hooks:", - "https://github.com/lorenzwalthert/precommit." + hooks_repo, "." )) config_source } diff --git a/R/release.R b/R/release.R index 62d027be9..1496744a2 100644 --- a/R/release.R +++ b/R/release.R @@ -135,7 +135,7 @@ release_prechecks <- function(bump, is_cran) { update_rev_in_config <- function(new_version, path = "inst/pre-commit-config.yaml") { config <- readLines(path) - ours <- grep("- repo: https://github.com/lorenzwalthert/precommit", config, fixed = TRUE) + ours <- grep(paste0("- repo: ", hooks_repo), config, fixed = TRUE) others <- setdiff(grep("- repo:", config, fixed = TRUE), ours) next_after_ours <- others[others > ours][1] rev <- grep("rev:", config) From 9114150876c1f2aec4f3fe5f631afd76602e5c84 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Thu, 6 May 2021 17:27:25 +0200 Subject: [PATCH 02/36] assert dependencies --- DESCRIPTION | 2 +- NAMESPACE | 2 + NEWS.md | 7 +++ R/blur.R | 2 + R/roxygen2.R | 34 +++++++++++++ R/setup.R | 52 ++++++++++++++++++++ inst/WORDLIST | 2 + inst/bin/roxygenize | 5 +- man/diff_requires_run_roxygenize.Rd | 3 +- man/may_require_permanent_cache.Rd | 3 +- man/roxygen_assert_additinal_dependencies.Rd | 18 +++++++ man/snippet_generate.Rd | 24 +++++++++ tests/testthat/test-hook-roxygenize.R | 26 ++++++++++ tests/testthat/test-setup.R | 24 +++++++++ 14 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 R/blur.R create mode 100644 man/roxygen_assert_additinal_dependencies.Rd create mode 100644 man/snippet_generate.Rd create mode 100644 tests/testthat/test-setup.R diff --git a/DESCRIPTION b/DESCRIPTION index 3b43f61db..ae8ce4298 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: precommit Title: Pre-Commit Hooks -Version: 0.1.3.9000 +Version: 0.1.3.9001 Author: Lorenz Walthert Maintainer: Lorenz Walthert Description: Useful git hooks for R building on top of the multi-language diff --git a/NAMESPACE b/NAMESPACE index 311d27914..242ad02df 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,8 @@ export(open_config) export(open_wordlist) export(path_pre_commit_exec) export(path_precommit_exec) +export(roxygen_assert_additinal_dependencies) +export(snippet_generate) export(uninstall_precommit) export(update_precommit) export(use_precommit) diff --git a/NEWS.md b/NEWS.md index 843036831..c08bdecf3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -19,6 +19,13 @@ This requires `pre-commit >= 2.11.1`. All hooks and dependencies are now contained in a virtual environment with [`{renv}`](https://rstudio.github.io/renv/) (#233). +- Because hooks run in a virtual environment and the `roxygenize` hook runs + `pkgload::load_all()`, you need to list all dependencies of your package in + `additional_dependencies` field in `.pre-commit-config.yaml`. You will be + prompted to add them if they are missing, + `precommit::snippet_generate("additional-deps-roxygenize")` generates + the code you can copy/paste (). + **Minor changes** diff --git a/R/blur.R b/R/blur.R new file mode 100644 index 000000000..2316e4920 --- /dev/null +++ b/R/blur.R @@ -0,0 +1,2 @@ +utils::adist +rlang::is_installed diff --git a/R/roxygen2.R b/R/roxygen2.R index 76c17dd0a..6706b392a 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -73,3 +73,37 @@ diff_requires_run_roxygenize <- function(root = here::here()) { any(grep("function(", without_comments, fixed = TRUE)) } } + +#' Assert if all dependencies are installed +#' +#' This function is only exported for use in hook scripts, but it's not intended +#' to be called by the end-user directly. +#' @family hook script helpers +#' @export +roxygen_assert_additinal_dependencies <- function() { + out <- rlang::with_handlers( + # roxygen2 will load: https://github.com/r-lib/roxygen2/issues/771 + pkgload::load_all(quiet = TRUE), + error = function(e) { + e + } + ) + if (inherits(out, "packageNotFoundError")) { + # case used in package but not installed + rlang::abort(paste0( + "The roxygenize hook requires all used packages to be listed in ", + "the file `.pre-commit-config.yaml` under `id: roxygenize` -> ", + "`additional_dependencies:`, like this:\n\n", + " - id: roxygenize", + " + additional_dependencies: + - tidyr + - dplyr\n\n", + "Call ", + "`precommit::snippet_generate('additional-deps-roxygenize')`", + "and paste the ", + "output into the file `.pre-commit-config.yaml`.\n\n", + "Context: https://github.com/lorenzwalthert/precommit/issues/243" + )) + } +} diff --git a/R/setup.R b/R/setup.R index 6dcb10fba..f7e0fa639 100644 --- a/R/setup.R +++ b/R/setup.R @@ -91,3 +91,55 @@ upstream_repo_url_is_outdated <- function() { grepl("https://github.com/lorenzwalthert/pre-commit-hooks", ., fixed = TRUE) %>% any() } + +#' Generate code snippets +#' +#' Utility function to generate code snippets: +#' +#' @details +#' Currently supported: +#' +#' * additional-deps-roxygenize: Code to paste into +#' `.pre-commit-config.yaml` for the additional dependencies required by +#' roxygen2. +#' @param snippet Name of the snippet. +#' @inheritParams fallback_doc +#' @export +snippet_generate <- function(snippet = "", root = here::here()) { + rlang::arg_match(snippet, c("additional-deps-roxygenize")) + if (snippet == "additional-deps-roxygenize") { + rlang::inform( + "Generating snippet using installed versions of all dependencies.\n" + ) + deps <- desc::desc_get_deps() + paste0( + " - ", deps$package, "@", + purrr::map_chr(deps$package, ~ as.character(packageVersion(.x))), "\n", + collapse = "" + ) %>% + sort() %>% + cat(sep = "") + remote_deps <- rlang::with_handlers( + 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 +Remotes: + tidyverse/tidyr@2fd80d5 + +You need in your `.pre-commit-config.yaml` + + additional_dependencies: + - tidyverse/tidyr@2fd80d5 + " + )) + } + } +} diff --git a/inst/WORDLIST b/inst/WORDLIST index 31c752e62..b8308c473 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -40,6 +40,7 @@ gh github gitignore gsub +Homebrew href http https @@ -90,6 +91,7 @@ rds Rds readLines readme +recognised renv Renviron repo diff --git a/inst/bin/roxygenize b/inst/bin/roxygenize index ae114b7a9..3a357ce0e 100755 --- a/inst/bin/roxygenize +++ b/inst/bin/roxygenize @@ -22,13 +22,14 @@ Options: " -> doc arguments <- docopt::docopt(doc) -if (packageVersion("precommit") < "0.0.0.9039") { +if (packageVersion("precommit") < "0.1.3.90001") { rlang::abort(paste( - "This hooks only works with the R package {precommit} >= 0.0.0.9039", + "This hooks only works with the R package {precommit} >= 0.1.3.9001", 'Please upgrade with `remotes::install_github("lorenzwalthert/precommit")`.' )) } else { precommit::may_require_permanent_cache(arguments$no_warn_cache) + precommit::roxygen_assert_additinal_dependencies() } roxygenize_with_cache <- function(key, dirs) { diff --git a/man/diff_requires_run_roxygenize.Rd b/man/diff_requires_run_roxygenize.Rd index fe7cdc7a3..50e78e01c 100644 --- a/man/diff_requires_run_roxygenize.Rd +++ b/man/diff_requires_run_roxygenize.Rd @@ -25,6 +25,7 @@ diff_requires_run_roxygenize() } \seealso{ Other hook script helpers: -\code{\link{may_require_permanent_cache}()} +\code{\link{may_require_permanent_cache}()}, +\code{\link{roxygen_assert_additinal_dependencies}()} } \concept{hook script helpers} diff --git a/man/may_require_permanent_cache.Rd b/man/may_require_permanent_cache.Rd index 449f22c40..1d1bed0f4 100644 --- a/man/may_require_permanent_cache.Rd +++ b/man/may_require_permanent_cache.Rd @@ -17,6 +17,7 @@ to be called by the end-user directly. } \seealso{ Other hook script helpers: -\code{\link{diff_requires_run_roxygenize}()} +\code{\link{diff_requires_run_roxygenize}()}, +\code{\link{roxygen_assert_additinal_dependencies}()} } \concept{hook script helpers} diff --git a/man/roxygen_assert_additinal_dependencies.Rd b/man/roxygen_assert_additinal_dependencies.Rd new file mode 100644 index 000000000..3b13b4e36 --- /dev/null +++ b/man/roxygen_assert_additinal_dependencies.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen2.R +\name{roxygen_assert_additinal_dependencies} +\alias{roxygen_assert_additinal_dependencies} +\title{Assert if all dependencies are installed} +\usage{ +roxygen_assert_additinal_dependencies() +} +\description{ +This function is only exported for use in hook scripts, but it's not intended +to be called by the end-user directly. +} +\seealso{ +Other hook script helpers: +\code{\link{diff_requires_run_roxygenize}()}, +\code{\link{may_require_permanent_cache}()} +} +\concept{hook script helpers} diff --git a/man/snippet_generate.Rd b/man/snippet_generate.Rd new file mode 100644 index 000000000..36950f7ca --- /dev/null +++ b/man/snippet_generate.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/setup.R +\name{snippet_generate} +\alias{snippet_generate} +\title{Generate code snippets} +\usage{ +snippet_generate(snippet = "", root = here::here()) +} +\arguments{ +\item{snippet}{Name of the snippet.} + +\item{root}{The path to the root directory of your project.} +} +\description{ +Utility function to generate code snippets: +} +\details{ +Currently supported: +\itemize{ +\item additional-deps-roxygenize: Code to paste into +\code{.pre-commit-config.yaml} for the additional dependencies required by +roxygen2. +} +} diff --git a/tests/testthat/test-hook-roxygenize.R b/tests/testthat/test-hook-roxygenize.R index a75167f06..b910d22a3 100644 --- a/tests/testthat/test-hook-roxygenize.R +++ b/tests/testthat/test-hook-roxygenize.R @@ -74,3 +74,29 @@ test_that("change in formals alone triggers invalidation", { git2r::commit(".", "clear case 5") }) }) + + +test_that("asserting installed dependencies", { + withr::local_options("usethis.quiet" = TRUE) + withr::local_dir(withr::local_tempdir()) + pkg_name <- "testPkg3" + usethis::create_package(pkg_name) + withr::local_dir(pkg_name) + usethis:::proj_set(".") + installed <- c("pkgload", "rlang", "testthat") + purrr::walk(installed, usethis::use_package) + additional_pkg <- "blabdjfdf83928" + if (rlang::is_installed(additional_pkg)) { + rlang::abort(paste0( + "Package ", additional_pkg, + " is installed. This test cannot be ran" + )) + } + writeLines(c("utils::adist", "rlang::is_installed"), "R/blur.R") + testthat::expect_silent(roxygen_assert_additinal_dependencies()) + writeLines(paste0(additional_pkg, "::blu"), "R/core.R") + testthat::expect_error( + roxygen_assert_additinal_dependencies(), + "requires all used packages to be listed" + ) +}) diff --git a/tests/testthat/test-setup.R b/tests/testthat/test-setup.R new file mode 100644 index 000000000..a7516121a --- /dev/null +++ b/tests/testthat/test-setup.R @@ -0,0 +1,24 @@ +test_that("snippet generation works", { + withr::local_options("usethis.quiet" = TRUE) + withr::local_dir(withr::local_tempdir()) + pkg_name <- "testPkg2" + usethis::create_package(pkg_name) + withr::local_dir(pkg_name) + usethis:::proj_set(".") + usethis::use_package("styler") + expect_warning( + out <- capture_output(snippet_generate("additional-deps-roxygenize")), + NA + ) + expect_match( + out, "^ - styler@.+$", + ) + desc::desc_set("Remotes", "r-lib/styler") + expect_warning( + out <- capture_output(snippet_generate("additional-deps-roxygenize")), + "you have remote dependencies " + ) + expect_match( + out, "^ - styler@.+$", + ) +}) From 8a81aa601cc6aa9f7227ea5ae9ab81d9387790b7 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Thu, 6 May 2021 17:52:29 +0200 Subject: [PATCH 03/36] must have pkgload in roxygen hook, fix checks. allow pre-commit config and template to differ. The tempalte must correspond to the latest release, 'our' can be also the current master or any other. --- DESCRIPTION | 1 + inst/WORDLIST | 1 + inst/bin/roxygenize | 2 +- inst/consistent-release-tag | 3 +-- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ae8ce4298..054b36617 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,6 +30,7 @@ Suggests: glue, knitr, lintr, + pkgload, reticulate (>= 1.16), rmarkdown, roxygen2, diff --git a/inst/WORDLIST b/inst/WORDLIST index b8308c473..13624d0ec 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -78,6 +78,7 @@ params parsable pkgapi pkgdown +pkgload pre precommit precommithooks diff --git a/inst/bin/roxygenize b/inst/bin/roxygenize index 3a357ce0e..a6d57dfaf 100755 --- a/inst/bin/roxygenize +++ b/inst/bin/roxygenize @@ -22,7 +22,7 @@ Options: " -> doc arguments <- docopt::docopt(doc) -if (packageVersion("precommit") < "0.1.3.90001") { +if (packageVersion("precommit") < "0.1.3.9001") { rlang::abort(paste( "This hooks only works with the R package {precommit} >= 0.1.3.9001", 'Please upgrade with `remotes::install_github("lorenzwalthert/precommit")`.' diff --git a/inst/consistent-release-tag b/inst/consistent-release-tag index d1dde6ed7..f27f2c786 100755 --- a/inst/consistent-release-tag +++ b/inst/consistent-release-tag @@ -19,8 +19,7 @@ arguments <- docopt::docopt(doc) path_config <- c( fs::path("inst", "pre-commit-config-pkg.yaml"), - fs::path("inst", "pre-commit-config-proj.yaml"), - if (!arguments$release_mode) fs::path(".pre-commit-config.yaml") + fs::path("inst", "pre-commit-config-proj.yaml") ) From cdffc485ef9164f8fe80e40a846d03c005fe807a Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Thu, 6 May 2021 18:13:41 +0200 Subject: [PATCH 04/36] add version requirement --- R/roxygen2.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/roxygen2.R b/R/roxygen2.R index 6706b392a..894e4b5b5 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -91,7 +91,7 @@ roxygen_assert_additinal_dependencies <- function() { if (inherits(out, "packageNotFoundError")) { # case used in package but not installed rlang::abort(paste0( - "The roxygenize hook requires all used packages to be listed in ", + "The roxygenize hook requires all dependencies of your package to be listed in ", "the file `.pre-commit-config.yaml` under `id: roxygenize` -> ", "`additional_dependencies:`, like this:\n\n", " - id: roxygenize", @@ -102,7 +102,8 @@ roxygen_assert_additinal_dependencies <- function() { "Call ", "`precommit::snippet_generate('additional-deps-roxygenize')`", "and paste the ", - "output into the file `.pre-commit-config.yaml`.\n\n", + "output into the file `.pre-commit-config.yaml`. This requires precommit", + " > 0.1.3.\n\n", "Context: https://github.com/lorenzwalthert/precommit/issues/243" )) } From a7c808ffb3a512df033d8df70a896791d818394f Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Thu, 6 May 2021 18:22:45 +0200 Subject: [PATCH 05/36] explicitly rely on DESCRIPTION --- R/roxygen2.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/roxygen2.R b/R/roxygen2.R index 894e4b5b5..afc84222e 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -103,8 +103,8 @@ roxygen_assert_additinal_dependencies <- function() { "`precommit::snippet_generate('additional-deps-roxygenize')`", "and paste the ", "output into the file `.pre-commit-config.yaml`. This requires precommit", - " > 0.1.3.\n\n", - "Context: https://github.com/lorenzwalthert/precommit/issues/243" + " > 0.1.3 and assumes you declared all dependencies in `DESCRIPTION`.", + "\n\nContext: https://github.com/lorenzwalthert/precommit/issues/243" )) } } From 6de8fff822d4b7c38d7cb139a3acec6af0af8a37 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Thu, 6 May 2021 18:44:44 +0200 Subject: [PATCH 06/36] better explanation --- NEWS.md | 9 ++++++++- R/roxygen2.R | 9 +++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index c08bdecf3..8fc419502 100644 --- a/NEWS.md +++ b/NEWS.md @@ -18,7 +18,14 @@ `language: script` from the [pre-commit framework](https://pre-commit.com). This requires `pre-commit >= 2.11.1`. All hooks and dependencies are now contained in a virtual environment with [`{renv}`](https://rstudio.github.io/renv/) - (#233). + (#233). Thanks to {renv}'s excellent + [caching](https://rstudio.github.io/renv/articles/renv.html#cache-1), this + does not consume much space and is fast. This makes output + of hooks more consistent across different local setups, make manual dependency + management redundant and will facilitate running R hooks as part of CI/CD, + e.g. https://pre-commit.ci or [GitHub Actions](https://github.com/pre-commit/action) + along with arbitrary other hooks. + - Because hooks run in a virtual environment and the `roxygenize` hook runs `pkgload::load_all()`, you need to list all dependencies of your package in `additional_dependencies` field in `.pre-commit-config.yaml`. You will be diff --git a/R/roxygen2.R b/R/roxygen2.R index afc84222e..cc655b3f1 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -91,7 +91,7 @@ roxygen_assert_additinal_dependencies <- function() { if (inherits(out, "packageNotFoundError")) { # case used in package but not installed rlang::abort(paste0( - "The roxygenize hook requires all dependencies of your package to be listed in ", + "The roxygenize hook requires all* dependencies of your package to be listed in ", "the file `.pre-commit-config.yaml` under `id: roxygenize` -> ", "`additional_dependencies:`, like this:\n\n", " - id: roxygenize", @@ -104,7 +104,12 @@ roxygen_assert_additinal_dependencies <- function() { "and paste the ", "output into the file `.pre-commit-config.yaml`. This requires precommit", " > 0.1.3 and assumes you declared all dependencies in `DESCRIPTION`.", - "\n\nContext: https://github.com/lorenzwalthert/precommit/issues/243" + "\n\nContext: https://github.com/lorenzwalthert/precommit/issues/243", + "\n\nThe initial error (from `pkgload::load_all()`) was: ", + conditionMessage(out), ".\n\n===================================\n", + "*Some packages are already installed in the renv to run the hook, so ", + "these technically don't have to be listed as additional dependencies, ", + "but we recommend listing all for simplicity and consistency." )) } } From de7c2282fe3d92bd5cf2b39172a20e9c7d8fb85a Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 00:32:08 +0200 Subject: [PATCH 07/36] move roxygenize_with_cache to pkg source. Check if pkapi and other non-dependencies are available --- NAMESPACE | 2 ++ R/roxygen2.R | 30 ++++++++++++++++++++ R/utils.R | 13 +++++++++ inst/bin/roxygenize | 26 ++++++----------- man/diff_requires_run_roxygenize.Rd | 4 ++- man/dirs_R.cache.Rd | 24 ++++++++++++++++ man/may_require_permanent_cache.Rd | 4 ++- man/roxygen_assert_additinal_dependencies.Rd | 4 ++- man/roxygenize_with_cache.Rd | 20 +++++++++++++ 9 files changed, 107 insertions(+), 20 deletions(-) create mode 100644 man/dirs_R.cache.Rd create mode 100644 man/roxygenize_with_cache.Rd diff --git a/NAMESPACE b/NAMESPACE index 242ad02df..1253cd3e2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(autoupdate) export(diff_requires_run_roxygenize) +export(dirs_R.cache) export(install_precommit) export(may_require_permanent_cache) export(open_config) @@ -9,6 +10,7 @@ export(open_wordlist) export(path_pre_commit_exec) export(path_precommit_exec) export(roxygen_assert_additinal_dependencies) +export(roxygenize_with_cache) export(snippet_generate) export(uninstall_precommit) export(update_precommit) diff --git a/R/roxygen2.R b/R/roxygen2.R index cc655b3f1..ebae567b8 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -113,3 +113,33 @@ roxygen_assert_additinal_dependencies <- function() { )) } } + +#' Roxygen depending on cache state +#' +#' This function is only exported for use in hook scripts, but it's not intended +#' to be called by the end-user directly. +#' @family hook script helpers +#' @export +roxygenize_with_cache <- function(key, dirs) { + if (diff_requires_run_roxygenize()) { + out <- rlang::with_handlers( + roxygen2::roxygenise(), + error = function(e) e + ) + if (inherits(out, "packageNotFoundError")) { + rlang::abort(paste0( + conditionMessage(out), + ". Please add the package as a dependency to ", + "`.pre-commit-config.yaml` -> `id: roxygenize` -> ", + "`additional_dependencies` and try again. The package must be ", + "specified so `renv::install()` understands it, e.g. like this:\n\n", + " - id: roxygenize", + " + additional_dependencies: + - r-lib/pkgapi + - dplyr@1.0.0\n\n" + )) + } + R.cache::saveCache(object = Sys.time(), key = key, dirs = dirs) + } +} diff --git a/R/utils.R b/R/utils.R index 0d3884970..3f57cae33 100644 --- a/R/utils.R +++ b/R/utils.R @@ -37,3 +37,16 @@ is_package <- function(root = here::here()) { add_trailing_linebreak <- function(x) { paste0(x, "\n") } + + +#' Create the path to the precommit R.cache cache +#' +#' This function is only exported for use in hook scripts, but it's not intended +#' to be called by the end-user directly. +#' @param hook_id The id of the hook for which we want the relative cache +#' directory. +#' @family hook script helpers +#' @export +dirs_R.cache <- function(hook_id) { + file.path("precommit", hook_id) +} diff --git a/inst/bin/roxygenize b/inst/bin/roxygenize index a6d57dfaf..3836ef72e 100755 --- a/inst/bin/roxygenize +++ b/inst/bin/roxygenize @@ -2,15 +2,15 @@ "Run roxygen2::roxygenize() -Obviously, this hook is only activated when any staged file passes the filter +Obviously, this hook is only activated when any staged file passes the filter specified in .pre-commit-hooks.yaml. Then, we check the time stamp of the last time we ran the hook. If any of our R files is younger than that, we consider running the hook. We next use {git2r} to inspect the cached diff of all .R files -in the R/ directory, and not the files passed to this hook. If we find any -roxygen2 comment in the diff, we run `roxygen2::roxygenize(). The preliminary -use case for this is when we previously attempted to commit but check failed, so -on the second try without any other files changed, it will succeed. -This check should run *after* check that modify the files that are passed to +in the R/ directory, and not the files passed to this hook. If we find any +roxygen2 comment in the diff, we run `roxygen2::roxygenize(). The preliminary +use case for this is when we previously attempted to commit but check failed, so +on the second try without any other files changed, it will succeed. +This check should run *after* check that modify the files that are passed to them (like styler) because they will never modify their input .R files. Usage: @@ -32,15 +32,7 @@ if (packageVersion("precommit") < "0.1.3.9001") { precommit::roxygen_assert_additinal_dependencies() } -roxygenize_with_cache <- function(key, dirs) { - if (precommit::diff_requires_run_roxygenize()) { - roxygen2::roxygenise() - saveCache(object = Sys.time(), key = key, dirs = dirs) - } -} - -library("R.cache") -path_relative_cache <- file.path("precommit", "roxygenize") +path_relative_cache <- precommit::dirs_R.cache('roxygenize') wd <- list(getwd()) cache <- loadCache(key = wd, dirs = path_relative_cache) @@ -52,8 +44,8 @@ if (!is.null(cache)) { all_files <- file.info(candidates) last_modified <- max(all_files$mtime) if (last_modified > cache[[1]]) { - roxygenize_with_cache(key = wd, dirs = path_relative_cache) + precommit::roxygenize_with_cache(key = wd, dirs = path_relative_cache) } } else { - roxygenize_with_cache(key = wd, dirs = path_relative_cache) + precommit::roxygenize_with_cache(key = wd, dirs = path_relative_cache) } diff --git a/man/diff_requires_run_roxygenize.Rd b/man/diff_requires_run_roxygenize.Rd index 50e78e01c..04d3411fd 100644 --- a/man/diff_requires_run_roxygenize.Rd +++ b/man/diff_requires_run_roxygenize.Rd @@ -25,7 +25,9 @@ diff_requires_run_roxygenize() } \seealso{ Other hook script helpers: +\code{\link{dirs_R.cache}()}, \code{\link{may_require_permanent_cache}()}, -\code{\link{roxygen_assert_additinal_dependencies}()} +\code{\link{roxygen_assert_additinal_dependencies}()}, +\code{\link{roxygenize_with_cache}()} } \concept{hook script helpers} diff --git a/man/dirs_R.cache.Rd b/man/dirs_R.cache.Rd new file mode 100644 index 000000000..cb9b4f867 --- /dev/null +++ b/man/dirs_R.cache.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{dirs_R.cache} +\alias{dirs_R.cache} +\title{Create the path to the precommit R.cache cache} +\usage{ +dirs_R.cache(hook_id) +} +\arguments{ +\item{hook_id}{The id of the hook for which we want the relative cache +directory.} +} +\description{ +This function is only exported for use in hook scripts, but it's not intended +to be called by the end-user directly. +} +\seealso{ +Other hook script helpers: +\code{\link{diff_requires_run_roxygenize}()}, +\code{\link{may_require_permanent_cache}()}, +\code{\link{roxygen_assert_additinal_dependencies}()}, +\code{\link{roxygenize_with_cache}()} +} +\concept{hook script helpers} diff --git a/man/may_require_permanent_cache.Rd b/man/may_require_permanent_cache.Rd index 1d1bed0f4..77478b229 100644 --- a/man/may_require_permanent_cache.Rd +++ b/man/may_require_permanent_cache.Rd @@ -18,6 +18,8 @@ to be called by the end-user directly. \seealso{ Other hook script helpers: \code{\link{diff_requires_run_roxygenize}()}, -\code{\link{roxygen_assert_additinal_dependencies}()} +\code{\link{dirs_R.cache}()}, +\code{\link{roxygen_assert_additinal_dependencies}()}, +\code{\link{roxygenize_with_cache}()} } \concept{hook script helpers} diff --git a/man/roxygen_assert_additinal_dependencies.Rd b/man/roxygen_assert_additinal_dependencies.Rd index 3b13b4e36..1f2fd32fc 100644 --- a/man/roxygen_assert_additinal_dependencies.Rd +++ b/man/roxygen_assert_additinal_dependencies.Rd @@ -13,6 +13,8 @@ to be called by the end-user directly. \seealso{ Other hook script helpers: \code{\link{diff_requires_run_roxygenize}()}, -\code{\link{may_require_permanent_cache}()} +\code{\link{dirs_R.cache}()}, +\code{\link{may_require_permanent_cache}()}, +\code{\link{roxygenize_with_cache}()} } \concept{hook script helpers} diff --git a/man/roxygenize_with_cache.Rd b/man/roxygenize_with_cache.Rd new file mode 100644 index 000000000..c9caf7678 --- /dev/null +++ b/man/roxygenize_with_cache.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/roxygen2.R +\name{roxygenize_with_cache} +\alias{roxygenize_with_cache} +\title{Roxygen depending on cache state} +\usage{ +roxygenize_with_cache(key, dirs) +} +\description{ +This function is only exported for use in hook scripts, but it's not intended +to be called by the end-user directly. +} +\seealso{ +Other hook script helpers: +\code{\link{diff_requires_run_roxygenize}()}, +\code{\link{dirs_R.cache}()}, +\code{\link{may_require_permanent_cache}()}, +\code{\link{roxygen_assert_additinal_dependencies}()} +} +\concept{hook script helpers} From 70cdf91dac1964fd817783220f172d32dc2964b6 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 00:53:24 +0200 Subject: [PATCH 08/36] bump version --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 054b36617..c0a4a238d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: precommit Title: Pre-Commit Hooks -Version: 0.1.3.9001 +Version: 0.1.3.9002 Author: Lorenz Walthert Maintainer: Lorenz Walthert Description: Useful git hooks for R building on top of the multi-language From 07429721779500ddabc9a809f9a1e9a773355f3d Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 08:41:29 +0200 Subject: [PATCH 09/36] needs R.cache --- inst/bin/roxygenize | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/bin/roxygenize b/inst/bin/roxygenize index 3836ef72e..f104a9d3a 100755 --- a/inst/bin/roxygenize +++ b/inst/bin/roxygenize @@ -34,7 +34,7 @@ if (packageVersion("precommit") < "0.1.3.9001") { path_relative_cache <- precommit::dirs_R.cache('roxygenize') wd <- list(getwd()) -cache <- loadCache(key = wd, dirs = path_relative_cache) +cache <- R.cache::loadCache(key = wd, dirs = path_relative_cache) if (!is.null(cache)) { candidates <- intersect( From f55a3ffe433e4c59686686cc9214eb89eb3989ad Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 08:49:53 +0200 Subject: [PATCH 10/36] add test setup --- .pre-commit-config.yaml | 2 +- DESCRIPTION | 1 + R/release.R | 4 +-- R/testing.R | 17 ++++++++++--- man/local_test_setup.Rd | 9 ++++--- man/release_gh.Rd | 4 +-- tests/testthat/test-hook-roxygenize.R | 35 ++++++++++++++++++++++----- tests/testthat/test-setup.R | 7 +----- 8 files changed, 54 insertions(+), 25 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bb4035eed..7d77389d5 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ default_stages: ["commit"] repos: - repo: https://github.com/lorenzwalthert/precommit - rev: acc8657498d5bfb9e9891098ba00b36e82c7ebd6 + rev: 07429721779500ddabc9a809f9a1e9a773355f3d hooks: - id: style-files args: [--style_pkg=styler, --style_fun=tidyverse_style] diff --git a/DESCRIPTION b/DESCRIPTION index c0a4a238d..99e439c66 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,6 +30,7 @@ Suggests: glue, knitr, lintr, + mockery, pkgload, reticulate (>= 1.16), rmarkdown, diff --git a/R/release.R b/R/release.R index 1496744a2..3364aeb11 100644 --- a/R/release.R +++ b/R/release.R @@ -9,8 +9,8 @@ #' - update default config in inst/ #' - commit #' - git tag -#' - run `inst/consistent-release-tag` hook with --release-mode (passing args to hooks -#' not possible interactively, hence we run in advance). +#' - run `inst/consistent-release-tag` hook with --release-mode (passing args to +#' hooks not possible interactively, hence we run in advance). #' - commit and push with skipping `inst/consistent-release-tag`. #' - autoupdate own config file #' - bump description with dev diff --git a/R/testing.R b/R/testing.R index 351213ceb..0f2a96354 100644 --- a/R/testing.R +++ b/R/testing.R @@ -179,17 +179,26 @@ not_conda <- function() { #' @param git Whether or not to init git in the local directory. #' @param use_precommmit Whether or not to [use_precommit()]. #' @keywords internal -local_test_setup <- function(.local_envir = parent.frame(), - git = TRUE, +local_test_setup <- function(git = TRUE, use_precommit = FALSE, - ...) { + package = FALSE, + ..., + .local_envir = parent.frame()) { dir <- withr::local_tempdir(.local_envir = .local_envir) + withr::local_dir(dir, .local_envir = .local_envir) + withr::local_options("usethis.quiet" = TRUE) if (git) { - git2r::init(path = dir) + git2r::init() withr::defer(fs::dir_delete(fs::path(dir, ".git")), envir = .local_envir) } if (use_precommit) { suppressMessages(use_precommit(...)) } + if (package) { + usethis::create_package(dir) + withr::local_dir(dir) + usethis::proj_set(dir) + } + dir } diff --git a/man/local_test_setup.Rd b/man/local_test_setup.Rd index 12861a270..15d9a1bd0 100644 --- a/man/local_test_setup.Rd +++ b/man/local_test_setup.Rd @@ -5,17 +5,18 @@ \title{Testing utilities} \usage{ local_test_setup( - .local_envir = parent.frame(), git = TRUE, use_precommit = FALSE, - ... + package = FALSE, + ..., + .local_envir = parent.frame() ) } \arguments{ -\item{.local_envir}{\verb{[environment]}\cr The environment to use for scoping.} - \item{git}{Whether or not to init git in the local directory.} +\item{.local_envir}{\verb{[environment]}\cr The environment to use for scoping.} + \item{use_precommmit}{Whether or not to \code{\link[=use_precommit]{use_precommit()}}.} } \description{ diff --git a/man/release_gh.Rd b/man/release_gh.Rd index 570055800..0eeadd814 100644 --- a/man/release_gh.Rd +++ b/man/release_gh.Rd @@ -21,8 +21,8 @@ This function does the following: \item update default config in inst/ \item commit \item git tag -\item run \code{inst/consistent-release-tag} hook with --release-mode (passing args to hooks -not possible interactively, hence we run in advance). +\item run \code{inst/consistent-release-tag} hook with --release-mode (passing args to +hooks not possible interactively, hence we run in advance). \item commit and push with skipping \code{inst/consistent-release-tag}. \item autoupdate own config file \item bump description with dev diff --git a/tests/testthat/test-hook-roxygenize.R b/tests/testthat/test-hook-roxygenize.R index b910d22a3..20e44a2b0 100644 --- a/tests/testthat/test-hook-roxygenize.R +++ b/tests/testthat/test-hook-roxygenize.R @@ -78,11 +78,7 @@ test_that("change in formals alone triggers invalidation", { test_that("asserting installed dependencies", { withr::local_options("usethis.quiet" = TRUE) - withr::local_dir(withr::local_tempdir()) - pkg_name <- "testPkg3" - usethis::create_package(pkg_name) - withr::local_dir(pkg_name) - usethis:::proj_set(".") + local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) installed <- c("pkgload", "rlang", "testthat") purrr::walk(installed, usethis::use_package) additional_pkg <- "blabdjfdf83928" @@ -97,6 +93,33 @@ test_that("asserting installed dependencies", { writeLines(paste0(additional_pkg, "::blu"), "R/core.R") testthat::expect_error( roxygen_assert_additinal_dependencies(), - "requires all used packages to be listed" + "requires all\\* dependencies of your package" + ) +}) + +test_that("roxygenize with cache gives helpful error", { + local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) + writeLines(c("#' This is a title", "#'", "#' More", "#' @name test", "NULL"), "R/blur.R") + mockery::stub(roxygenize_with_cache, "diff_requires_run_roxygenize", TRUE) + expect_output( + roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")), + "test.R" + ) + # add pkg error + additional_pkg <- "blabdjfdf83928" + if (rlang::is_installed(additional_pkg)) { + rlang::abort(paste0( + "Package ", additional_pkg, + " is installed. This test cannot be ran" + )) + } + roxygen_field <- paste0( + 'list(markdown = TRUE, roclets = c("rd", "namespace", "collate", "', + additional_pkg, '::api_roclet"))' + ) + desc::desc_set(Roxygen = roxygen_field) + expect_error( + roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")), + "Please add the package as a dependency" ) }) diff --git a/tests/testthat/test-setup.R b/tests/testthat/test-setup.R index a7516121a..653f79e42 100644 --- a/tests/testthat/test-setup.R +++ b/tests/testthat/test-setup.R @@ -1,10 +1,5 @@ test_that("snippet generation works", { - withr::local_options("usethis.quiet" = TRUE) - withr::local_dir(withr::local_tempdir()) - pkg_name <- "testPkg2" - usethis::create_package(pkg_name) - withr::local_dir(pkg_name) - usethis:::proj_set(".") + local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) usethis::use_package("styler") expect_warning( out <- capture_output(snippet_generate("additional-deps-roxygenize")), From fe016f815c5d2be6fe2c7f6de379ebbd56ea6c34 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 09:04:02 +0200 Subject: [PATCH 11/36] trigger roxygenize --- R/roxygen2.R | 4 ++-- man/extract_diff_root.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/roxygen2.R b/R/roxygen2.R index ebae567b8..ddee893c4 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -20,8 +20,8 @@ extract_diff_files <- function(files) { #' Extract old and new lines from `git diff --cached` #' -#' This is useful to detect within a hook script if the core function -#' from a hook such as [roxygen2::roxygenize()] must run at all or not. +#' This is useful to detect within a hook script if the core function from a +#' hook such as [roxygen2::roxygenize()] must run at all or not. #' @param root The root of project. #' @keywords internal extract_diff_root <- function(root = here::here()) { diff --git a/man/extract_diff_root.Rd b/man/extract_diff_root.Rd index 1a825e428..c9138fe81 100644 --- a/man/extract_diff_root.Rd +++ b/man/extract_diff_root.Rd @@ -10,7 +10,7 @@ extract_diff_root(root = here::here()) \item{root}{The root of project.} } \description{ -This is useful to detect within a hook script if the core function -from a hook such as \code{\link[roxygen2:roxygenize]{roxygen2::roxygenize()}} must run at all or not. +This is useful to detect within a hook script if the core function from a +hook such as \code{\link[roxygen2:roxygenize]{roxygen2::roxygenize()}} must run at all or not. } \keyword{internal} From e92ef5963bc013958816a061f2bc21d838d230b6 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 09:50:10 +0200 Subject: [PATCH 12/36] make sure you also show other errors and warnings --- DESCRIPTION | 1 + R/roxygen2.R | 4 ++ R/testing.R | 21 ++++++++++ man/generate_uninstalled_pkg_name.Rd | 15 ++++++++ tests/testthat/test-hook-roxygenize.R | 55 ++++++++++++++++++--------- 5 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 man/generate_uninstalled_pkg_name.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 99e439c66..5f9221989 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,6 +26,7 @@ Imports: yaml Suggests: desc, + digest, git2r, glue, knitr, diff --git a/R/roxygen2.R b/R/roxygen2.R index ddee893c4..0ea0a5387 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -139,6 +139,10 @@ roxygenize_with_cache <- function(key, dirs) { - r-lib/pkgapi - dplyr@1.0.0\n\n" )) + } else if (inherits(out, "error")) { + rlang::abort(conditionMessage(out)) + } else if (inherits(out, "warning")) { + rlang::warn(conditionMessage(out)) } R.cache::saveCache(object = Sys.time(), key = key, dirs = dirs) } diff --git a/R/testing.R b/R/testing.R index 0f2a96354..51935565b 100644 --- a/R/testing.R +++ b/R/testing.R @@ -198,7 +198,28 @@ local_test_setup <- function(git = TRUE, usethis::create_package(dir) withr::local_dir(dir) usethis::proj_set(dir) + usethis::use_testthat() } dir } + +#' Generate a random package name that is not installed +#' @param n The number of times we should try +#' @keywords internal +generate_uninstalled_pkg_name <- function(n = 10) { + additional_pkg <- paste0("package", digest::digest(Sys.time())) + if (rlang::is_installed(additional_pkg)) { + if (n > 0) { + generate_uninstalled_pkg_name(n - 1) + } else { + rlang::abort("could not find a package name that was not yet installed") + } + } else { + additional_pkg + } +} + +generate_uninstalled_pkg_call <- function(n = 10) { + paste0(generate_uninstalled_pkg_name(n), "::x") +} diff --git a/man/generate_uninstalled_pkg_name.Rd b/man/generate_uninstalled_pkg_name.Rd new file mode 100644 index 000000000..da82e4dc1 --- /dev/null +++ b/man/generate_uninstalled_pkg_name.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/testing.R +\name{generate_uninstalled_pkg_name} +\alias{generate_uninstalled_pkg_name} +\title{Generate a random package name that is not installed} +\usage{ +generate_uninstalled_pkg_name(n = 10) +} +\arguments{ +\item{n}{The number of times we should try} +} +\description{ +Generate a random package name that is not installed +} +\keyword{internal} diff --git a/tests/testthat/test-hook-roxygenize.R b/tests/testthat/test-hook-roxygenize.R index 20e44a2b0..710885242 100644 --- a/tests/testthat/test-hook-roxygenize.R +++ b/tests/testthat/test-hook-roxygenize.R @@ -81,41 +81,36 @@ test_that("asserting installed dependencies", { local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) installed <- c("pkgload", "rlang", "testthat") purrr::walk(installed, usethis::use_package) - additional_pkg <- "blabdjfdf83928" - if (rlang::is_installed(additional_pkg)) { - rlang::abort(paste0( - "Package ", additional_pkg, - " is installed. This test cannot be ran" - )) - } writeLines(c("utils::adist", "rlang::is_installed"), "R/blur.R") testthat::expect_silent(roxygen_assert_additinal_dependencies()) - writeLines(paste0(additional_pkg, "::blu"), "R/core.R") + writeLines(paste0(generate_uninstalled_pkg_call(), "R/core.R")) testthat::expect_error( roxygen_assert_additinal_dependencies(), "requires all\\* dependencies of your package" ) }) -test_that("roxygenize with cache gives helpful error", { +test_that("roxygenize works in general", { local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) writeLines(c("#' This is a title", "#'", "#' More", "#' @name test", "NULL"), "R/blur.R") + # works mockery::stub(roxygenize_with_cache, "diff_requires_run_roxygenize", TRUE) expect_output( roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")), "test.R" ) - # add pkg error - additional_pkg <- "blabdjfdf83928" - if (rlang::is_installed(additional_pkg)) { - rlang::abort(paste0( - "Package ", additional_pkg, - " is installed. This test cannot be ran" - )) - } +}) + + +test_that("fails when package is called but not installed", { + local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) + writeLines(c("NULL"), "R/blur.R") + # works + mockery::stub(roxygenize_with_cache, "diff_requires_run_roxygenize", TRUE) + # when there is a missing package roxygen_field <- paste0( 'list(markdown = TRUE, roclets = c("rd", "namespace", "collate", "', - additional_pkg, '::api_roclet"))' + generate_uninstalled_pkg_call(), '"))' ) desc::desc_set(Roxygen = roxygen_field) expect_error( @@ -123,3 +118,27 @@ test_that("roxygenize with cache gives helpful error", { "Please add the package as a dependency" ) }) + +test_that("fails when there is invalid code", { + local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) + mockery::stub(roxygenize_with_cache, "diff_requires_run_roxygenize", TRUE) + # when there is a missing package + writeLines(c("invalid code stuff /3kj"), "R/more.R") + expect_error( + roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")), + "[Uu]nexpected symbol" + ) +}) + +test_that("warns if there is any other warning", { + local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) + mockery::stub(roxygenize_with_cache, "diff_requires_run_roxygenize", TRUE) + writeLines(c("#' This is a title", "#'", "#' More", "NULL"), "R/blur.R") + + expect_warning( + roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")), + "Missing name" + ) +}) + +# todo other warning: # e.g. when there is a package listed that is not in DESCRIPTION From 4ebb74d45b87c8053a5da40415a913d71fe2ece4 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 09:59:06 +0200 Subject: [PATCH 13/36] fix tests --- tests/testthat/test-hook-roxygenize.R | 3 +-- tests/testthat/test-setup.R | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-hook-roxygenize.R b/tests/testthat/test-hook-roxygenize.R index 710885242..309055248 100644 --- a/tests/testthat/test-hook-roxygenize.R +++ b/tests/testthat/test-hook-roxygenize.R @@ -77,13 +77,12 @@ test_that("change in formals alone triggers invalidation", { test_that("asserting installed dependencies", { - withr::local_options("usethis.quiet" = TRUE) local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) installed <- c("pkgload", "rlang", "testthat") purrr::walk(installed, usethis::use_package) writeLines(c("utils::adist", "rlang::is_installed"), "R/blur.R") testthat::expect_silent(roxygen_assert_additinal_dependencies()) - writeLines(paste0(generate_uninstalled_pkg_call(), "R/core.R")) + writeLines(generate_uninstalled_pkg_call(), "R/core.R") testthat::expect_error( roxygen_assert_additinal_dependencies(), "requires all\\* dependencies of your package" diff --git a/tests/testthat/test-setup.R b/tests/testthat/test-setup.R index 653f79e42..e34540da9 100644 --- a/tests/testthat/test-setup.R +++ b/tests/testthat/test-setup.R @@ -6,7 +6,7 @@ test_that("snippet generation works", { NA ) expect_match( - out, "^ - styler@.+$", + out, "^ - testthat@.+\n - styler@.+$", ) desc::desc_set("Remotes", "r-lib/styler") expect_warning( @@ -14,6 +14,6 @@ test_that("snippet generation works", { "you have remote dependencies " ) expect_match( - out, "^ - styler@.+$", + out, "^ - testthat@.+\n - styler@.+$", ) }) From a1c98d1af2d8033486b5a72386f0cd9c8c53c584 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 10:15:03 +0200 Subject: [PATCH 14/36] fix test --- R/testing.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/testing.R b/R/testing.R index 51935565b..29e32233a 100644 --- a/R/testing.R +++ b/R/testing.R @@ -186,13 +186,13 @@ local_test_setup <- function(git = TRUE, .local_envir = parent.frame()) { dir <- withr::local_tempdir(.local_envir = .local_envir) withr::local_dir(dir, .local_envir = .local_envir) - withr::local_options("usethis.quiet" = TRUE) + withr::local_options("usethis.quiet" = TRUE, .local_envir = .local_envir) if (git) { git2r::init() withr::defer(fs::dir_delete(fs::path(dir, ".git")), envir = .local_envir) } if (use_precommit) { - suppressMessages(use_precommit(...)) + suppressMessages(use_precommit(..., root = dir)) } if (package) { usethis::create_package(dir) From 4c1d65b31242042e20558abe7cf458c4d4e41452 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 11:24:44 +0200 Subject: [PATCH 15/36] try to fix tests --- R/testing.R | 5 ++++- tests/testthat/test-conda.R | 14 +++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/R/testing.R b/R/testing.R index 29e32233a..a8add87b4 100644 --- a/R/testing.R +++ b/R/testing.R @@ -182,11 +182,14 @@ not_conda <- function() { local_test_setup <- function(git = TRUE, use_precommit = FALSE, package = FALSE, + quiet = TRUE, ..., .local_envir = parent.frame()) { dir <- withr::local_tempdir(.local_envir = .local_envir) withr::local_dir(dir, .local_envir = .local_envir) - withr::local_options("usethis.quiet" = TRUE, .local_envir = .local_envir) + if (quiet) { + withr::local_options("usethis.quiet" = TRUE, .local_envir = .local_envir) + } if (git) { git2r::init() withr::defer(fs::dir_delete(fs::path(dir, ".git")), envir = .local_envir) diff --git a/tests/testthat/test-conda.R b/tests/testthat/test-conda.R index 9815ea232..dd589d3e4 100644 --- a/tests/testthat/test-conda.R +++ b/tests/testthat/test-conda.R @@ -6,7 +6,7 @@ if (!on_cran()) { }) test_that("can use pre-commit", { - tempdir <- local_test_setup() + tempdir <- local_test_setup(quiet = FALSE) expect_message( use_precommit(open = FALSE, force = TRUE, root = tempdir), "to get the latest" @@ -18,7 +18,7 @@ if (!on_cran()) { }) test_that("fails early if repo is not a git repo ", { - tempdir <- local_test_setup(git = FALSE) + tempdir <- local_test_setup(git = FALSE, quiet = FALSE) expect_error( use_precommit(open = FALSE, root = tempdir), @@ -45,7 +45,7 @@ if (!on_cran()) { }) test_that("existing hooks are recognized", { - tempdir <- local_test_setup() + tempdir <- local_test_setup(quiet = FALSE) withr::with_dir(tempdir, { git2r::init() usethis::proj_set(".") @@ -80,7 +80,7 @@ if (!on_cran()) { test_that("Can uninstall pre-commit (repo scope)", { # with all files there - tempdir <- local_test_setup(use_precommit = TRUE) + tempdir <- local_test_setup(use_precommit = TRUE, quiet = FALSE) expect_message( uninstall_precommit(scope = "repo", root = tempdir), "Uninstalled pre-commit from repo scope.*" @@ -104,7 +104,7 @@ if (!on_cran()) { test_that("Can uninstall (userly)", { if (not_conda()) { - tempdir <- local_test_setup(use_precommit = TRUE) + tempdir <- local_test_setup(use_precommit = TRUE, quiet = FALSE) expect_error( uninstall_precommit(scope = "user", ask = "none", root = tempdir), "installed with conda" @@ -131,7 +131,7 @@ if (!on_cran()) { if (!not_conda()) { expect_error(install_precommit(), NA) } - tempdir <- local_test_setup() + tempdir <- local_test_setup(quiet = FALSE) expect_message( use_precommit( example_remote_config(), @@ -145,7 +145,7 @@ if (!on_cran()) { if (!not_conda()) { expect_message(install_precommit(), "already installed") } - tempdir <- local_test_setup(use_precommit = FALSE) + tempdir <- local_test_setup(use_precommit = FALSE, quiet = FALSE) withr::with_dir( tempdir, { From afcdf604c832026c94386408fc85840e349f91bc Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 11:29:21 +0200 Subject: [PATCH 16/36] remove quiet --- R/testing.R | 1 - man/local_test_setup.Rd | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/R/testing.R b/R/testing.R index a8add87b4..7683f2c68 100644 --- a/R/testing.R +++ b/R/testing.R @@ -182,7 +182,6 @@ not_conda <- function() { local_test_setup <- function(git = TRUE, use_precommit = FALSE, package = FALSE, - quiet = TRUE, ..., .local_envir = parent.frame()) { dir <- withr::local_tempdir(.local_envir = .local_envir) diff --git a/man/local_test_setup.Rd b/man/local_test_setup.Rd index 15d9a1bd0..ecbe198f8 100644 --- a/man/local_test_setup.Rd +++ b/man/local_test_setup.Rd @@ -8,6 +8,7 @@ local_test_setup( git = TRUE, use_precommit = FALSE, package = FALSE, + quiet = TRUE, ..., .local_envir = parent.frame() ) From 5a4b83e72feac5f34f9d5dc67e55dfd500ee693c Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 11:30:08 +0200 Subject: [PATCH 17/36] add quiet --- R/testing.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/testing.R b/R/testing.R index 7683f2c68..a8add87b4 100644 --- a/R/testing.R +++ b/R/testing.R @@ -182,6 +182,7 @@ not_conda <- function() { local_test_setup <- function(git = TRUE, use_precommit = FALSE, package = FALSE, + quiet = TRUE, ..., .local_envir = parent.frame()) { dir <- withr::local_tempdir(.local_envir = .local_envir) From 4a5bd43f59d05ba09cad44a81b8ccb6a6702453f Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 11:34:10 +0200 Subject: [PATCH 18/36] dependencies should be sorted --- R/setup.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/setup.R b/R/setup.R index f7e0fa639..11793b52a 100644 --- a/R/setup.R +++ b/R/setup.R @@ -111,7 +111,8 @@ snippet_generate <- function(snippet = "", root = here::here()) { rlang::inform( "Generating snippet using installed versions of all dependencies.\n" ) - deps <- desc::desc_get_deps() + deps <- desc::desc_get_deps() %>% + sort() paste0( " - ", deps$package, "@", purrr::map_chr(deps$package, ~ as.character(packageVersion(.x))), "\n", From 487daf4dcd68dad8e2b76516cfa467d2d17e9087 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 11:38:59 +0200 Subject: [PATCH 19/36] order should be fix --- R/setup.R | 4 ++-- tests/testthat/test-setup.R | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/setup.R b/R/setup.R index 11793b52a..acd1b6da6 100644 --- a/R/setup.R +++ b/R/setup.R @@ -111,8 +111,8 @@ snippet_generate <- function(snippet = "", root = here::here()) { rlang::inform( "Generating snippet using installed versions of all dependencies.\n" ) - deps <- desc::desc_get_deps() %>% - sort() + deps <- desc::desc_get_deps() + deps <- deps[order(deps$package), ] paste0( " - ", deps$package, "@", purrr::map_chr(deps$package, ~ as.character(packageVersion(.x))), "\n", diff --git a/tests/testthat/test-setup.R b/tests/testthat/test-setup.R index e34540da9..5d99525e6 100644 --- a/tests/testthat/test-setup.R +++ b/tests/testthat/test-setup.R @@ -6,7 +6,7 @@ test_that("snippet generation works", { NA ) expect_match( - out, "^ - testthat@.+\n - styler@.+$", + out, "^ - styler@.+\n - testthat@.+$", ) desc::desc_set("Remotes", "r-lib/styler") expect_warning( @@ -14,6 +14,6 @@ test_that("snippet generation works", { "you have remote dependencies " ) expect_match( - out, "^ - testthat@.+\n - styler@.+$", + out, "^ - styler@.+\n - testthat@.+$", ) }) From 0be73c590eb549d094e32a9600cb58193ecedcc3 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 11:41:32 +0200 Subject: [PATCH 20/36] dont need styler dev anymore --- .pre-commit-config.yaml | 27 +++++++++++++++++++++++++++ DESCRIPTION | 4 +--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7d77389d5..8e13cb56b 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,6 +13,33 @@ repos: renv/.* )$ - id: roxygenize + additional_dependencies: + - desc@1.3.0 + - digest@0.6.27 + - docopt@0.7.1 + - fs@1.5.0 + - git2r@0.28.0 + - glue@1.4.2 + - here@1.0.1 + - knitr@1.33 + - lintr@2.0.1 + - magrittr@2.0.1 + - mockery@0.4.2 + - pkgload@1.2.1 + - purrr@0.3.4 + - R.cache@0.15.0 + - reticulate@1.20 + - rlang@0.4.11 + - rmarkdown@2.8 + - roxygen2@7.1.1 + - rprojroot@2.0.2 + - rstudioapi@0.13 + - spelling@2.2 + - styler@1.4.1 + - testthat@3.0.2 + - usethis@2.0.1 + - withr@2.4.2 + - yaml@2.2.1 # codemeta must be above use-tidy-description when both are used # - id: codemeta-description-updated - id: use-tidy-description diff --git a/DESCRIPTION b/DESCRIPTION index 5f9221989..92e425d80 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Imports: rlang, rprojroot, rstudioapi, + styler, usethis (>= 2.0.0), withr, yaml @@ -37,12 +38,9 @@ Suggests: rmarkdown, roxygen2, spelling, - styler (>= 1.3.2.9000), testthat (>= 2.1.0) VignetteBuilder: knitr -Remotes: - r-lib/styler Encoding: UTF-8 Roxygen: list(markdown = TRUE, roclets = c( "rd", "namespace", "collate", if (rlang::is_installed("pkgapi")) "pkgapi::api_roclet" else { From f0905120d876115bf11c030e5e310c9a771b32dd Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 11:43:36 +0200 Subject: [PATCH 21/36] trigger roxygen --- R/config.R | 4 ++-- man/use_precommit.Rd | 4 ++-- man/use_precommit_config.Rd | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/config.R b/R/config.R index 675540eed..eabedd4aa 100644 --- a/R/config.R +++ b/R/config.R @@ -15,8 +15,8 @@ #' pre-commit with [use_precommit()] using the argument `config_source` to #' copy an existing config file into your repo. This argument defaults to the R #' option `precommit.config_source`, so you may want to set this option in -#' your `.Rprofile` for convenience. Note that this is **not** equivalent -#' to the `--config` option in the CLI command `pre-commit install` and similar, +#' your `.Rprofile` for convenience. Note that this is **not** equivalent to the +#' `--config` option in the CLI command `pre-commit install` and similar, #' which do *not* copy a config file into a project root (and allow to put it #' under version control), but rather link it in some more or less transparent #' way. diff --git a/man/use_precommit.Rd b/man/use_precommit.Rd index 666137da4..a7cdac1b0 100644 --- a/man/use_precommit.Rd +++ b/man/use_precommit.Rd @@ -72,8 +72,8 @@ You can use an existing \code{.pre-commit-config.yaml} file when initializing pre-commit with \code{\link[=use_precommit]{use_precommit()}} using the argument \code{config_source} to copy an existing config file into your repo. This argument defaults to the R option \code{precommit.config_source}, so you may want to set this option in -your \code{.Rprofile} for convenience. Note that this is \strong{not} equivalent -to the \code{--config} option in the CLI command \verb{pre-commit install} and similar, +your \code{.Rprofile} for convenience. Note that this is \strong{not} equivalent to the +\code{--config} option in the CLI command \verb{pre-commit install} and similar, which do \emph{not} copy a config file into a project root (and allow to put it under version control), but rather link it in some more or less transparent way. diff --git a/man/use_precommit_config.Rd b/man/use_precommit_config.Rd index 6baf6cb2e..003c26d91 100644 --- a/man/use_precommit_config.Rd +++ b/man/use_precommit_config.Rd @@ -41,8 +41,8 @@ You can use an existing \code{.pre-commit-config.yaml} file when initializing pre-commit with \code{\link[=use_precommit]{use_precommit()}} using the argument \code{config_source} to copy an existing config file into your repo. This argument defaults to the R option \code{precommit.config_source}, so you may want to set this option in -your \code{.Rprofile} for convenience. Note that this is \strong{not} equivalent -to the \code{--config} option in the CLI command \verb{pre-commit install} and similar, +your \code{.Rprofile} for convenience. Note that this is \strong{not} equivalent to the +\code{--config} option in the CLI command \verb{pre-commit install} and similar, which do \emph{not} copy a config file into a project root (and allow to put it under version control), but rather link it in some more or less transparent way. From 8486aa04cbdf37feb6652a7bd235280533e03af7 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 11:47:06 +0200 Subject: [PATCH 22/36] inherit params --- .pre-commit-config.yaml | 1 + R/roxygen2.R | 1 + man/roxygenize_with_cache.Rd | 9 +++++++++ 3 files changed, 11 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8e13cb56b..6550ae143 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,6 +31,7 @@ repos: - reticulate@1.20 - rlang@0.4.11 - rmarkdown@2.8 + - r-lib/pkgapi - roxygen2@7.1.1 - rprojroot@2.0.2 - rstudioapi@0.13 diff --git a/R/roxygen2.R b/R/roxygen2.R index 0ea0a5387..7f435dea9 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -118,6 +118,7 @@ roxygen_assert_additinal_dependencies <- function() { #' #' This function is only exported for use in hook scripts, but it's not intended #' to be called by the end-user directly. +#' @inheritParams R.cache::saveCache #' @family hook script helpers #' @export roxygenize_with_cache <- function(key, dirs) { diff --git a/man/roxygenize_with_cache.Rd b/man/roxygenize_with_cache.Rd index c9caf7678..bdbdb4503 100644 --- a/man/roxygenize_with_cache.Rd +++ b/man/roxygenize_with_cache.Rd @@ -6,6 +6,15 @@ \usage{ roxygenize_with_cache(key, dirs) } +\arguments{ +\item{key}{An optional object from which a hexadecimal hash + code will be generated and appended to the filename.} + +\item{dirs}{A \code{\link[base]{character}} \code{\link[base]{vector}} constituting the path to the + cache subdirectory (of the \emph{cache root directory} + as returned by \code{\link[R.cache]{getCacheRootPath}}()) to be used. + If \code{\link[base]{NULL}}, the path will be the cache root path.} +} \description{ This function is only exported for use in hook scripts, but it's not intended to be called by the end-user directly. From b50171f74d30411de963f2476344586301073020 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 11:58:54 +0200 Subject: [PATCH 23/36] load R.cache to avoid random error --- inst/bin/roxygenize | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inst/bin/roxygenize b/inst/bin/roxygenize index f104a9d3a..c70bcb657 100755 --- a/inst/bin/roxygenize +++ b/inst/bin/roxygenize @@ -34,7 +34,8 @@ if (packageVersion("precommit") < "0.1.3.9001") { path_relative_cache <- precommit::dirs_R.cache('roxygenize') wd <- list(getwd()) -cache <- R.cache::loadCache(key = wd, dirs = path_relative_cache) +library(R.cache) +cache <- loadCache(key = wd, dirs = path_relative_cache) if (!is.null(cache)) { candidates <- intersect( From f28482e168ae41826951ef5690e20fb758bebb82 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 12:00:39 +0200 Subject: [PATCH 24/36] more conda quiet errors --- tests/testthat/test-conda.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-conda.R b/tests/testthat/test-conda.R index dd589d3e4..364c5c522 100644 --- a/tests/testthat/test-conda.R +++ b/tests/testthat/test-conda.R @@ -110,7 +110,7 @@ if (!on_cran()) { "installed with conda" ) } else { - tempdir <- local_test_setup(use_precommit = FALSE) + tempdir <- local_test_setup(use_precommit = FALSE, quiet = FALSE) expect_message( uninstall_precommit(scope = "user", ask = "none", root = tempdir), "Removed pre-commit from" From 0c78d4c2a2517dfdb62978b065fe94a423400bfb Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 12:09:39 +0200 Subject: [PATCH 25/36] styler is suggested, not imported --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 92e425d80..9876f9c56 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,7 +21,6 @@ Imports: rlang, rprojroot, rstudioapi, - styler, usethis (>= 2.0.0), withr, yaml @@ -38,6 +37,7 @@ Suggests: rmarkdown, roxygen2, spelling, + styler, testthat (>= 2.1.0) VignetteBuilder: knitr From 9a34763443afe02b68bb45521e7c2e595ea88c79 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 12:13:11 +0200 Subject: [PATCH 26/36] also update R.cache for hooks test --- renv.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/renv.lock b/renv.lock index 2fb36fdb8..86dd75577 100644 --- a/renv.lock +++ b/renv.lock @@ -18,10 +18,10 @@ }, "R.cache": { "Package": "R.cache", - "Version": "0.14.0", + "Version": "0.15.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "1ca02d43e1a4d49e616bd23bb39b17e6" + "Hash": "e92a8ea8388c47c82ed8aa435ed3be50" }, "R.methodsS3": { "Package": "R.methodsS3", From c4986fe94fd19fb15d8170fdd02b5a2d86ad29a9 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 12:29:28 +0200 Subject: [PATCH 27/36] import may help --- API | 4 ++++ NAMESPACE | 3 ++- R/roxygen2.R | 5 +++-- man/diff_requires_run_roxygenize.Rd | 2 +- man/dirs_R.cache.Rd | 2 +- man/may_require_permanent_cache.Rd | 2 +- ...dencies.Rd => roxygen_assert_additional_dependencies.Rd} | 6 +++--- man/roxygenize_with_cache.Rd | 2 +- tests/testthat/test-hook-roxygenize.R | 4 ++-- 9 files changed, 18 insertions(+), 12 deletions(-) rename man/{roxygen_assert_additinal_dependencies.Rd => roxygen_assert_additional_dependencies.Rd} (79%) diff --git a/API b/API index b43386039..be1c95186 100644 --- a/API +++ b/API @@ -4,12 +4,16 @@ autoupdate(root = here::here()) diff_requires_run_roxygenize(root = here::here()) +dirs_R.cache(hook_id) install_precommit(force = FALSE) may_require_permanent_cache(temp_cache_is_enough = FALSE) open_config(root = here::here()) open_wordlist(root = here::here()) path_pre_commit_exec(check_if_exists = TRUE) path_precommit_exec(check_if_exists = TRUE) +roxygen_assert_additional_dependencies() +roxygenize_with_cache(key, dirs) +snippet_generate(snippet, root = here::here()) uninstall_precommit(scope = "repo", ask = "user", root = here::here()) update_precommit() use_precommit(config_source = getOption("precommit.config_source"), force = FALSE, legacy_hooks = "forbid", open = rstudioapi::isAvailable(), install_hooks = TRUE, root = here::here()) diff --git a/NAMESPACE b/NAMESPACE index 1253cd3e2..b98fb7593 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,7 +9,7 @@ export(open_config) export(open_wordlist) export(path_pre_commit_exec) export(path_precommit_exec) -export(roxygen_assert_additinal_dependencies) +export(roxygen_assert_additional_dependencies) export(roxygenize_with_cache) export(snippet_generate) export(uninstall_precommit) @@ -17,4 +17,5 @@ export(update_precommit) export(use_precommit) export(use_precommit_config) export(version_precommit) +importFrom(R.cache,saveCache) importFrom(magrittr,"%>%") diff --git a/R/roxygen2.R b/R/roxygen2.R index 7f435dea9..d6456e117 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -80,7 +80,7 @@ diff_requires_run_roxygenize <- function(root = here::here()) { #' to be called by the end-user directly. #' @family hook script helpers #' @export -roxygen_assert_additinal_dependencies <- function() { +roxygen_assert_additional_dependencies <- function() { out <- rlang::with_handlers( # roxygen2 will load: https://github.com/r-lib/roxygen2/issues/771 pkgload::load_all(quiet = TRUE), @@ -121,6 +121,7 @@ roxygen_assert_additinal_dependencies <- function() { #' @inheritParams R.cache::saveCache #' @family hook script helpers #' @export +#' @importFrom R.cache saveCache roxygenize_with_cache <- function(key, dirs) { if (diff_requires_run_roxygenize()) { out <- rlang::with_handlers( @@ -145,6 +146,6 @@ roxygenize_with_cache <- function(key, dirs) { } else if (inherits(out, "warning")) { rlang::warn(conditionMessage(out)) } - R.cache::saveCache(object = Sys.time(), key = key, dirs = dirs) + saveCache(object = Sys.time(), key = key, dirs = dirs) } } diff --git a/man/diff_requires_run_roxygenize.Rd b/man/diff_requires_run_roxygenize.Rd index 04d3411fd..700eb62ba 100644 --- a/man/diff_requires_run_roxygenize.Rd +++ b/man/diff_requires_run_roxygenize.Rd @@ -27,7 +27,7 @@ diff_requires_run_roxygenize() Other hook script helpers: \code{\link{dirs_R.cache}()}, \code{\link{may_require_permanent_cache}()}, -\code{\link{roxygen_assert_additinal_dependencies}()}, +\code{\link{roxygen_assert_additional_dependencies}()}, \code{\link{roxygenize_with_cache}()} } \concept{hook script helpers} diff --git a/man/dirs_R.cache.Rd b/man/dirs_R.cache.Rd index cb9b4f867..106e8d7c7 100644 --- a/man/dirs_R.cache.Rd +++ b/man/dirs_R.cache.Rd @@ -18,7 +18,7 @@ to be called by the end-user directly. Other hook script helpers: \code{\link{diff_requires_run_roxygenize}()}, \code{\link{may_require_permanent_cache}()}, -\code{\link{roxygen_assert_additinal_dependencies}()}, +\code{\link{roxygen_assert_additional_dependencies}()}, \code{\link{roxygenize_with_cache}()} } \concept{hook script helpers} diff --git a/man/may_require_permanent_cache.Rd b/man/may_require_permanent_cache.Rd index 77478b229..189bb7995 100644 --- a/man/may_require_permanent_cache.Rd +++ b/man/may_require_permanent_cache.Rd @@ -19,7 +19,7 @@ to be called by the end-user directly. Other hook script helpers: \code{\link{diff_requires_run_roxygenize}()}, \code{\link{dirs_R.cache}()}, -\code{\link{roxygen_assert_additinal_dependencies}()}, +\code{\link{roxygen_assert_additional_dependencies}()}, \code{\link{roxygenize_with_cache}()} } \concept{hook script helpers} diff --git a/man/roxygen_assert_additinal_dependencies.Rd b/man/roxygen_assert_additional_dependencies.Rd similarity index 79% rename from man/roxygen_assert_additinal_dependencies.Rd rename to man/roxygen_assert_additional_dependencies.Rd index 1f2fd32fc..53b4b140b 100644 --- a/man/roxygen_assert_additinal_dependencies.Rd +++ b/man/roxygen_assert_additional_dependencies.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/roxygen2.R -\name{roxygen_assert_additinal_dependencies} -\alias{roxygen_assert_additinal_dependencies} +\name{roxygen_assert_additional_dependencies} +\alias{roxygen_assert_additional_dependencies} \title{Assert if all dependencies are installed} \usage{ -roxygen_assert_additinal_dependencies() +roxygen_assert_additional_dependencies() } \description{ This function is only exported for use in hook scripts, but it's not intended diff --git a/man/roxygenize_with_cache.Rd b/man/roxygenize_with_cache.Rd index bdbdb4503..85272ccfd 100644 --- a/man/roxygenize_with_cache.Rd +++ b/man/roxygenize_with_cache.Rd @@ -24,6 +24,6 @@ Other hook script helpers: \code{\link{diff_requires_run_roxygenize}()}, \code{\link{dirs_R.cache}()}, \code{\link{may_require_permanent_cache}()}, -\code{\link{roxygen_assert_additinal_dependencies}()} +\code{\link{roxygen_assert_additional_dependencies}()} } \concept{hook script helpers} diff --git a/tests/testthat/test-hook-roxygenize.R b/tests/testthat/test-hook-roxygenize.R index 309055248..b05517f6a 100644 --- a/tests/testthat/test-hook-roxygenize.R +++ b/tests/testthat/test-hook-roxygenize.R @@ -81,10 +81,10 @@ test_that("asserting installed dependencies", { installed <- c("pkgload", "rlang", "testthat") purrr::walk(installed, usethis::use_package) writeLines(c("utils::adist", "rlang::is_installed"), "R/blur.R") - testthat::expect_silent(roxygen_assert_additinal_dependencies()) + testthat::expect_silent(roxygen_assert_additional_dependencies()) writeLines(generate_uninstalled_pkg_call(), "R/core.R") testthat::expect_error( - roxygen_assert_additinal_dependencies(), + roxygen_assert_additional_dependencies(), "requires all\\* dependencies of your package" ) }) From f8e003fb9e1f7a3f11ca927f3c6e20f590e698da Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 12:36:20 +0200 Subject: [PATCH 28/36] fix function name --- inst/bin/roxygenize | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/bin/roxygenize b/inst/bin/roxygenize index c70bcb657..ea2de7165 100755 --- a/inst/bin/roxygenize +++ b/inst/bin/roxygenize @@ -29,7 +29,7 @@ if (packageVersion("precommit") < "0.1.3.9001") { )) } else { precommit::may_require_permanent_cache(arguments$no_warn_cache) - precommit::roxygen_assert_additinal_dependencies() + precommit::roxygen_assert_additional_dependencies() } path_relative_cache <- precommit::dirs_R.cache('roxygenize') From fd4132e37f7bbbfd2b70e67ac164717827bf1ca8 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 12:37:18 +0200 Subject: [PATCH 29/36] move library call up --- inst/bin/roxygenize | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/inst/bin/roxygenize b/inst/bin/roxygenize index ea2de7165..8a891b9e6 100755 --- a/inst/bin/roxygenize +++ b/inst/bin/roxygenize @@ -21,7 +21,7 @@ Options: " -> doc arguments <- docopt::docopt(doc) - +library(R.cache) if (packageVersion("precommit") < "0.1.3.9001") { rlang::abort(paste( "This hooks only works with the R package {precommit} >= 0.1.3.9001", @@ -34,7 +34,6 @@ if (packageVersion("precommit") < "0.1.3.9001") { path_relative_cache <- precommit::dirs_R.cache('roxygenize') wd <- list(getwd()) -library(R.cache) cache <- loadCache(key = wd, dirs = path_relative_cache) if (!is.null(cache)) { From 1519f20a11fa236dfc716f02e68d52139c970818 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 12:47:00 +0200 Subject: [PATCH 30/36] switch back to not loading the package --- inst/bin/roxygenize | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/inst/bin/roxygenize b/inst/bin/roxygenize index 8a891b9e6..c88515227 100755 --- a/inst/bin/roxygenize +++ b/inst/bin/roxygenize @@ -21,7 +21,6 @@ Options: " -> doc arguments <- docopt::docopt(doc) -library(R.cache) if (packageVersion("precommit") < "0.1.3.9001") { rlang::abort(paste( "This hooks only works with the R package {precommit} >= 0.1.3.9001", @@ -34,7 +33,7 @@ if (packageVersion("precommit") < "0.1.3.9001") { path_relative_cache <- precommit::dirs_R.cache('roxygenize') wd <- list(getwd()) -cache <- loadCache(key = wd, dirs = path_relative_cache) +cache <- R.cache::loadCache(key = wd, dirs = path_relative_cache) if (!is.null(cache)) { candidates <- intersect( From d7d26a0cec087a6e60f4872f8667be45faae6afe Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 12:49:30 +0200 Subject: [PATCH 31/36] add note --- R/roxygen2.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/roxygen2.R b/R/roxygen2.R index d6456e117..fd77f440f 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -122,6 +122,7 @@ roxygen_assert_additional_dependencies <- function() { #' @family hook script helpers #' @export #' @importFrom R.cache saveCache +# fails if accessed with R.cache::saveCache()! roxygenize_with_cache <- function(key, dirs) { if (diff_requires_run_roxygenize()) { out <- rlang::with_handlers( From 35bda579205174a63d3c61f0f446e5eb8d976e44 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 13:11:35 +0200 Subject: [PATCH 32/36] indent consistently --- .pre-commit-config.yaml | 55 ++++++++++++++++++------------------- R/setup.R | 4 +-- tests/testthat/test-setup.R | 4 +-- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6550ae143..d2219ac04 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,34 +14,33 @@ repos: )$ - id: roxygenize additional_dependencies: - - desc@1.3.0 - - digest@0.6.27 - - docopt@0.7.1 - - fs@1.5.0 - - git2r@0.28.0 - - glue@1.4.2 - - here@1.0.1 - - knitr@1.33 - - lintr@2.0.1 - - magrittr@2.0.1 - - mockery@0.4.2 - - pkgload@1.2.1 - - purrr@0.3.4 - - R.cache@0.15.0 - - reticulate@1.20 - - rlang@0.4.11 - - rmarkdown@2.8 - - r-lib/pkgapi - - roxygen2@7.1.1 - - rprojroot@2.0.2 - - rstudioapi@0.13 - - spelling@2.2 - - styler@1.4.1 - - testthat@3.0.2 - - usethis@2.0.1 - - withr@2.4.2 - - yaml@2.2.1 - # codemeta must be above use-tidy-description when both are used + - desc@1.3.0 + - digest@0.6.27 + - docopt@0.7.1 + - fs@1.5.0 + - git2r@0.28.0 + - glue@1.4.2 + - here@1.0.1 + - knitr@1.33 + - lintr@2.0.1 + - magrittr@2.0.1 + - mockery@0.4.2 + - pkgload@1.2.1 + - purrr@0.3.4 + - R.cache@0.15.0 + - reticulate@1.20 + - rlang@0.4.11 + - r-lib/pkgapi@35226e9 + - rmarkdown@2.8 + - roxygen2@7.1.1 + - rprojroot@2.0.2 + - rstudioapi@0.13 + - spelling@2.2 + - styler@1.4.1 + - testthat@3.0.2 + - usethis@2.0.1 + - withr@2.4.2 + - yaml@2.2.1 # codemeta must be above use-tidy-description when both are used # - id: codemeta-description-updated - id: use-tidy-description - id: spell-check diff --git a/R/setup.R b/R/setup.R index acd1b6da6..91df8ae7d 100644 --- a/R/setup.R +++ b/R/setup.R @@ -114,7 +114,7 @@ snippet_generate <- function(snippet = "", root = here::here()) { deps <- desc::desc_get_deps() deps <- deps[order(deps$package), ] paste0( - " - ", deps$package, "@", + " - ", deps$package, "@", purrr::map_chr(deps$package, ~ as.character(packageVersion(.x))), "\n", collapse = "" ) %>% @@ -138,7 +138,7 @@ Remotes: You need in your `.pre-commit-config.yaml` additional_dependencies: - - tidyverse/tidyr@2fd80d5 + - tidyverse/tidyr@2fd80d5 " )) } diff --git a/tests/testthat/test-setup.R b/tests/testthat/test-setup.R index 5d99525e6..b6f0bbdd9 100644 --- a/tests/testthat/test-setup.R +++ b/tests/testthat/test-setup.R @@ -6,7 +6,7 @@ test_that("snippet generation works", { NA ) expect_match( - out, "^ - styler@.+\n - testthat@.+$", + out, "^ - styler@.+\n - testthat@.+$", ) desc::desc_set("Remotes", "r-lib/styler") expect_warning( @@ -14,6 +14,6 @@ test_that("snippet generation works", { "you have remote dependencies " ) expect_match( - out, "^ - styler@.+\n - testthat@.+$", + out, "^ - styler@.+\n - testthat@.+$", ) }) From 66758fd51f70ffb772cee01f819882e885ed1dfe Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 17:18:54 +0200 Subject: [PATCH 33/36] drop random file --- R/blur.R | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 R/blur.R diff --git a/R/blur.R b/R/blur.R deleted file mode 100644 index 2316e4920..000000000 --- a/R/blur.R +++ /dev/null @@ -1,2 +0,0 @@ -utils::adist -rlang::is_installed From 8da11564e785ebb9611fefa86b5123b64dccd7f6 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 17:31:06 +0200 Subject: [PATCH 34/36] yaml formatting --- .pre-commit-config.yaml | 2 +- R/roxygen2.R | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d2219ac04..206a5ebf0 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ default_stages: ["commit"] repos: - repo: https://github.com/lorenzwalthert/precommit - rev: 07429721779500ddabc9a809f9a1e9a773355f3d + rev: 66758fd51f70ffb772cee01f819882e885ed1dfe hooks: - id: style-files args: [--style_pkg=styler, --style_fun=tidyverse_style] diff --git a/R/roxygen2.R b/R/roxygen2.R index fd77f440f..7db12da02 100644 --- a/R/roxygen2.R +++ b/R/roxygen2.R @@ -97,8 +97,8 @@ roxygen_assert_additional_dependencies <- function() { " - id: roxygenize", " additional_dependencies: - - tidyr - - dplyr\n\n", + - tidyr + - dplyr\n\n", "Call ", "`precommit::snippet_generate('additional-deps-roxygenize')`", "and paste the ", From 1435aba4f2c91753517512d90fe9b4995db59874 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 7 May 2021 18:19:06 +0200 Subject: [PATCH 35/36] remove todo --- tests/testthat/test-hook-roxygenize.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/testthat/test-hook-roxygenize.R b/tests/testthat/test-hook-roxygenize.R index b05517f6a..e4904b46e 100644 --- a/tests/testthat/test-hook-roxygenize.R +++ b/tests/testthat/test-hook-roxygenize.R @@ -139,5 +139,3 @@ test_that("warns if there is any other warning", { "Missing name" ) }) - -# todo other warning: # e.g. when there is a package listed that is not in DESCRIPTION From 71cc571a6c0502cd4998e1d26ee9886babe408c4 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 9 May 2021 20:45:00 +0200 Subject: [PATCH 36/36] add newly exported functions to pkgdown --- .pre-commit-config.yaml | 2 +- _pkgdown.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 206a5ebf0..c865a8f53 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ default_stages: ["commit"] repos: - repo: https://github.com/lorenzwalthert/precommit - rev: 66758fd51f70ffb772cee01f819882e885ed1dfe + rev: 1435aba4f2c91753517512d90fe9b4995db59874 hooks: - id: style-files args: [--style_pkg=styler, --style_fun=tidyverse_style] diff --git a/_pkgdown.yml b/_pkgdown.yml index 5f66c26d8..4be9d1587 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -13,6 +13,7 @@ reference: - open_config - open_wordlist - autoupdate + - snippet_generate - title: "Manage the pre-commit executable" - contents: - uninstall_precommit