Skip to content

Commit 994723d

Browse files
authored
Move rmarkdown::render action to happen in a temp directory (#330)
1 parent cff4610 commit 994723d

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
## CHANGES
55

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

89
# pkgnet 0.5.0
910
## NEW FEATURES

R/CreatePackageReport.R

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,28 @@ PackageReport <- R6::R6Class(
6767
, render_report = function() {
6868
log_info("Rendering package report...")
6969

70+
# copy Rmd files to temp directory to avoid writing interim files to package repo
71+
tmp_dir <- tempfile("package_report") # subdir within temp directory
72+
tmp_package_report_rmd <- file.path(tmp_dir, "package_report.Rmd")
73+
74+
orig_package_report_rmd <- system.file(file.path("package_report", "package_report.Rmd"), package = "pkgnet")
75+
orig_files <- list.files(dirname(orig_package_report_rmd), full.names = TRUE)
76+
77+
dir.create(tmp_dir)
78+
file.copy(
79+
from = orig_files,
80+
to = tmp_dir,
81+
recursive = TRUE,
82+
overwrite = TRUE
83+
)
84+
85+
# render report
7086
rmarkdown::render(
71-
input = system.file(
72-
file.path("package_report", "package_report.Rmd")
73-
, package = "pkgnet"
74-
)
87+
input = tmp_package_report_rmd
7588
, output_dir = dirname(self$report_path)
7689
, output_file = basename(self$report_path)
90+
, intermediates_dir = tmp_dir
91+
, knit_root_dir = tmp_dir
7792
, quiet = TRUE
7893
, params = list(
7994
reporters = private$reporters
@@ -86,6 +101,9 @@ PackageReport <- R6::R6Class(
86101
, sprintf("It is available at %s", self$report_path)
87102
))
88103

104+
# Clean up tmp
105+
unlink(tmp_dir, recursive = TRUE)
106+
89107
# If suppress flag is unset, then env variable will be emptry string ""
90108
if (identical(Sys.getenv("PKGNET_SUPPRESS_BROWSER"), "")) {
91109
utils::browseURL(self$report_path)

R/testing_utils.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,29 @@
104104

105105
return(invisible(TRUE))
106106
}
107+
108+
109+
# Get Files with Modification Date after a specific date
110+
# Used to provide more verbose logging in the event package directory is modified.
111+
# Will not list files created and deleted in the interium but it helps.
112+
.printModifiedFiles <- function(dir_path, after_posixct){
113+
file_list <- list.files(
114+
path = dir_path,
115+
full.names = TRUE
116+
)
117+
118+
print(sprintf("FILE INFO FOR UPDATES AFTER %s", as.character(after_posixct)))
119+
for (file in file_list){
120+
info <- as.list(file.info(file))
121+
if (info['mtime'] > after_posixct){
122+
msg <- sprintf(
123+
"%s %s %s %s",
124+
file,
125+
info['mtime'],
126+
info['mode'],
127+
info['isdir']
128+
)
129+
print(msg)
130+
}
131+
}
132+
}

tests/testthat/test-0-test-package-installation.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ test_that('Test packages installed correctly',{
1212
, info = sprintf("Test package %s is not installed.", thisTestPkg)
1313
)
1414
}
15-
})
15+
})
16+
17+
# Record modifaction time of package directory to be checked at end of testing
18+
# in test-Z-test-no-source-modifcations.R
19+
tmp_pkgnet_path <- file.path(Sys.getenv('PKGNET_TEST_LIB'), 'pkgnet')
20+
Sys.setenv(PKGNET_LATEST_MOD = as.character(file.info(tmp_pkgnet_path)$mtime))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Inditended to be run last, this is to confirm that no files
2+
# within the package directory are modified during the normal
3+
# operation of pkgnet during these tests. This includes the
4+
# creation and deletion of temp files for rendering.
5+
6+
test_that('No modification to pkgnet directory during testing',{
7+
startModTime <- as.POSIXct(Sys.getenv('PKGNET_LATEST_MOD'))
8+
9+
tmp_pkgnet_path <- file.path(Sys.getenv('PKGNET_TEST_LIB'), 'pkgnet')
10+
currentModTime <- file.info(tmp_pkgnet_path)$mtime
11+
12+
if (as.character(startModTime) != as.character(currentModTime)){
13+
pkgnet:::.printModifiedFiles(tmp_pkgnet_path, startModTime)
14+
}
15+
16+
expect_equal(object = currentModTime
17+
, expected = startModTime
18+
, info = "The source directory was modified during the execution of tests."
19+
)
20+
21+
})

0 commit comments

Comments
 (0)