Skip to content

Commit

Permalink
Fix for breaking changes in glue.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rory Nolan committed Oct 7, 2024
1 parent 7687f9b commit a09924e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 139 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: exampletestr
Title: Help for Writing Unit Tests Based on Function Examples
Version: 1.7.2
Version: 1.7.3
Authors@R:
c(person(given = "Rory",
family = "Nolan",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# `exampletestr` 1.7.3

## BUG FIXES
* `glue` had breaking changes for which `exampletestr` needed to be fixed.


# `exampletestr` 1.7.2

## BUG FIXES
Expand Down
37 changes: 18 additions & 19 deletions R/exemplar.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ make_test_shell_fun <- function(fun, pkg_dir = ".",
fun %<>% stringr::str_trim()
if (stringr::str_detect(fun, stringr::coll("("))) {
if (strex::str_elem(fun, 1) %in% c("(", ")")) {
custom_stop(
"The function 'fun' cannot start with a parenthesis.",
"Your 'fun' is '{fun}'."
rlang::abort(
c(
"The function 'fun' cannot start with a parenthesis.",
i = stringr::str_glue("Your 'fun' is '{fun}'.")
)
)
}
fun %<>% stringr::str_extract("^.*\\(") %>%
Expand Down Expand Up @@ -119,24 +121,21 @@ make_test_shell_fun <- function(fun, pkg_dir = ".",
}
if (!fun_found) {
if (is_documented(fun)) {
custom_stop(
"
The function {usethis::ui_code(stringr::str_c(fun, '()'))}
is documented but has no accompanying examples.
", "
{usethis::ui_code('make_test_shell_fun()')} only works on functions
with examples.
"
rlang::abort(
c(
stringr::str_glue(
"The function {fun}() is documented but has no examples."
),
"i" = "make_test_shell_fun() only works on functions with examples."
)
)
}
custom_stop(
"
Could not find a documented function called
{usethis::ui_code(stringr::str_c(fun, '()'))}.
", "
Make sure it's documented in the {usethis::ui_code('man/')} folder of
your package.
"
rlang::abort(
c(
stringr::str_glue(
"Could not find a documented function called '{fun}'."
)
)
)
}
}
Expand Down
98 changes: 18 additions & 80 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,65 +182,15 @@ make_available_test_file_name <- function(test_file_name) {
test_file_name
}

#' Construct the bullet point bits for `custom_stop()`.
#'
#' @param string The message for the bullet point.
#'
#' @return A string with the bullet-pointed message nicely formatted for the
#' console.
#'
#' @noRd
custom_stop_bullet <- function(string) {
checkmate::assert_string(string)
string %>%
stringr::str_replace_all("\\s+", " ") %>%
paste(" *", .)
}

#' Nicely formatted error message.
#'
#' Format an error message with bullet-pointed sub-messages with nice
#' line-breaks.
#'
#' Arguments should be entered as `glue`-style strings.
#'
#' @param main_message The main error message.
#' @param ... Bullet-pointed sub-messages.
#'
#' @noRd
custom_stop <- function(main_message, ..., .envir = parent.frame()) {
checkmate::assert_string(main_message)
main_message %<>%
stringr::str_replace_all("\\s+", " ") %>%
stringr::str_glue(.envir = .envir)
out <- main_message
dots <- unlist(list(...))
if (length(dots)) {
if (!is.character(dots)) {
stop("\nThe arguments in ... must all be of character type.")
}
dots %<>%
purrr::map_chr(stringr::str_glue, .envir = .envir) %>%
purrr::map_chr(custom_stop_bullet)
out %<>% {
stringr::str_c(c(., dots), collapse = "\n")
}
}
rlang::abort(stringr::str_c(out, collapse = "\n"))
}

check_for_DESCRIPTION <- function() {
if (!fs::file_exists(usethis::proj_path("DESCRIPTION"))) {
custom_stop(
"Your package has no {usethis::ui_path('DESCRIPTION')} file.",
"
Every R package must have a {usethis::ui_path('DESCRIPTION')} file in the
root directory.
",
"Perhaps you specified the wrong {usethis::ui_code('pkg_dir')}?",
"You specified {usethis::ui_code(pkgdireq)}.",
.envir = list(
pkgdireq = paste0("pkg_dir = \"", usethis::proj_path(), "\"")
rlang::abort(
c(
"Your package has no 'DESCRIPTION' file.",
"i" = "Every R package must have a 'DESCRIPTION' in the root dir.",
"i" = stringr::str_glue(
"You specified the package '{usethis::proj_path()}'."
)
)
)
}
Expand All @@ -249,32 +199,20 @@ check_for_DESCRIPTION <- function() {

check_for_man <- function() {
if (!fs::dir_exists(usethis::proj_path("man"))) {
custom_stop(
"
Your package has no
{usethis::ui_path(usethis::proj_path('man/'),
usethis::proj_path())} folder.",
"
{usethis::ui_code('exampletestr')} looks for examples in the
{usethis::ui_path('*.Rd')} files in the
{usethis::ui_path(usethis::proj_path('man/'), usethis::proj_path())}
folder of a package and cannot function without them.
",
.envir = list(
pkgdireq = paste0("pkg_dir = \"", usethis::proj_path(), "\"")
rlang::abort(
c(
"Your package has no 'man/' folder.",
"i" = "'exampletestr' looks for examples in the .Rd files in 'man/'.",
"i" = stringr::str_glue("Package path used: '{usethis::proj_path()}'.")
)
)
} else if (!length(fs::dir_ls(usethis::proj_path("man")))) {
custom_stop(
"Your package has no {usethis::ui_path('*.Rd')} files in its
{usethis::ui_path(usethis::proj_path('man'), usethis::proj_path())}
folder.",
"
exampletestr looks for examples in the {usethis::ui_path('*.Rd')}
files in the {usethis::ui_path(usethis::proj_path('man'),
usethis::proj_path())} folder of a package and cannot
function if there are no {usethis::ui_path('*.Rd')} files there.
"
rlang::abort(
c(
"Your package has no .Rd files in the 'man/' folder.",
"i" = "'exampletestr' looks for examples in the .Rd files in 'man/'.",
"i" = stringr::str_glue("Package path used: '{usethis::proj_path()}'.")
)
)
}
invisible(TRUE)
Expand Down
4 changes: 2 additions & 2 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"codeRepository": "https://github.com/rorynolan/exampletestr",
"issueTracker": "https://github.com/rorynolan/exampletestr/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "1.7.2",
"version": "1.7.3",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down Expand Up @@ -324,7 +324,7 @@
},
"SystemRequirements": null
},
"fileSize": "89.239KB",
"fileSize": "86.299KB",
"citation": [
{
"@type": "ScholarlyArticle",
Expand Down
27 changes: 4 additions & 23 deletions tests/testthat/test-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ test_that("`make_tests_shell_fun()` works", {
)
expect_error(
make_test_shell_fun("non_fun", pkg_dir = pkg_dir),
"Could not find.*non_fun\\(\\)"
"Could not find.*'non_fun'"
)
fs::file_copy(system.file("extdata", c("hello.R", "goodbye.R"),
package = "exampletestr"
Expand All @@ -108,14 +108,7 @@ test_that("`make_tests_shell_fun()` works", {
)
)$message
expect_match(crayon::strip_style(no_man_err_msg),
paste(
"Your package has no 'man' folder.\n *",
"`exampletestr` looks for examples in",
"the '*.Rd' files in",
"the 'man' folder of a package and",
"cannot function without them."
),
fixed = TRUE
"no.+man.+folder"
)
fs::dir_create(paste0(pkg_dir2, "/man"))
no_rd_err_msg <- rlang::catch_cnd(
Expand All @@ -125,15 +118,7 @@ test_that("`make_tests_shell_fun()` works", {
)
)$message
expect_match(crayon::strip_style(no_rd_err_msg),
paste(
"Your package has no '*.Rd' files in",
"its 'man/' folder.\n * exampletestr",
"looks for examples in the '*.Rd' files",
"in the 'man/' folder of a package and",
"cannot function if there are no '*.Rd'",
"files there."
),
fixed = TRUE
"no.+.Rd.+files.+man.+folder"
)
no_examples_err_msg <- rlang::catch_cnd(
make_test_shell_fun("hello",
Expand All @@ -144,11 +129,7 @@ test_that("`make_tests_shell_fun()` works", {
)$message
expect_match(
crayon::strip_style(no_examples_err_msg),
paste0(
"The function .+ is documented but has.+",
"no.+accompanying.+examples.+",
"only works on.+functions.+with.+examples."
)
"function.+documented.+no.+examples"
)
})
})
15 changes: 1 addition & 14 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,8 @@ test_that("`check_for_DESCRIPTION()` works", {
)$message
expect_match(
crayon::strip_style(no_DESCRIPTION_err_msg),
paste0(
"Your package has no 'DESCRIPTION' file.\n",
" * Every R package must have a 'DESCRIPTIO",
"N' file in the root directory.\n * Perhaps ",
"you specified the wrong `pkg_dir`?\n *",
" You specified `pkg_dir = "
),
"Your package has no 'DESCRIPTION' file.",
fixed = TRUE
)
readr::write_lines(description, paste0(pkg_dir, "/DESCRIPTION"))
})

test_that("custom_stop works", {
expect_error(
custom_stop("1", 2, 3),
"arguments .+ must all be of character type"
)
})

0 comments on commit a09924e

Please sign in to comment.