From af667337b92cbe3573c5652cf959b2d5fc50b500 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Mon, 12 Aug 2024 15:36:59 -0700 Subject: [PATCH 1/3] invoke package installation in separate R process --- NEWS.md | 3 +++ R/covr.R | 32 ++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/NEWS.md b/NEWS.md index a3b0a314..f7bfb25f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # covr (development version) +* Fixed an issue where attempting to generate code coverage on an already-loaded + package could fail on Windows. (@kevinushey, #574) + * Prevent `covr.record_tests` option from logging duplicate tests when the same line of testing code is hit repeatedly, as in a loop. (@dgkf, #528) diff --git a/R/covr.R b/R/covr.R index 106d8f03..7d6499fa 100644 --- a/R/covr.R +++ b/R/covr.R @@ -441,18 +441,26 @@ package_coverage <- function(path = ".", if (isTRUE(pre_clean)) clean_objects(pkg$path) # install the package in a temporary directory - withr::with_makevars(flags, assignment = "+=", - utils::install.packages(repos = NULL, - lib = install_path, - pkg$path, - type = "source", - INSTALL_opts = c("--example", - "--install-tests", - "--with-keep.source", - "--with-keep.parse.data", - "--no-staged-install", - "--no-multiarch"), - quiet = quiet)) + withr::with_envvar( + list(R_LIBS = paste(.libPaths(), collapse = .Platform$path.sep)), + withr::with_makevars(flags, assignment = "+=", { + args <- c( + "--vanilla", "CMD", "INSTALL", + "-l", shQuote(install_path), + "--example", + "--install-tests", + "--with-keep.source", + "--with-keep.parse.data", + "--no-staged-install", + "--no-multiarch", + pkg$path + ) + + name <- if (.Platform$OS.type == "windows") "R.exe" else "R" + path <- file.path(R.home("bin"), name) + system2(path, args) + }) + ) # add hooks to the package startup add_hooks(pkg$package, install_path, From 55405432b4463dd354bee499da8164455c5baeb9 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Mon, 12 Aug 2024 15:41:25 -0700 Subject: [PATCH 2/3] add shQuote; more robust status check --- R/covr.R | 2 +- tests/testthat/test-record_tests.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/covr.R b/R/covr.R index 7d6499fa..7510db65 100644 --- a/R/covr.R +++ b/R/covr.R @@ -453,7 +453,7 @@ package_coverage <- function(path = ".", "--with-keep.parse.data", "--no-staged-install", "--no-multiarch", - pkg$path + shQuote(pkg$path) ) name <- if (.Platform$OS.type == "windows") "R.exe" else "R" diff --git a/tests/testthat/test-record_tests.R b/tests/testthat/test-record_tests.R index c7c1bd83..712f4e70 100644 --- a/tests/testthat/test-record_tests.R +++ b/tests/testthat/test-record_tests.R @@ -194,7 +194,7 @@ test_that("covr.record_tests: safely handles extremely large calls", { res <- system2(file.path(R.home("bin"), "R"), list("-q", "-s", "--vanilla", "-f", r_script), stdout = TRUE, stderr = TRUE) }) - if (attr(res, "status") == 0L) { + if (identical(attr(res, "status"), 0L)) { warning(paste0(collapse = "\n", strwrap(paste0( "Looks like R was updated and the work-around for Rds ", "deserialization segfaults can now be made to apply conditionally to only ", From 4a5ee5a3a133032f4a8e3ea86ad0966800e316b0 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Mon, 12 Aug 2024 15:56:46 -0700 Subject: [PATCH 3/3] add Rf_ prefix --- tests/testthat/Test+Char/TestCompiled/src/simple.cc | 2 +- tests/testthat/TestCompiled/src/simple-header.h | 2 +- tests/testthat/TestCompiled/src/simple.cc | 2 +- tests/testthat/TestCompiledSubdir/src/lib/simple.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/Test+Char/TestCompiled/src/simple.cc b/tests/testthat/Test+Char/TestCompiled/src/simple.cc index 52a0ca58..4d6c69e4 100644 --- a/tests/testthat/Test+Char/TestCompiled/src/simple.cc +++ b/tests/testthat/Test+Char/TestCompiled/src/simple.cc @@ -6,7 +6,7 @@ extern "C" SEXP simple_(SEXP x) { double *px, *pout; - SEXP out = PROTECT(allocVector(REALSXP, 1)); + SEXP out = PROTECT(Rf_allocVector(REALSXP, 1)); px = REAL(x); pout = REAL(out); diff --git a/tests/testthat/TestCompiled/src/simple-header.h b/tests/testthat/TestCompiled/src/simple-header.h index a55cd33a..55978b6b 100644 --- a/tests/testthat/TestCompiled/src/simple-header.h +++ b/tests/testthat/TestCompiled/src/simple-header.h @@ -9,7 +9,7 @@ template SEXP simple2_(SEXP x) { R *px, *pout; - SEXP out = PROTECT(allocVector(R_SXP, 1)); + SEXP out = PROTECT(Rf_allocVector(R_SXP, 1)); px = (R *) DATAPTR(x); pout = (R *) DATAPTR(out); diff --git a/tests/testthat/TestCompiled/src/simple.cc b/tests/testthat/TestCompiled/src/simple.cc index bc11e344..eca4d816 100644 --- a/tests/testthat/TestCompiled/src/simple.cc +++ b/tests/testthat/TestCompiled/src/simple.cc @@ -7,7 +7,7 @@ extern "C" SEXP simple_(SEXP x) { double *px, *pout; - SEXP out = PROTECT(allocVector(REALSXP, 1)); + SEXP out = PROTECT(Rf_allocVector(REALSXP, 1)); px = REAL(x); pout = REAL(out); diff --git a/tests/testthat/TestCompiledSubdir/src/lib/simple.c b/tests/testthat/TestCompiledSubdir/src/lib/simple.c index 8a382879..4867d2df 100644 --- a/tests/testthat/TestCompiledSubdir/src/lib/simple.c +++ b/tests/testthat/TestCompiledSubdir/src/lib/simple.c @@ -6,7 +6,7 @@ SEXP simple_(SEXP x) { double *px, *pout; - SEXP out = PROTECT(allocVector(REALSXP, 1)); + SEXP out = PROTECT(Rf_allocVector(REALSXP, 1)); px = REAL(x); pout = REAL(out);