diff --git a/DESCRIPTION b/DESCRIPTION index 81b9d81..0829fc0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -48,8 +48,6 @@ Imports: sessioninfo (>= 1.2), tibble, digest, - lifecycle, - methods, yaml Suggests: testthat (>= 3.0.0), diff --git a/NEWS.md b/NEWS.md index 2a34226..65ed26e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,6 @@ # logrx (development version) ## New Features - - Added `error_on_unapproved` parameter to `axecute()` to optionally stop execution if unapproved packages are detected before running the script (#204) - Parses the script's AST to detect packages from `library()`/`require()` calls @@ -19,7 +18,7 @@ - `yaml` has been moved from `Suggests` to `Imports` ## Updates - +- Removed the `remove_log_object` argument from `log_write()` (#298). - Improved error messaging when a log.rx environment already exists (#276) - In non-interactive mode, provides clear guidance on how to resolve the issue - In interactive mode, offers users the option to automatically remove the existing environment or handle it manually diff --git a/R/axecute.R b/R/axecute.R index 15c414d..5858666 100644 --- a/R/axecute.R +++ b/R/axecute.R @@ -68,24 +68,18 @@ #' close(fileConn) #' #' axecute(file.path(dir, "hello.Rmd")) -axecute <- function(file, - log_name = NA, - log_path = NA, - include_rds = FALSE, - quit_on_error = TRUE, - to_report = c("messages", "output", "result"), - show_repo_url = FALSE, - error_on_unapproved = FALSE, - extra_info = NA, - ...) { - # deprecations - if (methods::hasArg(remove_log_object)) { - lifecycle::deprecate_stop("0.3.0", "axecute(remove_log_object = )", "axecute(include_rds = )") - } - - # remove log object - remove_log_object <- TRUE - +axecute <- function( + file, + log_name = NA, + log_path = NA, + include_rds = FALSE, + quit_on_error = TRUE, + to_report = c("messages", "output", "result"), + show_repo_url = FALSE, + error_on_unapproved = FALSE, + extra_info = NA, + ... +) { # lower everything for consistency and check values to_report <- map_chr(to_report, tolower) match.arg(to_report, several.ok = TRUE) @@ -104,18 +98,25 @@ axecute <- function(file, if (!is.null(unapproved_pkgs)) { set_log_element("hash_sum", digest::sha1(readLines(file, warn = FALSE))) set_log_element("session_info", get_session_info()) - set_log_element("errors", list(message = paste0( - "Unapproved packages detected prior to execution: ", - paste(unapproved_pkgs, collapse = ", ") - ))) - set_log_element("unapproved_packages_functions", data.frame( - function_name = "", - library = paste0("package:", unapproved_pkgs), - stringsAsFactors = FALSE - )) + set_log_element( + "errors", + list( + message = paste0( + "Unapproved packages detected prior to execution: ", + paste(unapproved_pkgs, collapse = ", ") + ) + ) + ) + set_log_element( + "unapproved_packages_functions", + data.frame( + function_name = "", + library = paste0("package:", unapproved_pkgs), + stringsAsFactors = FALSE + ) + ) log_write( file = file, - remove_log_object = TRUE, show_repo_url = show_repo_url, include_rds = include_rds, to_report = character(0), @@ -138,7 +139,6 @@ axecute <- function(file, # write log log_write( file = file, - remove_log_object = remove_log_object, show_repo_url = show_repo_url, include_rds = include_rds, to_report = to_report, diff --git a/R/log.R b/R/log.R index 75f11dd..64f00c3 100644 --- a/R/log.R +++ b/R/log.R @@ -118,10 +118,12 @@ log_init <- function() { #' #' # Write the log #' log_write(file) -log_config <- function(file = NA, - log_name = NA, - log_path = NA, - extra_info = NA) { +log_config <- function( + file = NA, + log_name = NA, + log_path = NA, + extra_info = NA +) { # If the log.rx environment is not NULL or empty, handle it appropriately if (!is.null(getOption("log.rx"))) { if (!(identical(ls(getOption("log.rx")), character(0)))) { @@ -233,8 +235,6 @@ log_cleanup <- function() { #' @param file String. Path to file executed #' @param include_rds Boolean. Option to export log object as Rds file. #' Defaults to FALSE -#' @param remove_log_object Boolean. Should the log object be removed after -#' writing the log file? Defaults to TRUE #' @param to_report String vector. Objects to optionally report; additional #' information in \code{\link{axecute}} #' @param show_repo_url Boolean. Should the repo URLs be reported @@ -262,12 +262,13 @@ log_cleanup <- function() { #' #' # Write the log #' log_write(file) -log_write <- function(file = NA, - remove_log_object = TRUE, - show_repo_url = FALSE, - include_rds = FALSE, - extra_info = NA, - to_report = c("messages", "output", "result")) { +log_write <- function( + file = NA, + show_repo_url = FALSE, + include_rds = FALSE, + extra_info = NA, + to_report = c("messages", "output", "result") +) { # Set end time and run time set_log_element("end_time", strftime(Sys.time(), usetz = TRUE)) set_log_element( @@ -305,7 +306,6 @@ log_write <- function(file = NA, write_hash_sum() ) - cleaned_log_vec <- c( cleaned_log_vec, write_log_header("Session Information"), @@ -340,7 +340,9 @@ log_write <- function(file = NA, write_used_functions() ) - cleaned_log <- cleaned_log[!(names(cleaned_log)) %in% "used_packages_functions"] + cleaned_log <- cleaned_log[ + !(names(cleaned_log)) %in% "used_packages_functions" + ] } if (file.exists(getOption("log.rx.approved"))) { @@ -354,7 +356,10 @@ log_write <- function(file = NA, } else { readRDS(approved_path) } - unapproved_functions <- get_unapproved_use(used_functions, approved_functions) + unapproved_functions <- get_unapproved_use( + used_functions, + approved_functions + ) set_log_element("unapproved_packages_functions", unapproved_functions) } @@ -364,7 +369,9 @@ log_write <- function(file = NA, write_unapproved_functions() ) - cleaned_log <- cleaned_log[!(names(cleaned_log)) %in% "unapproved_packages_functions"] + cleaned_log <- cleaned_log[ + !(names(cleaned_log)) %in% "unapproved_packages_functions" + ] } if ("lint_results" %in% names(log_cleanup())) { @@ -433,7 +440,8 @@ log_write <- function(file = NA, write_log_element("log_name", "Log name: "), write_log_element("log_path", "Log path: ") ) - writeLines(cleaned_log_vec, + writeLines( + cleaned_log_vec, con = file.path( get_log_element("log_path"), get_log_element("log_name") @@ -441,9 +449,18 @@ log_write <- function(file = NA, ) if (include_rds) { rds_fields <- c( - "end_time", "start_time", "run_time", "user", "hash_sum", - "log_path", "log_name", "file_path", "file_name", - "unapproved_packages_functions", "errors", "warnings", + "end_time", + "start_time", + "run_time", + "user", + "hash_sum", + "log_path", + "log_name", + "file_path", + "file_name", + "unapproved_packages_functions", + "errors", + "warnings", "session_info" ) log_options <- as.list(getOption("log.rx")) @@ -460,18 +477,20 @@ log_write <- function(file = NA, } } ) - saveRDS(cleaned_log_list, + saveRDS( + cleaned_log_list, file = file.path( get_log_element("log_path"), - paste0(tools::file_path_sans_ext( - get_log_element("log_name") - ), ".Rds") + paste0( + tools::file_path_sans_ext( + get_log_element("log_name") + ), + ".Rds" + ) ) ) } - if (remove_log_object) { - log_remove() - } + log_remove() } diff --git a/man/axecute.Rd b/man/axecute.Rd index 22c4c61..0d6331f 100644 --- a/man/axecute.Rd +++ b/man/axecute.Rd @@ -44,7 +44,7 @@ Defaults to FALSE} \item{error_on_unapproved}{Boolean. Should execution be stopped if unapproved packages are detected before running the script? Requires an approved list to be configured via \code{options(log.rx.approved)}. Defaults to FALSE. -See the \href{https://pharmaverse.github.io/logrx/articles/approved.html}{Logging Unapproved Package and Function Use vignette} +See the \href{https://pharmaverse.github.io/logrx/articles/approved.html#stopping-execution-on-unapproved-packages}{Logging Unapproved Package and Function Use vignette} for details on setting up an approved package list.} \item{extra_info}{List. Objects to optionally add on to end of log diff --git a/man/log_write.Rd b/man/log_write.Rd index 76687e7..60b38a0 100644 --- a/man/log_write.Rd +++ b/man/log_write.Rd @@ -6,7 +6,6 @@ \usage{ log_write( file = NA, - remove_log_object = TRUE, show_repo_url = FALSE, include_rds = FALSE, extra_info = NA, @@ -16,9 +15,6 @@ log_write( \arguments{ \item{file}{String. Path to file executed} -\item{remove_log_object}{Boolean. Should the log object be removed after -writing the log file? Defaults to TRUE} - \item{show_repo_url}{Boolean. Should the repo URLs be reported Defaults to FALSE} diff --git a/tests/testthat/test-log.R b/tests/testthat/test-log.R index 0e633aa..48e31d7 100644 --- a/tests/testthat/test-log.R +++ b/tests/testthat/test-log.R @@ -2,12 +2,17 @@ test_that("log_write reads approved list from yaml", { fp <- test_path("ref", "ex1.R") log_out <- tempfile(fileext = ".log") - withr::local_options(list(log.rx.approved = test_path("ref", "approved_example2.yaml"))) + withr::local_options(list( + log.rx.approved = test_path("ref", "approved_example2.yaml") + )) axecute(fp, log_path = dirname(log_out), log_name = basename(log_out)) print(readLines(log_out)) - expect_true(any(grepl("No unapproved packages or functions used", readLines(log_out)))) + expect_true(any(grepl( + "No unapproved packages or functions used", + readLines(log_out) + ))) # Clean up log_remove() @@ -24,7 +29,10 @@ test_that("log_write reads approved list from rds", { print(readLines(log_out)) - expect_true(any(grepl("No unapproved packages or functions used", readLines(log_out)))) + expect_true(any(grepl( + "No unapproved packages or functions used", + readLines(log_out) + ))) # Clean up log_remove() @@ -43,16 +51,39 @@ test_that("log_config configures the log and all the necessary elements", { expect_setequal( names(getOption("log.rx")), c( - "metadata", "session_info", "warnings", "errors", "messages", - "result", "output", "start_time", "end_time", "run_time", - "file_name", "file_path", "user", "hash_sum", "masked_functions", - "used_packages_functions", "unapproved_packages_functions", - "lint_results", "log_name", "log_path", "repo_urls", "extra_info" + "metadata", + "session_info", + "warnings", + "errors", + "messages", + "result", + "output", + "start_time", + "end_time", + "run_time", + "file_name", + "file_path", + "user", + "hash_sum", + "masked_functions", + "used_packages_functions", + "unapproved_packages_functions", + "lint_results", + "log_name", + "log_path", + "repo_urls", + "extra_info" ) ) - expect_identical(getOption("log.rx")[["file_path"]], dirname(get_file_path("./test-get.R"))) - expect_identical(getOption("log.rx")[["file_name"]], basename(get_file_path("./test-get.R"))) + expect_identical( + getOption("log.rx")[["file_path"]], + dirname(get_file_path("./test-get.R")) + ) + expect_identical( + getOption("log.rx")[["file_name"]], + basename(get_file_path("./test-get.R")) + ) expect_identical(getOption("log.rx")[["user"]], Sys.info()[["user"]]) }) @@ -175,11 +206,28 @@ test_that("log_config works after environment is removed interactively", { expect_setequal( names(getOption("log.rx")), c( - "metadata", "session_info", "warnings", "errors", "messages", - "result", "output", "start_time", "end_time", "run_time", - "file_name", "file_path", "user", "hash_sum", "masked_functions", - "used_packages_functions", "unapproved_packages_functions", - "lint_results", "log_name", "log_path", "repo_urls", "extra_info" + "metadata", + "session_info", + "warnings", + "errors", + "messages", + "result", + "output", + "start_time", + "end_time", + "run_time", + "file_name", + "file_path", + "user", + "hash_sum", + "masked_functions", + "used_packages_functions", + "unapproved_packages_functions", + "lint_results", + "log_name", + "log_path", + "repo_urls", + "extra_info" ) ) }, @@ -199,3 +247,15 @@ test_that("log_remove removes a log if one exists", { log_remove() expect_null(getOption("log.rx")) }) + +test_that("log_write removes the log object after writing", { + options("log.rx" = NULL) + fp <- test_path("ref", "ex1.R") + + log_config(fp) + logrx:::run_safely_loudly(fp) + + log_write(fp) + + expect_null(getOption("log.rx")) +}) diff --git a/tests/testthat/test-sha1.R b/tests/testthat/test-sha1.R index cbb9ece..5216fe5 100644 --- a/tests/testthat/test-sha1.R +++ b/tests/testthat/test-sha1.R @@ -7,7 +7,10 @@ test_that("Test 1: File HashSum does not change for ex1.R", { flines <- readLines(con) close(con) - expect_true(any(grepl("File HashSum: 69bba3ee00038e547f0a95b6b62d16f9a56a946b", flines) == TRUE)) + expect_true(any( + grepl("File HashSum: 69bba3ee00038e547f0a95b6b62d16f9a56a946b", flines) == + TRUE + )) # remove all the stuff we added rm(scriptPath, logDir) @@ -29,16 +32,21 @@ test_that("Test 2: File HashSum generated for temp file", { log_config(scriptPath, log_name = "log_out_report", log_path = logDir) logrx:::run_safely_loudly(scriptPath) - log_write(scriptPath, remove_log_object = FALSE, to_report = c("messages", "result")) + log_write( + scriptPath, + to_report = c("messages", "result") + ) con <- file(file.path(logDir, "log_out_report"), "r") flines <- readLines(con) close(con) - expect_true(any(grepl("File HashSum: 1a850097971365b8846fd7a935cc792896e97fbe", flines) == TRUE)) + expect_true(any( + grepl("File HashSum: 1a850097971365b8846fd7a935cc792896e97fbe", flines) == + TRUE + )) # remove all the stuff we added rm(scriptPath, logDir) - log_remove() }) test_that("Test 3: Different File HashSum generated for similar temp file with slight change", { @@ -56,17 +64,21 @@ test_that("Test 3: Different File HashSum generated for similar temp file with s log_config(scriptPath, log_name = "log_out_report", log_path = logDir) logrx:::run_safely_loudly(scriptPath) - log_write(scriptPath, remove_log_object = FALSE, to_report = c("messages", "result")) - + log_write( + scriptPath, + to_report = c("messages", "result") + ) con <- file(file.path(logDir, "log_out_report"), "r") flines <- readLines(con) close(con) - expect_true(any(grepl("File HashSum: 1a850097971365b8846fd7a935cc792896e97fbe", flines) != TRUE)) + expect_true(any( + grepl("File HashSum: 1a850097971365b8846fd7a935cc792896e97fbe", flines) != + TRUE + )) # remove all the stuff we added rm(scriptPath, logDir) - log_remove() }) test_that("Test 4: Same File HashSum generated for temp file in Test 2", { @@ -84,14 +96,19 @@ test_that("Test 4: Same File HashSum generated for temp file in Test 2", { log_config(scriptPath, log_name = "log_out_report", log_path = logDir) logrx:::run_safely_loudly(scriptPath) - log_write(scriptPath, remove_log_object = FALSE, to_report = c("messages", "result")) + log_write( + scriptPath, + to_report = c("messages", "result") + ) con <- file(file.path(logDir, "log_out_report"), "r") flines <- readLines(con) close(con) - expect_true(any(grepl("File HashSum: 1a850097971365b8846fd7a935cc792896e97fbe", flines) == TRUE)) + expect_true(any( + grepl("File HashSum: 1a850097971365b8846fd7a935cc792896e97fbe", flines) == + TRUE + )) # remove all the stuff we added rm(scriptPath, logDir) - log_remove() }) diff --git a/vignettes/approved.Rmd b/vignettes/approved.Rmd index a4f1768..ab2814a 100644 --- a/vignettes/approved.Rmd +++ b/vignettes/approved.Rmd @@ -209,7 +209,15 @@ close(fileConn) fp <- file.path(dir, "mpg.R") log_config(fp) logrx:::run_safely_loudly(fp) -log_write(fp, remove_log_object = FALSE) + +log_file <- file.path( + logrx:::get_log_element("log_path"), + logrx:::get_log_element("log_name") +) + +log_write(fp) + +log_lines <- readLines(log_file, warn = FALSE) ``` Here we have the log elements for "Used Package and Functions" and @@ -219,25 +227,10 @@ include the base library or tidyr functions in our approved list, so this has been logged! ```{r echo=FALSE} -cleaned_log_vec <- c() - -cleaned_log_vec <- c( - cleaned_log_vec, - write_log_header("Used Package and Functions"), - write_used_functions() -) - -cleaned_log_vec <- c( - cleaned_log_vec, - write_log_header("Unapproved Package and Functions"), - write_unapproved_functions() -) -cat(cleaned_log_vec, sep = "\n") +cat(log_lines, sep = "\n") ``` ```{r cleanup, echo = FALSE} -logrx::log_remove() - unlink(dir, recursive = TRUE) ```