From 0d991834bc70660c5387a4d3553a9f40359760d1 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Sat, 21 Oct 2017 11:25:57 -0400 Subject: [PATCH] Add covr helper (#1635) * Add test_coverage helper Fixes #1628 --- NAMESPACE | 1 + NEWS.md | 3 +++ R/test.r | 31 +++++++++++++++++++------------ man/test.Rd | 21 ++++++++------------- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 428b00a2d..032875901 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -131,6 +131,7 @@ export(submit_cran) export(system_check) export(system_output) export(test) +export(test_coverage) export(uninstall) export(unload) export(update_packages) diff --git a/NEWS.md b/NEWS.md index 79623d398..3f7664510 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # devtools 1.13.3.9000 +* New `test_coverage()` provides helper to compute test coverage using covr + (#1628). + * `build_win()` has been renamed to `check_win_release()`, `check_win_devel()`, `check_win_oldrelease()` (#1598). diff --git a/R/test.r b/R/test.r index 3cf02e12d..55df758c5 100644 --- a/R/test.r +++ b/R/test.r @@ -1,19 +1,13 @@ #' Execute all \pkg{test_that} tests in a package. #' -#' Tests are assumed to be located in either the \code{inst/tests/} or -#' \code{tests/testthat} directory (the latter is recommended). -#' See \code{\link[testthat]{test_dir}} for the naming convention of test -#' scripts within one of those directories and -#' \code{\link[testthat]{test_check}} for the folder structure conventions. -#' -#' If no testing infrastructure is present -#' (detected by the \code{uses_testthat} function), you'll be asked if you want -#' devtools to create it for you (in interactive sessions only). See -#' \code{\link{use_test}} for more details. +#' `test()` is a shortcut for [testthat::test_dir()]. +#' `test_coverage()` is a shortcut for [covr::package_coverage()]. #' +#' @md #' @param pkg package description, can be path or package name. See -#' \code{\link{as.package}} for more information -#' @param ... additional arguments passed to \code{\link[testthat]{test_dir}} +#' [as.package()] for more information +#' @param ... additional arguments passed to [testthat::test_dir()] and +#' [covr::package_coverage()] #' @inheritParams testthat::test_dir #' @inheritParams run_examples #' @export @@ -68,6 +62,19 @@ test <- function(pkg = ".", filter = NULL, ...) { do.call(testthat::test_dir, testthat_args)) } +#' @export +#' @rdname test +test_coverage <- function(pkg = ".", ...) { + pkg <- as.package(pkg) + + check_suggested("covr") + + coverage <- covr::package_coverage(pkg$path, ...) + covr::report(coverage) + + invisible(coverage) +} + find_test_dir <- function(path) { testthat <- file.path(path, "tests", "testthat") if (dir.exists(testthat)) return(testthat) diff --git a/man/test.Rd b/man/test.Rd index 724227501..bd7cd0913 100644 --- a/man/test.Rd +++ b/man/test.Rd @@ -2,33 +2,28 @@ % Please edit documentation in R/test.r \name{test} \alias{test} +\alias{test_coverage} \alias{uses_testthat} \title{Execute all \pkg{test_that} tests in a package.} \usage{ test(pkg = ".", filter = NULL, ...) +test_coverage(pkg = ".", ...) + uses_testthat(pkg = ".") } \arguments{ \item{pkg}{package description, can be path or package name. See -\code{\link{as.package}} for more information} +\code{\link[=as.package]{as.package()}} for more information} \item{filter}{If not \code{NULL}, only tests with file names matching this regular expression will be executed. Matching will take on the file name after it has been stripped of \code{"test-"} and \code{".R"}.} -\item{...}{additional arguments passed to \code{\link[testthat]{test_dir}}} +\item{...}{additional arguments passed to \code{\link[testthat:test_dir]{testthat::test_dir()}} and +\code{\link[covr:package_coverage]{covr::package_coverage()}}} } \description{ -Tests are assumed to be located in either the \code{inst/tests/} or -\code{tests/testthat} directory (the latter is recommended). -See \code{\link[testthat]{test_dir}} for the naming convention of test -scripts within one of those directories and -\code{\link[testthat]{test_check}} for the folder structure conventions. -} -\details{ -If no testing infrastructure is present -(detected by the \code{uses_testthat} function), you'll be asked if you want -devtools to create it for you (in interactive sessions only). See -\code{\link{use_test}} for more details. +\code{test()} is a shortcut for \code{\link[testthat:test_dir]{testthat::test_dir()}}. +\code{test_coverage()} is a shortcut for \code{\link[covr:package_coverage]{covr::package_coverage()}}. }