Skip to content

Commit

Permalink
Start running separately
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Sep 21, 2023
1 parent 7920c56 commit 2d74ed6
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 14 deletions.
44 changes: 40 additions & 4 deletions R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@
##' functionality was never available in orderly version 1, though
##' we had intended to support it.
##'
##' @section Running with a source tree separate from outpack root:
##'
##' Sometimes it is useful to run things from a different place on
##' disk to your outpack root. We know of two cases where this has
##' come up:
##'
##' * when running reports within a runner on a server, we make a
##' clean clone of the source tree at a particular git reference
##' into a new temporary directory and then run the report there,
##' but have it insert into an orderly repo at a fixed and
##' non-temporary location.
##' * we have a user for whom it is more convenient torun their report
##' on a hard drive but store the archive and metadata on a (larger)
##' shared drive.
##'
##' In the first instance, we have a source path at `<src>` which
##' contains the file `orderly_config.yml` and the directory `src/`
##' with our source reports, and a separate path `<root>` which
##' contains the directory `.outpack/` with all the metadata - it
##' may also have an unpacked archive, and a `.git/` directory
##' depending on the configuration. (Later this will make more sense
##' once we support a "bare" outpack layout.)
##'
##' @title Run a report
##'
##' @param name Name of the report to run. Any leading `./` `src/` or
Expand Down Expand Up @@ -113,6 +136,12 @@
##' 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 @@ -129,10 +158,17 @@
##' # 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 <- root_open(root, locate, require_orderly = TRUE,
call = environment())
root_src <- root$path
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())
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())
}

name <- validate_orderly_directory(name, root_src, environment())

Expand Down
35 changes: 34 additions & 1 deletion man/orderly_run.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 33 additions & 9 deletions tests/testthat/helper-orderly.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,42 @@ options(outpack.schema_validate =
test_prepare_orderly_example <- function(examples, ...) {
tmp <- tempfile()
withr::defer_parent(unlink(tmp, recursive = TRUE))
suppressMessages(orderly_init(tmp))
config <- readLines(file.path(tmp, "orderly_config.yml"))
suppressMessages(orderly_init(tmp, ...))
copy_examples(examples, tmp)
tmp
}


test_prepare_orderly_example_separate <- function(examples, ...) {
tmp <- tempfile()
withr::defer_parent(unlink(tmp, recursive = TRUE))

path_outpack <- file.path(tmp, "outpack")
suppressMessages(orderly_init(path_outpack, ...))

path_src <- file.path(tmp, "src")
copy_examples(examples, path_src)

list(src = path_src, outpack = path_outpack)
}


copy_examples <- function(examples, path_src) {
if (file.exists(path_src)) {
config <- readLines(file.path(path_src, "orderly_config.yml"))
} else {
config <- empty_config_contents()
}

fs::dir_create(path_src)
if (any(c("shared", "shared-dir") %in% examples)) {
fs::dir_create(file.path(tmp, "shared"))
fs::dir_create(file.path(path_src, "shared"))
if ("shared" %in% examples) {
fs::file_copy(test_path("examples/explicit/data.csv"),
file.path(tmp, "shared"))
file.path(path_src, "shared"))
}
if ("shared-dir" %in% examples) {
fs::dir_create(file.path(tmp, "shared", "data"))
fs::dir_create(file.path(path_src, "shared", "data"))
}
}

Expand All @@ -28,13 +53,12 @@ test_prepare_orderly_example <- function(examples, ...) {
" distribution:",
" normal")
}
writeLines(config, file.path(path_src, "orderly_config.yml"))

writeLines(config, file.path(tmp, "orderly_config.yml"))
fs::dir_create(file.path(tmp, "src"))
fs::dir_create(file.path(path_src, "src"))
for (i in examples) {
fs::dir_copy(test_path("examples", i), file.path(tmp, "src"))
fs::dir_copy(test_path("examples", i), file.path(path_src, "src"))
}
tmp
}


Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-run-separate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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)
expect_type(id, "character")
expect_true(file.exists(file.path(info$src, "draft")))
expect_false(file.exists(file.path(info$src, "archive")))
expect_false(file.exists(file.path(info$outpack, "draft")))
expect_true(file.exists(file.path(info$outpack, "archive")))
expect_true(file.exists(file.path(info$outpack, "archive", "explicit", id)))
})

0 comments on commit 2d74ed6

Please sign in to comment.