Skip to content

Commit

Permalink
pull root src from envvar
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Kusumgar committed Mar 1, 2024
1 parent 2cf7d79 commit 480bb02
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
28 changes: 14 additions & 14 deletions R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
##' depending on the configuration. (Later this will make more sense
##' once we support a "bare" outpack layout.)
##'
##' @section Manually setting report source directory:
##'
##' To manually set the report source directory, you will need to set
##' the path of the directory as the `ORDERLY_REPORT_SRC` environment
##' variable.
##'
##' @title Run a report
##'
##' @param name Name of the report to run. Any leading `./` `src/` or
Expand Down Expand Up @@ -136,12 +142,6 @@
##' then orderly looks in the working directory and up through its
##' parents until it finds an `.outpack` directory
##'
##' @param root_src Separately, the root of the orderly source tree,
##' if separate from the outpack root (given as `root`). This is
##' intended for running reports in situations where the source tree
##' is kept in a different place to the outpack root; see Details
##' for more information.
##'
##' @return The id of the created report (a string)
##'
##' @export
Expand All @@ -158,16 +158,16 @@
##' # and we can query the metadata:
##' orderly2::orderly_metadata_extract(name = "data", root = path)
orderly_run <- function(name, parameters = NULL, envir = NULL, echo = TRUE,
search_options = NULL, root = NULL, locate = TRUE,
root_src = NULL) {
if (is.null(root_src)) {
root <- root_open(root, locate, require_orderly = TRUE,
call = environment())
search_options = NULL, root = NULL, locate = TRUE) {
env_src <- Sys.getenv("ORDERLY_REPORT_SRC")
is_env_src_empty <- !nzchar(env_src)
root <- root_open(root, locate, require_orderly = is_env_src_empty,
call = environment())

if (is_env_src_empty) {
root_src <- root$path
} else {
root <- root_open(root, locate, require_orderly = FALSE,
call = environment())
root_src <- orderly_src_root(root_src, locate, call = environment())
root_src <- orderly_src_root(env_src, locate, call = environment())
}

name <- validate_orderly_directory(name, root_src, environment())
Expand Down
44 changes: 25 additions & 19 deletions tests/testthat/test-run-separate.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
test_that("can run simple case in separate directory", {
info <- test_prepare_orderly_example_separate("explicit")
id <- orderly_run_quietly("explicit", envir = new.env(),
root = info$outpack, root_src = info$src)
id <- withr::with_envvar(
c(ORDERLY_REPORT_SRC = info$src),
orderly_run_quietly("explicit", envir = new.env(), root = info$outpack)
)
expect_type(id, "character")
expect_true(file.exists(file.path(info$src, "draft")))
expect_false(file.exists(file.path(info$src, "archive")))
Expand All @@ -16,9 +18,10 @@ test_that("can run shared resources case in separate directory", {
## resources are relative to the *source* tree and not the outpack
## root.
info <- test_prepare_orderly_example_separate("shared")
envir <- new.env()
id <- orderly_run_quietly("shared", envir = envir,
root = info$outpack, root_src = info$src)
id <- withr::with_envvar(
c(ORDERLY_REPORT_SRC = info$src),
orderly_run_quietly("shared", envir = new.env(), root = info$outpack)
)
expect_setequal(
dir(file.path(info$outpack, "archive", "shared", id)),
c("shared_data.csv", "mygraph.png", "shared.R"))
Expand All @@ -28,13 +31,14 @@ test_that("can run shared resources case in separate directory", {
test_that("can use dependencies in separate directory", {
## Ensures that we hit the outpack root for pulling deps in
info <- test_prepare_orderly_example_separate(c("data", "depends"))
envir1 <- new.env()
id1 <- orderly_run_quietly("data", envir = envir1,
root = info$outpack, root_src = info$src)
envir2 <- new.env()
id2 <- orderly_run_quietly("depends", envir = envir2,
root = info$outpack, root_src = info$src)

id1 <- withr::with_envvar(
c(ORDERLY_REPORT_SRC = info$src),
orderly_run_quietly("data", envir = new.env(), root = info$outpack)
)
id2 <- withr::with_envvar(
c(ORDERLY_REPORT_SRC = info$src),
orderly_run_quietly("depends", envir = new.env(), root = info$outpack)
)
path1 <- file.path(info$outpack, "archive", "data", id1)
path2 <- file.path(info$outpack, "archive", "depends", id2)

Expand All @@ -48,8 +52,10 @@ test_that("can use dependencies in separate directory", {
test_that("can get git information in separate directory", {
info <- test_prepare_orderly_example_separate("explicit")
info$git <- helper_add_git(info$src)
id <- orderly_run_quietly("explicit", envir = new.env(),
root = info$outpack, root_src = info$src)
id <- withr::with_envvar(
c(ORDERLY_REPORT_SRC = info$src),
orderly_run_quietly("explicit", envir = new.env(), root = info$outpack)
)
meta <- orderly_metadata(id, root = info$outpack)
expect_mapequal(meta$git, info$git[c("sha", "branch", "url")])
})
Expand All @@ -59,13 +65,13 @@ test_that("can't run interactively in separate directory", {
## Picking on depends here because it really requires the outpack
## root
info <- test_prepare_orderly_example_separate(c("data", "depends"))
envir1 <- new.env()
id1 <- orderly_run_quietly("data", envir = envir1,
root = info$outpack, root_src = info$src)
envir2 <- new.env()
id1 <- withr::with_envvar(
c(ORDERLY_REPORT_SRC = info$src),
orderly_run_quietly("data", envir = new.env(), root = info$outpack)
)
path_src <- file.path(info$src, "src", "depends")
expect_error(
withr::with_dir(path_src,
sys.source("depends.R", envir2)),
sys.source("depends.R", new.env())),
"orderly directory '.+' not initialised")
})

0 comments on commit 480bb02

Please sign in to comment.