Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Fix issue where dependencies were not being tracked when checking produced files #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
^\.github$
\.*gcov$
^TODO\.md$
^.*\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions R/orderly.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ orderly_run <- function(name, parameters = NULL, envir = NULL,
outpack::outpack_packet_use_dependency(dat$depends$id[[i]],
dat$depends$files[[i]])
}
packet <- outpack::outpack_packet_current()
lapply(packet$depends, function(dependency) {
expected <- c(expected, dependency$files$here)
})

## TODO: if run fails we might not close out the device stack
## here, we do need to do that to make things easy for the user.
Expand Down
20 changes: 20 additions & 0 deletions orderly2.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
53 changes: 51 additions & 2 deletions tests/testthat/helper-orderly.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ test_prepare_orderly_example <- function(examples, ...) {
config <- c(config,
"global_resources: global")
fs::dir_create(file.path(tmp, "global"))
fs::file_copy("examples/minimal/data.csv", file.path(tmp, "global"))
fs::file_copy(test_path("examples/minimal/data.csv"),
file.path(tmp, "global"))
}

if ("plugin" %in% examples) {
Expand All @@ -29,7 +30,7 @@ test_prepare_orderly_example <- function(examples, ...) {
writeLines(config, file.path(tmp, "orderly_config.yml"))
fs::dir_create(file.path(tmp, "src"))
for (i in examples) {
fs::dir_copy(file.path("examples", i), file.path(tmp, "src"))
fs::dir_copy(test_path("examples", i), file.path(tmp, "src"))
}
tmp
}
Expand All @@ -54,3 +55,51 @@ json_to_df <- function(x) {
hash_file <- function(...) {
outpack:::hash_file(...)
}


test_path <- function(...) {
if (basename(getwd()) == "testthat") {
file.path(...)
} else {
testthat::test_path(...)
}
}


## Expects dependencies to be a list of name and id (or search query)
## e.g.
## list(a = "latest", b = "latest(parameter:x == 2)") # nolint
## By convention will expect report artefact to be "data.rds" and will
## map this to "input.rds" in the script.R
create_random_report <- function(root, name = "data", dependencies = NULL) {
report_dir <- fs::dir_create(file.path(root, "src", name))
withr::defer_parent(unlink(report_dir, recursive = TRUE))

yml <- c(
"script: script.R",
"artefacts:",
" data:",
" description: Random data",
" filenames: data.rds"
)

if (!is.null(dependencies)) {
assert_named(dependencies)
formatted_depends <- do.call(c, lapply(names(dependencies), function(name) {
id <- dependencies[[name]]
c(sprintf(" - %s:", name),
sprintf(" id: %s", id),
" use:",
" input.rds: data.rds")
}))
yml <- c(yml, "depends:", formatted_depends)
script <- c("x <- readRDS(\"input.rds\")",
"saveRDS(x + runif(10), \"data.rds\")")
} else {
script <- "saveRDS(runif(10), \"data.rds\")"
}

writeLines(yml, file.path(report_dir, "orderly.yml"))
writeLines(script, file.path(report_dir, "script.R"))
invisible(report_dir)
}
22 changes: 21 additions & 1 deletion tests/testthat/test-run.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test_that("Check that required files are produced", {
## TODO: we might add something nice to expose the status of a
## currently running packet.
expect_error(outpack::outpack_packet_end(),
"No current packet")
"No currently active packet")

## TODO: Still need something much nicer in outpack for this:
root <- orderly_root(path, FALSE)
Expand Down Expand Up @@ -207,3 +207,23 @@ test_that("can use global resources", {
list(list(here = "global_data.csv",
there = "data.csv")))
})


test_that("dependencies left in archive do not print extra files message", {
path <- test_prepare_orderly_example(NULL)
create_random_report(path, "a")
create_random_report(path, "b", list(a = "latest"))

id_a <- orderly_run("a", root = path)
expect_message(id_b <- orderly_run("b", root = path), NULL)

root <- orderly_root(path, FALSE)
meta <- root$outpack$metadata(id_b, full = TRUE)
expect_equal(meta$depends$packet, id_a)
expect_equal(meta$depends$files[[1]],
data_frame(here = "input.rds", there = "data.rds"))
expect_true(file.exists(
file.path(path, "archive", "b", id_b, "input.rds")))
expect_true(file.exists(
file.path(path, "archive", "b", id_b, "data.rds")))
})