Skip to content

Commit

Permalink
Add option to allow code in package_coverage to output to consoel
Browse files Browse the repository at this point in the history
  • Loading branch information
gergness committed Feb 16, 2024
1 parent 3ec2edf commit 3657038
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ Suggests:
box (>= 1.2.0)
License: MIT + file LICENSE
VignetteBuilder: knitr
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# covr (development version)

* Added `code_stdout` option to `package_coverage()` which allows code output to print to the console while running

* Added support for `klmr/box` modules. This works best with `file_coverage()`. (@radbasa, #491)

# covr 3.6.4
Expand Down
33 changes: 23 additions & 10 deletions R/covr.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ environment_coverage <- function(
#' and tests run in. By default it is a path in the R sessions temporary
#' directory. It can sometimes be useful to set this (along with `clean =
#' FALSE`) to help debug test failures.
#' @param code_stdout When running code specified in the `code` argument, whether to
#' use Rscript so that the console updates live (defaults to FALSE, using R CMD BATCH,
#' and only showing the output after all tests have run when there is a failure)
#' @seealso [exclusions()] For details on excluding parts of the
#' package from the coverage calculations.
#' @export
Expand All @@ -364,7 +367,7 @@ package_coverage <- function(path = ".",
code = character(),
install_path = temp_file("R_LIBS"),
...,
exclusions, pre_clean=TRUE) {
exclusions, pre_clean=TRUE, code_stdout = FALSE) {

if (!missing(exclusions)) {
warning(
Expand Down Expand Up @@ -501,7 +504,7 @@ package_coverage <- function(path = ".",

# We always run the commands file (even if empty) to load the package and
# initialize all the counters to 0.
run_commands(pkg, install_path, code)
run_commands(pkg, install_path, code, code_stdout)
},
message = function(e) if (quiet) invokeRestart("muffleMessage") else e,
warning = function(e) if (quiet) invokeRestart("muffleWarning") else e)
Expand Down Expand Up @@ -735,20 +738,30 @@ run_vignettes <- function(pkg, lib) {
}
}

run_commands <- function(pkg, lib, commands) {
run_commands <- function(pkg, lib, commands, code_stdout = FALSE) {
outfile <- file.path(lib, paste0(pkg$package, "-commands.Rout"))
failfile <- paste(outfile, "fail", sep = "." )
writeLines(c(
paste0("library('", pkg$package, "')"),
commands), con = outfile)
cmd <- paste(shQuote(file.path(R.home("bin"), "R")),
"CMD BATCH --vanilla --no-timing",
shQuote(outfile), shQuote(failfile))
res <- system(cmd)
if (res != 0L) {
show_failures(dirname(failfile))

if (!code_stdout) {
cmd <- paste(shQuote(file.path(R.home("bin"), "R")),
"CMD BATCH --vanilla --no-timing",
shQuote(outfile), shQuote(failfile))
res <- system(cmd)
if (res != 0L) {
show_failures(dirname(failfile))
} else {
file.rename(failfile, outfile)
}
} else {
file.rename(failfile, outfile)
cmd <- paste(shQuote(file.path(R.home("bin"), "Rscript")),
"--vanilla", shQuote(outfile))
res <- system(cmd)
if (res != 0L) {
stop("Failure when running covr commands")
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion man/package_coverage.Rd

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

0 comments on commit 3657038

Please sign in to comment.