Skip to content

Fix lang r #295

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

Merged
merged 12 commits into from
Oct 7, 2021
21 changes: 17 additions & 4 deletions R/roxygen2.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,21 @@ extract_diff_root <- function(root = here::here()) {
#' @export
diff_requires_run_roxygenize <- function(root = here::here()) {
if (rlang::with_handlers(withr::with_namespace("git2r", FALSE), error = function(...) TRUE)) {
rlang::warn("You need to install the R package git2r (and the required system dependency if you are on Linux) to benefit from caching of this hook.")
generic <- paste0(
" 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/git2r\n\n"
)
msg <- paste0(
"The R package {git2r} must be available to benefit from caching of this hook.",
generic
)
rlang::warn(msg)
return(TRUE)
}
changed_lines_content <- extract_diff_root(root)
Expand Down Expand Up @@ -89,7 +103,7 @@ roxygen_assert_additional_dependencies <- function() {
e
}
)
if (inherits(out, "packageNotFoundError")) {
if (inherits(out, "packageNotFoundError") || ("message" %in% names(out) && grepl("Dependency package(\\(s\\))? .* not available", out$message))) {
# case used in package but not installed
rlang::abort(paste0(
"The roxygenize hook requires all* dependencies of your package to be listed in ",
Expand Down Expand Up @@ -143,8 +157,7 @@ roxygenize_with_cache <- function(key, dirs) {
" - id: roxygenize",
"
additional_dependencies:
- r-lib/pkgapi
- [email protected]\n\n"
- r-lib/pkgapi\n\n"
))
} else if (inherits(out, "error")) {
rlang::abort(conditionMessage(out))
Expand Down
16 changes: 10 additions & 6 deletions R/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,7 @@ snippet_generate <- function(snippet = "", root = here::here()) {
)
deps <- desc::desc_get_deps()
deps <- deps[order(deps$package), ]
paste0(
" - ", deps$package, "@",
purrr::map_chr(deps$package, ~ as.character(packageVersion(.x))), "\n",
collapse = ""
) %>%
sort() %>%
snippet_generate_impl_additional_deps_roxygenize(deps$package) %>%
cat(sep = "")
remote_deps <- rlang::with_handlers(
desc::desc_get_field("Remotes"),
Expand All @@ -144,3 +139,12 @@ You need in your `.pre-commit-config.yaml`
}
}
}

snippet_generate_impl_additional_deps_roxygenize <- function(packages) {
paste0(
" - ", packages, "@",
purrr::map_chr(packages, ~ as.character(packageVersion(.x))), "\n",
collapse = ""
) %>%
sort()
}
27 changes: 27 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ CMD
Ctrl
EOF
Ei
Eic
Homebrew
KernSmooth
Lifecycle
MERCHANTABILITY
RData
Expand All @@ -25,24 +27,31 @@ SHA
Sublicensing
Sys
Uninstallation
Unstaged
Upvote
VignetteBuilder
WIPO
Walthert
Za
ae
api
appveyor
arg
args
artific
autoupdate
bd
beaefa
behaviour
bliblablupp
cfe
ci
cli
cmd
codemeta
codemetar
codetools
commandArgs
comparator
conda
conditionMessage
Expand Down Expand Up @@ -73,10 +82,13 @@ fs
fsf
fsssile
getOption
getParseData
getwd
gh
github
gitignore
grDevices
grepl
gsub
href
http
Expand All @@ -87,10 +99,12 @@ impl
init
io
isAvailable
isTRUE
jpeg
json
knitr
lang
lapply
lgpl
licensors
lintr
Expand All @@ -102,16 +116,20 @@ macOS
magrittr
maxkb
md
mgcv
miniconda
mis
modef
msg
mtime
navbar
netlify
nlme
nnet
noncommercially
nrow
nt
num
oneliner
os
overscope
Expand Down Expand Up @@ -150,23 +168,32 @@ ropenscilabs
roxygen
roxygenise
roxygenize
rpart
rprojroot
rstudio
rstudioapi
saveCache
seealso
sep
setdiff
stderr
stdout
sterr
stopifnot
styler
sublicenses
tcltk
tempfile
testthat
tibble
tidyverse
trailingOnly
travis
tryCatch
ubuntu
uninitialised
unlist
unname
usethis
usr
walthert
Expand Down
22 changes: 0 additions & 22 deletions inst/hooks/exported/readme-rmd-rendered

This file was deleted.

2 changes: 1 addition & 1 deletion tests/testthat/test-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test_that("CLI API works for roxygenize", {
suffix = "-cache-success.R",
env = paste0("R_CACHE_ROOTPATH=", R.cache_root),
msg = "You can silent this",
artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")),
artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf")),
file_transformer = function(x) {
git2r::init()
x
Expand Down
44 changes: 22 additions & 22 deletions tests/testthat/test-conda.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@ if (!on_cran()) {
})

test_that("can use pre-commit", {
tempdir <- local_test_setup(quiet = FALSE)
tempdir <- local_test_setup(quiet = FALSE, install_hooks = FALSE)
expect_message(
use_precommit(open = FALSE, force = TRUE, root = tempdir),
use_precommit(open = FALSE, force = TRUE, install_hooks = FALSE, root = tempdir),
"to get the latest"
)
expect_message(
use_precommit(open = FALSE, force = FALSE, root = tempdir),
use_precommit(open = FALSE, force = FALSE, install_hooks = FALSE, root = tempdir),
"There is already "
)
})

test_that("fails early if repo is not a git repo ", {
tempdir <- local_test_setup(git = FALSE, quiet = FALSE)
tempdir <- local_test_setup(git = FALSE, quiet = FALSE, install_hooks = FALSE)

expect_error(
use_precommit(open = FALSE, root = tempdir),
use_precommit(open = FALSE, install_hooks = FALSE, root = tempdir),
"is not a git repo"
)
})

test_that("can use custom config file ", {
tempdir1 <- local_test_setup()
tempdir2 <- local_test_setup()
tempdir1 <- local_test_setup(install_hooks = FALSE)
tempdir2 <- local_test_setup(install_hooks = FALSE)

path_custom <- fs::path(tempdir2, "some-precommit.yaml")
new_text <- "# 4js93"
readLines(system.file("pre-commit-config-proj.yaml", package = "precommit")) %>%
c(new_text) %>%
writeLines(path_custom)
git2r::init(tempdir1)
use_precommit(config_source = path_custom, open = FALSE, force = TRUE, root = tempdir1)
use_precommit(config_source = path_custom, open = FALSE, force = TRUE, install_hooks = FALSE, root = tempdir1)
config <- readLines(fs::path(tempdir1, ".pre-commit-config.yaml"))
expect_equal(
config[length(config)],
Expand All @@ -45,33 +45,33 @@ if (!on_cran()) {
})

test_that("existing hooks are recognized", {
tempdir <- local_test_setup(quiet = FALSE)
tempdir <- local_test_setup(quiet = FALSE, install_hooks = FALSE)
withr::with_dir(tempdir, {
git2r::init()
usethis::proj_set(".")
usethis::use_readme_rmd(open = FALSE)

# usethis hook is removed without error
expect_message(
use_precommit(legacy_hooks = "forbid", open = FALSE, root = "."),
use_precommit(legacy_hooks = "forbid", open = FALSE, install_hooks = FALSE, root = "."),
"Removed the render-README hook,"
)
writeLines(letters, ".git/hooks/pre-commit")
expect_error(
use_precommit(legacy_hooks = "forbid", open = FALSE, root = "."),
use_precommit(legacy_hooks = "forbid", open = FALSE, install_hooks = FALSE, root = "."),
"existing hooks installed"
)

# tolerate other hook scripts in migration mode
expect_message(
use_precommit(legacy_hooks = "allow", force = TRUE, open = FALSE, root = "."),
use_precommit(legacy_hooks = "allow", force = TRUE, open = FALSE, install_hooks = FALSE, root = "."),
"Running in migration"
)

# can also remove other hooks
writeLines(letters, ".git/hooks/pre-commit")
expect_message(
use_precommit(legacy_hooks = "remove", force = TRUE, open = FALSE, root = "."),
use_precommit(legacy_hooks = "remove", force = TRUE, open = FALSE, install_hooks = FALSE, root = "."),
"Sucessfully installed"
)
})
Expand All @@ -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, quiet = FALSE)
tempdir <- local_test_setup(use_precommit = TRUE, quiet = FALSE, install_hooks = FALSE)
expect_message(
uninstall_precommit(scope = "repo", root = tempdir),
"Uninstalled pre-commit from repo scope.*"
Expand All @@ -94,7 +94,7 @@ if (!on_cran()) {
)

# when there is no pre-commit.yaml anymore
suppressMessages(use_precommit(open = FALSE, force = TRUE, root = tempdir))
suppressMessages(use_precommit(open = FALSE, force = TRUE, install_hooks = FALSE, root = tempdir))
fs::file_delete(fs::path(tempdir, ".pre-commit-config.yaml"))
expect_message(
uninstall_precommit(scope = "repo", root = tempdir),
Expand All @@ -104,13 +104,13 @@ if (!on_cran()) {

test_that("Can uninstall (userly)", {
if (not_conda()) {
tempdir <- local_test_setup(use_precommit = TRUE, quiet = FALSE)
tempdir <- local_test_setup(use_precommit = TRUE, quiet = FALSE, install_hooks = FALSE)
expect_error(
uninstall_precommit(scope = "user", ask = "none", root = tempdir),
"installed with conda"
)
} else {
tempdir <- local_test_setup(use_precommit = FALSE, quiet = FALSE)
tempdir <- local_test_setup(use_precommit = FALSE, quiet = FALSE, install_hooks = FALSE)
expect_message(
uninstall_precommit(scope = "user", ask = "none", root = tempdir),
"Removed pre-commit from"
Expand All @@ -124,18 +124,18 @@ if (!on_cran()) {

test_that("use_precommit fails when no user installation is found", {
skip_if(not_conda())
expect_error(use_precommit(open = FALSE, root = tempdir), "installed on your system")
expect_error(use_precommit(open = FALSE, install_hooks = FALSE, root = tempdir), "installed on your system")
})

test_that("can install pre-commit with remote config", {
if (!not_conda()) {
expect_error(install_precommit(), NA)
}
tempdir <- local_test_setup(quiet = FALSE)
tempdir <- local_test_setup(quiet = FALSE, install_hooks = FALSE)
expect_message(
use_precommit(
example_remote_config(),
open = FALSE, force = TRUE, root = tempdir
open = FALSE, force = TRUE, install_hooks = FALSE, root = tempdir
),
"to get the latest"
)
Expand All @@ -145,14 +145,14 @@ if (!on_cran()) {
if (!not_conda()) {
expect_message(install_precommit(), "already installed")
}
tempdir <- local_test_setup(use_precommit = FALSE, quiet = FALSE)
tempdir <- local_test_setup(use_precommit = FALSE, quiet = FALSE, install_hooks = FALSE)
withr::with_dir(
tempdir,
{
withr::defer(call_and_capture("git", "config --unset-all core.hooksPath"))
call_and_capture("git", "config core.hooksPath .githooks")
expect_error(
use_precommit(open = FALSE, force = TRUE, root = tempdir),
use_precommit(open = FALSE, force = TRUE, install_hooks = FALSE, root = tempdir),
"stdout: [ERROR] Cowardly refusing to install hooks with `core.hooksPath` set.",
fixed = TRUE
)
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-hook-roxygenize.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ test_that("fails gratefully when not installed package is required according to
)
mockery::stub(roxygenize_with_cache, "diff_requires_run_roxygenize", TRUE)
expect_error(
roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")),
suppressWarnings(
roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize"))
),
"Please add the package"
)
})
Expand Down
Loading