From 6d287dfc29b412bcd2527caa57c8d3eae42193d5 Mon Sep 17 00:00:00 2001 From: Jamie Gilbert Date: Fri, 12 May 2023 14:45:00 -0700 Subject: [PATCH] Tests and tidy up --- R/Module.R | 4 ++-- R/ModuleInstaller.R | 11 +++++++---- tests/testthat/test-ModuleInstaller.R | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/test-ModuleInstaller.R diff --git a/R/Module.R b/R/Module.R index 8d2773f9..91f18ba0 100644 --- a/R/Module.R +++ b/R/Module.R @@ -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( ) @@ -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) } \ No newline at end of file diff --git a/R/ModuleInstaller.R b/R/ModuleInstaller.R index cf4738a4..66e01663 100644 --- a/R/ModuleInstaller.R +++ b/R/ModuleInstaller.R @@ -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") @@ -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)) { @@ -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) @@ -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) diff --git a/tests/testthat/test-ModuleInstaller.R b/tests/testthat/test-ModuleInstaller.R new file mode 100644 index 00000000..93e443fc --- /dev/null +++ b/tests/testthat/test-ModuleInstaller.R @@ -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) +}) \ No newline at end of file