Skip to content

Improve cache validation #214

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 5 commits into from
Nov 1, 2020
Merged
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
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ For this reason, always test the hook manually end-to-end with

- add a description of the new hook to the `README.Rmd`. Both description and
`yaml` example code.

12 changes: 11 additions & 1 deletion R/roxygen2.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,15 @@ diff_requires_run_roxygenize <- function(root = here::here()) {
rlang::abort("You need to install the R package git2r to run this hook.")
}
changed_lines_content <- extract_diff_root(root)
any(grepl("^#'", changed_lines_content))
is_roxygen <- grepl("^#'", changed_lines_content)
if (any(is_roxygen)) {
return(TRUE)
} else {
# check if formals were changed
# we invalidate the cache on formal change, even if it is not sure they are
# documented with roxygen. This is easy, cheap and safe. Might give false
# positive (invalidates in cases where it's not necessary).
without_comments <- gsub("#.*", "", changed_lines_content)
any(grep("function(", without_comments, fixed = TRUE))
}
}
1 change: 0 additions & 1 deletion inst/bin/codemeta-description-updated
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ then
echo >&2 "codemeta.json is out of date; please re-run codemetar::write_codemeta()."
exit 1
fi

2 changes: 1 addition & 1 deletion inst/bin/no-browser-statement
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ no_browser_statement <- function(path) {

for (file in files) {
temp <- no_browser_statement(file)
}
}
2 changes: 1 addition & 1 deletion inst/bin/use-tidy-description
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env Rscript

usethis::use_tidy_description()
usethis::use_tidy_description()
1 change: 0 additions & 1 deletion tests/testthat/in/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fsssile

39 changes: 33 additions & 6 deletions tests/testthat/test-hook-roxygenize.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
path <- fs::path(tempdir(), "a12")
fs::dir_create(path)
path_r <- fs::path(path, "R")
fs::dir_create(path_r)

test_that("roxygen runs are done if necessary", {
path <- fs::path(tempdir(), "a12")
fs::dir_create(path)
path_r <- fs::path(path, "R")
fs::dir_create(path_r)
setwd(path)
withr::with_dir(
path,
{
Expand All @@ -24,7 +24,7 @@ test_that("roxygen runs are done if necessary", {
git2r::commit(".", "add roxgen2 file")

# when non roxygen lines are added
text2 <- c("x <- function()", " NULL")
text2 <- c("if (x) {", " TRUE", "} else {", " not_TRue(sinux(-i))", "}")
writeLines(text2, "R/third.R")
expect_equal(extract_diff_root("."), NULL)
git2r::add(".", "R/third.R")
Expand All @@ -51,6 +51,33 @@ test_that("roxygen runs are done if necessary", {
git2r::add(".", "R/first.R")
expect_equal(length(extract_diff_root(".")), 5)
expect_true(diff_requires_run_roxygenize("."))
git2r::commit(".", "when reomved")
}
)
})

test_that("change in formals alone triggers invalidation", {
# when the function formals change but nothing else
withr::with_dir(
path,
{
# when new lines are added
text <- c("#' Roxygen comment", "#'", "#' more things", "x <- function(a = 2) {", " a", "}")
writeLines(text, "R/fifth.R")
expect_equal(length(extract_diff_root(".")), 0)
expect_false(diff_requires_run_roxygenize("."))
git2r::add(".", "R/fifth.R")

expect_equal(extract_diff_root("."), add_trailing_linebreak(text))
expect_true(diff_requires_run_roxygenize("."))
git2r::commit(".", "add file 5")
# change signature
text <- c("#' Roxygen comment", "#'", "#' more things", "x <- function(a = 3) {", " a", "}")
writeLines(text, "R/fifth.R")
git2r::add(".", "R/fifth.R")
expect_equal(extract_diff_root("."), add_trailing_linebreak(c("x <- function(a = 2) {", "x <- function(a = 3) {")))
expect_true(diff_requires_run_roxygenize("."))
git2r::commit(".", "clear case 5")
}
)
})