Skip to content

Commit

Permalink
cleaning up checks + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkf committed Mar 24, 2024
1 parent 497efce commit 7465a0f
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
inst/pkg\.example/\.Rbuildignore
\.github
LICENSE\.md
.github
pkgdown
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ importFrom(utils,getSrcLocation)
importFrom(utils,getSrcref)
importFrom(utils,head)
importFrom(utils,packageName)
importFrom(utils,packageVersion)
importFrom(utils,tail)
9 changes: 2 additions & 7 deletions R/register-s3.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,16 @@ s3_register <- function(generic, class, method = NULL) {
method_fn <- get_method(method)
stopifnot(is.function(method_fn))


# Only register if generic can be accessed
if (exists(generic, envir)) {
registerS3method(generic, class, method_fn, envir = envir)
} else if (identical(Sys.getenv("NOT_CRAN"), "true")) {
warn <- .rlang_s3_register_compat("warn")

warn(c(
warning(c(
sprintf(
"Can't find generic `%s` in package %s to register S3 method.",
generic,
package
),
"i" = "This message is only shown to developers using devtools.",
"i" = sprintf("Do you need to update %s to the latest version?", package)
)
))
}
}
Expand Down
5 changes: 4 additions & 1 deletion R/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,14 @@ test_files <- function(files, context, ...) {

#' Wraps an example expression in a testthat expectation to not error
#'
#' @param expr An expr to wrap in a [`testex::expect_no_error()`] expectation
#' @param expr An expr to wrap in a `expect_no_error()` expectation. Uses
#' 'testthat's version if recent enough version is available, or provides
#' a fallback otherwise.
#' @param value A symbol to use to store the result of `expr`
#'
#' @return A [`testthat::test_that()`] call
#'
#' @importFrom utils packageVersion
#' @keywords internal
wrap_expect_no_error <- function(expr, value) {
srckey <- srcref_key(expr, path = "root")
Expand Down
8 changes: 5 additions & 3 deletions R/use.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @param path A package source code working directory
#' @param check A \code{logical} value indicating whether tests should be
#' executing during \code{R CMD check}.
#' @param quiet Whether output should be suppressed
#'
#' @return The result of [`write.dcf()`] upon modifying the package
#' `DESCRIPTION` file.
Expand Down Expand Up @@ -104,17 +105,17 @@ update_desc_roxygen <- function(desc, report) {
update_desc_suggests <- function(desc, report) {
# add testex to Suggests
suggests <- if (!"Suggests" %in% colnames(desc)) {
character(0L)
""
} else {
desc[1L, "Suggests"]
}

package_re <- paste0("\\b", packageName(), "\\b")
if (!any(grepl(package_re, suggests))) {
lines <- Filter(nchar, strsplit(suggests, "\n")[[1]])
ws <- min(nchar(gsub("[^ ].*", "", lines)))
ws <- min(nchar(gsub("[^ ].*", "", lines)), 4)
package <- paste0(strrep(" ", ws), packageName())
suggests <- paste(c(suggests, package), collapse = ",\n")
suggests <- paste0("\n", paste(c(lines, package), collapse = ",\n"))
report$add("Adding {.code Suggests} package {.pkg {packageName()}}")
}

Expand Down Expand Up @@ -258,6 +259,7 @@ cliless <- function(..., .envir = parent.frame(), .less = FALSE) {
#' @param path A package source code working directory
#' @param context A testthat test context to use as the basis for a new test
#' filename.
#' @param quiet Whether to emit output messages.
#'
#' @return The result of [`writeLines()`] after writing a new `testthat` file.
#'
Expand Down
1 change: 0 additions & 1 deletion inst/pkg.example/.Rbuildignore

This file was deleted.

21 changes: 0 additions & 21 deletions inst/pkg.example/LICENSE.md

This file was deleted.

2 changes: 2 additions & 0 deletions man/use_testex.Rd

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

2 changes: 2 additions & 0 deletions man/use_testex_as_testthat.Rd

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

4 changes: 3 additions & 1 deletion man/wrap_expect_no_error.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test_that("wrap_expect_no_error adds srcref, wraps code in expect_no_error expec
expect_match(res_str, "testthat::test_that")
expect_match(res_str, "testex::with_srcref(\"<text>:1:3\"", fixed = TRUE)
expect_match(res_str, "..Last.value\\s+<<-\\s+")
expect_match(res_str, "testex::expect_no_error\\(\\s*1\\s+\\+\\s+2")
expect_match(res_str, "testthat::expect_no_error\\(\\s*1\\s+\\+\\s+2")
})

test_that("expect_no_error reports when testthat errors occurs while evaluating an expression", {
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-use.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that(paste0(
"use_rd_roclet adds testex to Suggests and Roxygen roclets specification ",
"use_testex adds testex to Suggests and Roxygen roclets specification ",
"if it does not yet exist"
), {
ex_pkg_inst <- system.file(package = "testex", "pkg.example")
Expand All @@ -18,9 +18,9 @@ Version: 1.2.3
withr::defer(unlink(test_dir, recursive = TRUE))

expect_equal(read.dcf(desc_path, fields = "Roxygen")[1,][[1]], NA_character_)
expect_silent(use_rd_roclet(ex_pkg_path))
expect_silent(use_testex(ex_pkg_path, quiet = TRUE))
expect_match(read.dcf(desc_path, fields = "Roxygen")[1,][[1]], "^list\\(")
expect_match(read.dcf(desc_path, fields = "Roxygen")[1,][[1]], "\\btestex::rd\\b")
expect_match(read.dcf(desc_path, fields = "Roxygen")[1,][[1]], "packages = \"testex\"")
expect_match(read.dcf(desc_path, fields = "Suggests")[1,][[1]], "\\btestex\\b")
})

Expand Down

0 comments on commit 7465a0f

Please sign in to comment.