From a4f1d2d3938d790fb27e12422ad36137329ffceb Mon Sep 17 00:00:00 2001 From: akselthomsen Date: Fri, 13 Dec 2024 15:27:48 +0100 Subject: [PATCH] feat: #62 possible to change execute directory of the quarto process with an option --- R/options.R | 5 +++++ R/whirl_r_session.R | 23 ++++++++++++++++++----- man/options.Rd | 18 ++++++++++++------ man/options_params.Rd | 2 ++ tests/testthat/test-run.R | 26 +++++++++++++++++++++++++- 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/R/options.R b/R/options.R index 9b9afab..5363f10 100644 --- a/R/options.R +++ b/R/options.R @@ -89,3 +89,8 @@ options::define_option( desc = "The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information see the examples of `run()` or `vignette('whirl')`." ) +options::define_option( + option = "execute_dir", + default = NULL, + desc = "The working directory of the process executing each script. Defeault us to execute R files from the working directory when calling `run()` and all other functions from the directory of the script. To change provide a character path (used for all scripts) or a function that takes the script as input and returns the execution directory." +) diff --git a/R/whirl_r_session.R b/R/whirl_r_session.R index 10d38c0..212715c 100644 --- a/R/whirl_r_session.R +++ b/R/whirl_r_session.R @@ -230,6 +230,24 @@ wrs_check_status <- function(self, private, super) { wrs_log_script <- function(script, self, private, super) { private$current_script <- script + # Set the execute directory of the Quarto process calling the script + quarto_execute_dir <- options::opt("execute_dir", env = "whirl") + if (is.null(quarto_execute_dir)) { + quarto_execute_dir <- switch( + get_file_ext(script), + "R" = getwd(), + normalizePath(dirname(script)) + ) + } else if (is.function(quarto_execute_dir)) { + quarto_execute_dir <- quarto_execute_dir(script) + } + + if (!file.exists(quarto_execute_dir)) { + cli::cli_abort("Script {.val {script}} cannot be run because execute directory {.val {quarto_execute_dir}} does not exist") + } + + # Execute the script + if (private$verbosity_level != "quiet") { private$pb <- pb_script$new( script = private$current_script, @@ -239,11 +257,6 @@ wrs_log_script <- function(script, self, private, super) { self$pb_update(status = "Running script") - quarto_execute_dir <- switch(get_file_ext(script), - "R" = getwd(), - normalizePath(dirname(script)) # TODO: Should this default be changed? - ) - self$call( func = \(...) quarto::quarto_render(...), args = list( diff --git a/man/options.Rd b/man/options.Rd index 3d036b1..c19abb8 100644 --- a/man/options.Rd +++ b/man/options.Rd @@ -20,6 +20,12 @@ options::opt(x, default, env = "whirl") \section{Options}{ \describe{ +\item{verbosity_level}{\describe{ +How chatty should the log be? Possibilities are \code{quiet}, \code{minimal} and \code{verbose}.\item{default: }{\preformatted{"verbose"}} +\item{option: }{whirl.verbosity_level} +\item{envvar: }{R_WHIRL_VERBOSITY_LEVEL (evaluated if possible, raw string otherwise)} +}} + \item{out_formats}{\describe{ Which log format(s) to produce. Possiblities are \code{html}, \code{json}, and markdown formats:\code{gfm}, \code{commonmark}, and \code{markua}.\item{default: }{\preformatted{"html"}} \item{option: }{whirl.out_formats} @@ -51,12 +57,6 @@ List of file naming patterns alway to be tracked when track_files = TRUE\item{de \item{envvar: }{R_WHIRL_TRACK_FILES_KEEP (as character vector, split on ';' delimiter)} }} -\item{verbosity_level}{\describe{ -How chatty should the log be? Possibilities are \code{quiet}, \code{minimal} and \code{verbose}.\item{default: }{\preformatted{"verbose"}} -\item{option: }{whirl.verbosity_level} -\item{envvar: }{R_WHIRL_VERBOSITY_LEVEL (evaluated if possible, raw string otherwise)} -}} - \item{approved_pkgs_folder}{\describe{ Approved folder library packages\item{default: }{\preformatted{NULL}} \item{option: }{whirl.approved_pkgs_folder} @@ -81,6 +81,12 @@ The output directory of the log files. Default is the folder of the excuted scri \item{envvar: }{R_WHIRL_LOG_DIR (evaluated if possible, raw string otherwise)} }} +\item{execute_dir}{\describe{ +The working directory of the process executing each script. Defeault us to execute R files from the working directory when calling \code{run()} and all other functions from the directory of the script. To change provide a character path (used for all scripts) or a function that takes the script as input and returns the execution directory.\item{default: }{\preformatted{NULL}} +\item{option: }{whirl.execute_dir} +\item{envvar: }{R_WHIRL_EXECUTE_DIR (evaluated if possible, raw string otherwise)} +}} + } } diff --git a/man/options_params.Rd b/man/options_params.Rd index 2d5f314..0f75215 100644 --- a/man/options_params.Rd +++ b/man/options_params.Rd @@ -10,6 +10,8 @@ \item{n_workers}{Number of simultanous workers used in the run function. A maximum of 128 workers is allowed. (Defaults to \code{1}, overwritable using option 'whirl.n_workers' or environment variable 'R_WHIRL_N_WORKERS')} +\item{execute_dir}{The working directory of the process executing each script. Defeault us to execute R files from the working directory when calling \code{run()} and all other functions from the directory of the script. To change provide a character path (used for all scripts) or a function that takes the script as input and returns the execution directory. (Defaults to \code{NULL}, overwritable using option 'whirl.execute_dir' or environment variable 'R_WHIRL_EXECUTE_DIR')} + \item{track_files_discards}{List of file naming patterns not be tracked when track_files = TRUE (Defaults to \verb{c("^/lib", "^/etc", "^/lib64", "^/usr", "^/var", "^/opt", "^/sys", ; "^/proc", "^/tmp", "^/null", "^/urandom", "^/.cache", .libPaths())}, overwritable using option 'whirl.track_files_discards' or environment variable 'R_WHIRL_TRACK_FILES_DISCARDS')} \item{track_files}{Should files read and written be tracked? Currently only supported on Linux. (Defaults to \code{FALSE}, overwritable using option 'whirl.track_files' or environment variable 'R_WHIRL_TRACK_FILES')} diff --git a/tests/testthat/test-run.R b/tests/testthat/test-run.R index ac9a8ac..0090d41 100644 --- a/tests/testthat/test-run.R +++ b/tests/testthat/test-run.R @@ -86,7 +86,6 @@ test_that("Run yaml config file", { }) - test_that("Change the log_dir to a path", { #Custom path custom_path <- withr::local_tempdir() @@ -120,3 +119,28 @@ test_that("Change the log_dir with a function", { expect_true() }) +test_that("Change the execute_dir to a path", { + + custom_path <- withr::local_tempdir() + withr::local_options(whirl.execute_dir = custom_path) + + test_script("success.R") |> + run() |> + expect_no_error() + + withr::local_options(whirl.execute_dir = "this/path/does/not/exist") + + test_script("success.R") |> + run() |> + expect_error() +}) + +test_that("Change the execute_dir to a function", { + + withr::local_options(whirl.execute_dir = \(x) dirname(x)) + + test_script("success.R") |> + run() |> + expect_no_error() +}) +