Skip to content

Commit

Permalink
Tests and tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
azimov committed May 12, 2023
1 parent de53edf commit 6d287df
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions R/Module.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#' Returns a list of module info for a given module
#'
loadModule <- function(moduleName = null, version = NULL, folder = NULL, instantiate = FALSE) {
available <- listAvailableModules()
available <- getAvailableModules()
module <- list(

)
Expand Down Expand Up @@ -60,7 +60,7 @@ moduleExec <- function(module, code, substituteVars = list(), useLocalStrategusL
tempScriptFile = tempfile(fileext = ".R"),
useLocalStrategusLibrary = useLocalStrategusLibrary,
job = job,
processName = paste(moduleFolder, "_renv_run"))
processName = paste(module$moduleFolder, "_renv_run"))

unlink(tempFile)
}
11 changes: 7 additions & 4 deletions R/ModuleInstaller.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ checkModuleDependencies <- function(dependencies, moduleSet) {
#' This returns the set of currently installed in the specifed system path
#'
#' @export
getAvailableModules <- function(installedModulesPath = Sys.getenv("STRATEGUS_INSTALLED_MODULES"),
refreshCache = FALSE) {
getAvailableModules <- function(installedModulesPath = Sys.getenv("STRATEGUS_INSTALLED_MODULES")) {

if (installedModulesPath == "") {
warning("No system module path set, set STRATEGUS_INSTALLED_MODULES in your .renviron to enable global modules")
Expand Down Expand Up @@ -74,6 +73,10 @@ installLocalModule <- function(pathToModule,
)

checkmate::assertFileExists(file.path(pathToModule, requiredFiles))

if (!dir.exists(installedModulesPath)) {
dir.create(installedModulesPath)
}
# lock rds object
installLockFile <- file.path(installedModulesPath, "install_module.lock")
if (file.exists(installLockFile)) {
Expand All @@ -82,7 +85,7 @@ installLocalModule <- function(pathToModule,
}

# Save a timestamp inside a file to inform when last lock was
a <- writeLines(timestamp(), con = installLockFile)
writeLines(timestamp(quiet = TRUE), con = installLockFile)
on.exit(unlink(installLockFile, force = TRUE))
currentModules <- getAvailableModules(installedModulesPath = installedModulesPath)
metaData <- getModuleMetaData(pathToModule)
Expand Down Expand Up @@ -119,7 +122,7 @@ installLocalModule <- function(pathToModule,
# Write to install path
modulesRdsFile <- file.path(installedModulesPath, "installedModules.rds")
# save rds
saveRDS(currentModules, modulesRdsFile)
saveRDS(currentModules %>% dplyr::distinct(), modulesRdsFile)

message(paste("Module", iRow$moduleName, "Installed"))
invisible(NULL)
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-ModuleInstaller.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
test_that("Modules install from github", {
installedModulesPath <- tempfile()
unlink(installedModulesPath, recursive = TRUE)
on.exit(unlink(installedModulesPath, recursive = TRUE))

installGithubModule("OHDSI/CohortGeneratorModule", installedModulesPath = installedModulesPath)
checkmate::expect_directory_exists(file.path(installedModulesPath, "/CohortGeneratorModule"))
expect_error(installGithubModule("OHDSI/CohortGeneratorModule", installedModulesPath = installedModulesPath))

checkmate::expect_data_frame(getAvailableModules(installedModulesPath))

installGithubModule("OHDSI/CohortGeneratorModule", installedModulesPath = installedModulesPath, overwrite = TRUE)
installGithubModule("OHDSI/CohortMethodModule", installedModulesPath = installedModulesPath)
checkmate::expect_directory_exists(file.path(installedModulesPath, "CohortGeneratorModule"))

# Should not be able to install if lock file is present
installLockFile <- file.path(installedModulesPath, "install_module.lock")
writeLines(timestamp(quiet = TRUE), con = installLockFile)

expect_error(installGithubModule("OHDSI/CohortGeneratorModule", installedModulesPath = installedModulesPath, overwrite = TRUE))
unlink(installLockFile)
})

0 comments on commit 6d287df

Please sign in to comment.