Skip to content

Commit

Permalink
unit test to confirm no source dir modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
bburns632 committed May 18, 2024
1 parent 390434b commit 8624a45
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
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)
}
}
}
5 changes: 5 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ if(identical(Sys.getenv("NOT_CRAN"), "true")){

}

# 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))

# This withr statement should be redundant.
# This is within a test environment in which .libpaths() has been altered to include PKGNET_TEST_LIB.
# Yet, it appears to be necessary.
Expand Down
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 8624a45

Please sign in to comment.