Skip to content

Commit

Permalink
Merge branch 'main' into mrc-4645
Browse files Browse the repository at this point in the history
  • Loading branch information
weshinsley authored Oct 19, 2023
2 parents 50d9377 + 522f200 commit c25d7b9
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: orderly2
Title: Orderly Next Generation
Version: 1.99.7
Version: 1.99.9
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Robert", "Ashton", role = "aut"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export(orderly_location_rename)
export(orderly_metadata)
export(orderly_metadata_extract)
export(orderly_metadata_read)
export(orderly_new)
export(orderly_parameters)
export(orderly_plugin_add_metadata)
export(orderly_plugin_context)
Expand Down
11 changes: 8 additions & 3 deletions R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ static_orderly_artefact <- function(args) {
##' @return Undefined
##' @export
orderly_dependency <- function(name, query, files) {
assert_scalar_character(name, call = environment())
if (!is.null(name)) {
assert_scalar_character(name, call = environment())
}

ctx <- orderly_context(rlang::caller_env())
subquery <- NULL
Expand Down Expand Up @@ -292,6 +294,9 @@ static_orderly_dependency <- function(args) {
query <- args$query
files <- args$files

static_name <- static_string(name)
has_name <- !is.null(static_name) || is.null(name)

name <- static_string(name)
files <- static_character_vector(files, TRUE)

Expand All @@ -304,10 +309,10 @@ static_orderly_dependency <- function(args) {
query <- NULL
}

if (is.null(name) || is.null(files) || is.null(query)) {
if (!has_name || is.null(files) || is.null(query)) {
return(NULL)
}
list(name = name, query = query, files = files)
list(name = static_name, query = query, files = files)
}


Expand Down
54 changes: 54 additions & 0 deletions R/orderly.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,57 @@ orderly_list_src <- function(root = NULL, locate = TRUE) {
pos <- fs::dir_ls(file.path(root_path, "src"), type = "directory")
basename(pos)[file_exists(file.path(pos, "orderly.R"))]
}


##' Create a new empty report.
##'
##' @title Create a new report
##'
##' @param name The name of the report
##'
##' @param template The template to use. The only acceptable values
##' for now are `NULL` (uses the built-in default) and `FALSE` which
##' suppresses any default content. We may support customisable
##' templates in future - let us know if this would be useful.
##'
##' @param force Create an `orderly.R` file within an existing
##' directory `src/<name>`; this may be useful if you have already
##' created the directory and some files first but want help
##' creating the orderly file.
##'
##' @inheritParams orderly_run
##'
##' @return Nothing, called for its side effects only
##' @export
orderly_new <- function(name, template = NULL, force = FALSE,
root = NULL, locate = TRUE) {
root <- root_open(root, locate, require_orderly = TRUE, call = environment())
dest <- file.path(root$path, "src", name)

if (file.exists(file.path(dest, "orderly.R"))) {
cli::cli_abort("'src/{name}/orderly.R' already exists")
}
if (file.exists(dest) && !fs::is_dir(dest)) {
cli::cli_abort(
c("'src/{name}' already exists, but is not a directory",
i = "This file really should not be here, you might need to tidy up"))
}
if (length(dir(dest, all.files = TRUE, no.. = TRUE)) > 0 && !force) {
cli::cli_abort(
c("'src/{name}/' already exists and contains files",
i = paste("If you want to add an orderly.R to this directory,",
"rerun {.code orderly_new()} with {.code force = TRUE}")))
}

if (is.null(template)) {
contents <- readLines(orderly2_file("template/default.R"))
} else if (isFALSE(template)) {
contents <- character()
} else {
cli::cli_abort("'template' must be 'NULL' or 'FALSE' for now")
}

fs::dir_create(dest)
writeLines(contents, file.path(dest, "orderly.R"))
cli::cli_alert_success("Created 'src/{name}/orderly.R'")
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ reference:
- orderly_location_rename
- title: Help for developing
contents:
- orderly_new
- orderly_cleanup
- orderly_cleanup_status
- orderly_gitignore_update
Expand Down
22 changes: 22 additions & 0 deletions inst/template/default.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This is an orderly script - edit it to suit your needs. You might include
#
# * orderly2::orderly_parameters():
# declare parameters that your report accepts
# * orderly2::orderly_description():
# describe your report with friendly names, descriptions and metadata
# * orderly2::orderly_resource():
# declare files in your source tree that are inputs
# * orderly2::orderly_shared_resource():
# use files from the root directory's 'shared/' directory
# * orderly2::orderly_dependency():
# use files from a previously-run packet
# * orderly2::orderly_artefact():
# declare files that you promise to produce, and describe them
# * orderly2::orderly_strict_mode():
# enable some optional checks
#
# See the docs for more information:
# https://mrc-ide.github.io/orderly2/reference/
#
# To generate templates without this header, pass template = FALSE to
# orderly_new(); this header can be safely deleted if you don't need it.
38 changes: 38 additions & 0 deletions man/orderly_new.Rd

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

81 changes: 81 additions & 0 deletions tests/testthat/test-orderly.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,84 @@ test_that("no candidates returns empty character vector", {
expect_equal(withr::with_dir(path, orderly_list_src()), character())
expect_equal(orderly_list_src(path), character())
})


test_that("can create empty orderly report", {
path <- test_prepare_orderly_example(character())
expect_message(
orderly_new("foo", root = path),
"Created 'src/foo/orderly.R'")
path_orderly <- file.path(path, "src", "foo", "orderly.R")
expect_true(file.exists(path_orderly))
txt <- readLines(path_orderly)
expect_match(txt[[1]], "This is an orderly script")
})


test_that("can create a totally blank orderly report", {
path <- test_prepare_orderly_example(character())
expect_message(
orderly_new("foo", template = FALSE, root = path),
"Created 'src/foo/orderly.R'")
path_orderly <- file.path(path, "src", "foo", "orderly.R")
expect_true(file.exists(path_orderly))
expect_equal(readLines(path_orderly), character())
})


test_that("error if orderly.R exists already", {
path <- test_prepare_orderly_example("data")
expect_error(orderly_new("data", root = path),
"'src/data/orderly.R' already exists")
expect_error(orderly_new("data", force = TRUE, root = path),
"'src/data/orderly.R' already exists")
})


test_that("error if a non-directory file is found in the src dir", {
path <- test_prepare_orderly_example(character())
file.create(file.path(path, "src", "foo"))
err <- expect_error(
orderly_new("foo", template = FALSE, root = path),
"'src/foo' already exists, but is not a directory")
expect_equal(
err$body,
c(i = "This file really should not be here, you might need to tidy up"))
})


test_that("allow creation of orderly.R in existing dir if force is given", {
path <- test_prepare_orderly_example(character())
fs::dir_create(file.path(path, "src", "foo"))
file.create(file.path(path, "src", "foo", "bar"))
err <- expect_error(
orderly_new("foo", root = path),
"'src/foo/' already exists and contains files")
expect_equal(
err$body,
c(i = paste("If you want to add an orderly.R to this directory,",
"rerun `orderly_new()` with `force = TRUE`")))
expect_message(
orderly_new("foo", force = TRUE, root = path),
"Created 'src/foo/orderly.R'")
expect_true(file.exists(file.path(path, "src/foo/orderly.R")))
})


test_that("allow creation of orderly.R in existing empty dir", {
path <- test_prepare_orderly_example(character())
fs::dir_create(file.path(path, "src", "foo"))
expect_message(
orderly_new("foo", root = path),
"Created 'src/foo/orderly.R'")
expect_true(file.exists(file.path(path, "src/foo/orderly.R")))
})


test_that("disallow template arguments", {
path <- test_prepare_orderly_example(character())
expect_error(
orderly_new("foo", template = "other", root = path),
"'template' must be 'NULL' or 'FALSE' for now")
expect_false(file.exists(file.path(path, "src/foo")))
})
3 changes: 3 additions & 0 deletions tests/testthat/test-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ test_that("read dependency", {
args <- list(name = "a", query = "latest", files = c(x = "y"))
expect_equal(static_orderly_dependency(args), args)

args <- list(name = NULL, query = "latest", files = c(x = "y"))
expect_equal(static_orderly_dependency(args), args)

expect_null(
static_orderly_dependency(list(name = quote(a),
query = "latest",
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-run.R
Original file line number Diff line number Diff line change
Expand Up @@ -1233,3 +1233,28 @@ test_that("nice error if resource file not found", {
expect_match(err$parent$body[[1]],
"Looked within directory '.+/src/explicit'")
})


test_that("can add a dependency on an id with no name", {
path <- test_prepare_orderly_example(c("data", "depends"))
envir1 <- new.env()
id1 <- orderly_run_quietly("data", root = path, envir = envir1)

path_src <- file.path(path, "src", "depends", "orderly.R")
code <- readLines(path_src)
i <- grep("orderly2::orderly_dependency", code)
code[[i]] <- sprintf(
"orderly2::orderly_dependency(NULL, '%s', c(input.rds = 'data.rds'))",
id1)
writeLines(code, path_src)
envir2 <- new.env()
id2 <- orderly_run_quietly("depends", root = path, envir = envir2)

meta <- orderly_metadata(id2, root = path)
expect_equal(
meta$depends,
data_frame(
packet = id1,
query = sprintf('single(id == "%s")', id1),
files = I(list(data_frame(here = "input.rds", there = "data.rds")))))
})

0 comments on commit c25d7b9

Please sign in to comment.