Skip to content

Commit

Permalink
Move rmarkdown::render action to happen in a temp directory (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
bburns632 authored May 18, 2024
1 parent cff4610 commit 994723d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## CHANGES

## BUGFIXES
* Moved `rmarkdown::render` interium files to occur within a temp directory, not the installed package directory (#329 Thanks @jcarbaut!)

# pkgnet 0.5.0
## NEW FEATURES
Expand Down
26 changes: 22 additions & 4 deletions R/CreatePackageReport.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,28 @@ PackageReport <- R6::R6Class(
, render_report = function() {
log_info("Rendering package report...")

# copy Rmd files to temp directory to avoid writing interim files to package repo
tmp_dir <- tempfile("package_report") # subdir within temp directory
tmp_package_report_rmd <- file.path(tmp_dir, "package_report.Rmd")

orig_package_report_rmd <- system.file(file.path("package_report", "package_report.Rmd"), package = "pkgnet")
orig_files <- list.files(dirname(orig_package_report_rmd), full.names = TRUE)

dir.create(tmp_dir)
file.copy(
from = orig_files,
to = tmp_dir,
recursive = TRUE,
overwrite = TRUE
)

# render report
rmarkdown::render(
input = system.file(
file.path("package_report", "package_report.Rmd")
, package = "pkgnet"
)
input = tmp_package_report_rmd
, output_dir = dirname(self$report_path)
, output_file = basename(self$report_path)
, intermediates_dir = tmp_dir
, knit_root_dir = tmp_dir
, quiet = TRUE
, params = list(
reporters = private$reporters
Expand All @@ -86,6 +101,9 @@ PackageReport <- R6::R6Class(
, sprintf("It is available at %s", self$report_path)
))

# Clean up tmp
unlink(tmp_dir, recursive = TRUE)

# If suppress flag is unset, then env variable will be emptry string ""
if (identical(Sys.getenv("PKGNET_SUPPRESS_BROWSER"), "")) {
utils::browseURL(self$report_path)
Expand Down
26 changes: 26 additions & 0 deletions R/testing_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,29 @@

return(invisible(TRUE))
}


# Get Files with Modification Date after a specific date
# Used to provide more verbose logging in the event package directory is modified.
# Will not list files created and deleted in the interium but it helps.
.printModifiedFiles <- function(dir_path, after_posixct){
file_list <- list.files(
path = dir_path,
full.names = TRUE
)

print(sprintf("FILE INFO FOR UPDATES AFTER %s", as.character(after_posixct)))
for (file in file_list){
info <- as.list(file.info(file))
if (info['mtime'] > after_posixct){
msg <- sprintf(
"%s %s %s %s",
file,
info['mtime'],
info['mode'],
info['isdir']
)
print(msg)
}
}
}
7 changes: 6 additions & 1 deletion tests/testthat/test-0-test-package-installation.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ test_that('Test packages installed correctly',{
, info = sprintf("Test package %s is not installed.", thisTestPkg)
)
}
})
})

# Record modifaction time of package directory to be checked at end of testing
# in test-Z-test-no-source-modifcations.R
tmp_pkgnet_path <- file.path(Sys.getenv('PKGNET_TEST_LIB'), 'pkgnet')
Sys.setenv(PKGNET_LATEST_MOD = as.character(file.info(tmp_pkgnet_path)$mtime))
21 changes: 21 additions & 0 deletions tests/testthat/test-Z-test-no-source-modifcations.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Inditended to be run last, this is to confirm that no files
# within the package directory are modified during the normal
# operation of pkgnet during these tests. This includes the
# creation and deletion of temp files for rendering.

test_that('No modification to pkgnet directory during testing',{
startModTime <- as.POSIXct(Sys.getenv('PKGNET_LATEST_MOD'))

tmp_pkgnet_path <- file.path(Sys.getenv('PKGNET_TEST_LIB'), 'pkgnet')
currentModTime <- file.info(tmp_pkgnet_path)$mtime

if (as.character(startModTime) != as.character(currentModTime)){
pkgnet:::.printModifiedFiles(tmp_pkgnet_path, startModTime)
}

expect_equal(object = currentModTime
, expected = startModTime
, info = "The source directory was modified during the execution of tests."
)

})

0 comments on commit 994723d

Please sign in to comment.