From 7465a0f45fd342790bfc23aa2c5f9e4c02657a3b Mon Sep 17 00:00:00 2001 From: dgkf <18220321+dgkf@users.noreply.github.com> Date: Sun, 24 Mar 2024 15:49:27 -0400 Subject: [PATCH] cleaning up checks + tests --- .Rbuildignore | 4 ++-- NAMESPACE | 1 + R/register-s3.R | 9 ++------- R/testthat.R | 5 ++++- R/use.R | 8 +++++--- inst/pkg.example/.Rbuildignore | 1 - inst/pkg.example/LICENSE.md | 21 --------------------- man/use_testex.Rd | 2 ++ man/use_testex_as_testthat.Rd | 2 ++ man/wrap_expect_no_error.Rd | 4 +++- tests/testthat/test-testthat.R | 2 +- tests/testthat/test-use.R | 6 +++--- 12 files changed, 25 insertions(+), 40 deletions(-) delete mode 100644 inst/pkg.example/.Rbuildignore delete mode 100644 inst/pkg.example/LICENSE.md diff --git a/.Rbuildignore b/.Rbuildignore index b56c606..a01807a 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,3 +1,3 @@ -inst/pkg\.example/\.Rbuildignore +\.github LICENSE\.md -.github +pkgdown diff --git a/NAMESPACE b/NAMESPACE index 4dee89c..447da97 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,4 +15,5 @@ importFrom(utils,getSrcLocation) importFrom(utils,getSrcref) importFrom(utils,head) importFrom(utils,packageName) +importFrom(utils,packageVersion) importFrom(utils,tail) diff --git a/R/register-s3.R b/R/register-s3.R index 35ddbc3..ebbe04e 100644 --- a/R/register-s3.R +++ b/R/register-s3.R @@ -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) + ) )) } } diff --git a/R/testthat.R b/R/testthat.R index 33067a5..8dc04ed 100644 --- a/R/testthat.R +++ b/R/testthat.R @@ -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") diff --git a/R/use.R b/R/use.R index b9d566e..518e83e 100644 --- a/R/use.R +++ b/R/use.R @@ -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. @@ -104,7 +105,7 @@ 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"] } @@ -112,9 +113,9 @@ update_desc_suggests <- function(desc, report) { 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()}}") } @@ -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. #' diff --git a/inst/pkg.example/.Rbuildignore b/inst/pkg.example/.Rbuildignore deleted file mode 100644 index 5163d0b..0000000 --- a/inst/pkg.example/.Rbuildignore +++ /dev/null @@ -1 +0,0 @@ -^LICENSE\.md$ diff --git a/inst/pkg.example/LICENSE.md b/inst/pkg.example/LICENSE.md deleted file mode 100644 index b9ec8bf..0000000 --- a/inst/pkg.example/LICENSE.md +++ /dev/null @@ -1,21 +0,0 @@ -# MIT License - -Copyright (c) 2022 testex authors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/man/use_testex.Rd b/man/use_testex.Rd index 22426cf..eaaf636 100644 --- a/man/use_testex.Rd +++ b/man/use_testex.Rd @@ -11,6 +11,8 @@ use_testex(path = getwd(), check = TRUE, quiet = FALSE) \item{check}{A \code{logical} value indicating whether tests should be executing during \code{R CMD check}.} + +\item{quiet}{Whether output should be suppressed} } \value{ The result of \code{\link[=write.dcf]{write.dcf()}} upon modifying the package diff --git a/man/use_testex_as_testthat.Rd b/man/use_testex_as_testthat.Rd index 3d0cfbb..397be94 100644 --- a/man/use_testex_as_testthat.Rd +++ b/man/use_testex_as_testthat.Rd @@ -11,6 +11,8 @@ use_testex_as_testthat(path = getwd(), context = "testex", quiet = FALSE) \item{context}{A testthat test context to use as the basis for a new test filename.} + +\item{quiet}{Whether to emit output messages.} } \value{ The result of \code{\link[=writeLines]{writeLines()}} after writing a new \code{testthat} file. diff --git a/man/wrap_expect_no_error.Rd b/man/wrap_expect_no_error.Rd index ce11172..58b5594 100644 --- a/man/wrap_expect_no_error.Rd +++ b/man/wrap_expect_no_error.Rd @@ -7,7 +7,9 @@ wrap_expect_no_error(expr, value) } \arguments{ -\item{expr}{An expr to wrap in a \code{\link[=expect_no_error]{expect_no_error()}} expectation} +\item{expr}{An expr to wrap in a \code{expect_no_error()} expectation. Uses +'testthat's version if recent enough version is available, or provides +a fallback otherwise.} \item{value}{A symbol to use to store the result of \code{expr}} } diff --git a/tests/testthat/test-testthat.R b/tests/testthat/test-testthat.R index 274ce87..f3fdf04 100644 --- a/tests/testthat/test-testthat.R +++ b/tests/testthat/test-testthat.R @@ -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(\":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", { diff --git a/tests/testthat/test-use.R b/tests/testthat/test-use.R index 7996231..55cdd19 100644 --- a/tests/testthat/test-use.R +++ b/tests/testthat/test-use.R @@ -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") @@ -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") })