Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop configurability of global resources #48

Merged
merged 4 commits into from
Jul 26, 2023
Merged
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
8 changes: 8 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ jobs:
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

# This does take a minute or two to install, and we could cache
# it but that's not super easy while still allowing easy
# updating. Once things stabilise we might tag outpack server
# releases and then we can install and cache against that.
- name: setup server
run: |
cargo install --git https://github.com/mrc-ide/outpack_server --branch mrc-4364

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ jobs:
with:
use-public-rspm: true

- name: setup server
run: |
cargo install --git https://github.com/mrc-ide/outpack_server --branch mrc-4364

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
Expand Down
12 changes: 1 addition & 11 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ orderly_config_yml_read <- function(path) {

check <- list(
minimum_orderly_version = orderly_config_validate_minimum_orderly_version,
plugins = orderly_config_validate_plugins,
global_resources = orderly_config_validate_global_resources)
plugins = orderly_config_validate_plugins)

required <- "minimum_orderly_version"
optional <- setdiff(names(check), required)
Expand Down Expand Up @@ -51,15 +50,6 @@ orderly_config_validate_minimum_orderly_version <- function(value, filename) {
}


orderly_config_validate_global_resources <- function(global_resources,
filename) {
if (!is.null(global_resources)) {
assert_is_directory(global_resources, name = "Global resource directory")
global_resources
}
}


orderly_config_validate_plugins <- function(plugins, filename) {
if (is.null(plugins)) {
return(NULL)
Expand Down
20 changes: 11 additions & 9 deletions R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,9 @@ static_orderly_dependency <- function(args) {
##' Copy global resources into a packet directory. You can use this to
##' share common resources (data or code) between multiple packets.
##' Additional metadata will be added to keep track of where the files
##' came from. Using this function requires that the orderly
##' repository has global resources enabled, with a
##' `global_resources:` section in the `orderly_config.yml`; an error
##' will be raised if this is not configured.
##' came from. Using this function requires the global resources
##' directory `global/` exists at the orderly root; an error will be
##' raised if this is not configured when we attempt to fetch files.
##'
##' @title Copy global resources into a packet directory
##'
Expand Down Expand Up @@ -341,16 +340,19 @@ validate_global_resource <- function(args) {


copy_global <- function(path_root, path_dest, config, files) {
if (is.null(config$global_resources)) {
stop(paste("'global_resources' is not supported;",
"please edit orderly_config.yml to enable"),
call. = FALSE)
## This used to be configurable in orderly1, but almost everyone
## just kept it as 'global'. We might make it configurable later.
global_dir <- "global"
global_path <- file.path(path_root, global_dir)
if (!is_directory(global_path)) {
cli::cli_abort(sprintf(
"The global resources directory '%s' does not exist at orderly's root",
global_dir))
}

here <- names(files)
there <- unname(files)

global_path <- file.path(path_root, config$global_resources)
assert_file_exists(
there, workdir = global_path,
name = sprintf("Global resources in '%s'", global_path))
Expand Down
7 changes: 3 additions & 4 deletions man/orderly_global_resource.Rd

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

2 changes: 0 additions & 2 deletions tests/testthat/helper-orderly.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ test_prepare_orderly_example <- function(examples, ...) {
config <- readLines(file.path(tmp, "orderly_config.yml"))

if (any(c("global", "global-dir") %in% examples)) {
config <- c(config,
"global_resources: global")
fs::dir_create(file.path(tmp, "global"))
if ("global" %in% examples) {
fs::file_copy(test_path("examples/explicit/data.csv"),
Expand Down
23 changes: 0 additions & 23 deletions tests/testthat/test-config.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,6 @@ test_that("environment files must be really simple", {
})


test_that("can interpolate values into an orderly configuration", {
## This is a silly test because we'd never normally interpolate in
## the global resources - this is really to support databases via
## the plugin....
path <- test_prepare_orderly_example("global")
expect_equal(orderly_root(path, FALSE)$config$global_resources, "global")

writeLines(c(empty_config_contents(),
"global_resources: $PATH_GLOBAL"),
file.path(path, "orderly_config.yml"))
expect_error(
orderly_root(path, FALSE),
paste0("Environment variable 'PATH_GLOBAL' is not set\n\t",
"(used in orderly_config.yml$global_resources)"),
fixed = TRUE)
withr::with_envvar(
c(PATH_GLOBAL = "global"),
expect_equal(orderly_root(path, FALSE)$config$global_resources, "global"))
writeLines("PATH_GLOBAL: global", file.path(path, "orderly_envir.yml"))
expect_equal(orderly_root(path, FALSE)$config$global_resources, "global")
})


test_that("can validate minimum required version", {
expect_error(
orderly_config_validate_minimum_orderly_version("1.4.5", "orderly.yml"),
Expand Down
15 changes: 0 additions & 15 deletions tests/testthat/test-root.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,3 @@ test_that("can turn an outpack root into an orderly one", {
expect_equal(root2$config,
list(minimum_orderly_version = numeric_version("1.99.0")))
})


test_that("Can validate global resources", {
tmp <- tempfile()
on.exit(unlink(tmp, recursive = TRUE))
root <- orderly_init(tmp, logging_console = FALSE)
writeLines(c(empty_config_contents(),
"global_resources: global"),
file.path(tmp, "orderly_config.yml"))
expect_error(orderly_config(tmp),
"Global resource directory does not exist: 'global'")
dir.create(file.path(tmp, "global"))
cfg <- orderly_config(tmp)
expect_equal(cfg$global_resources, "global")
})
6 changes: 3 additions & 3 deletions tests/testthat/test-run.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ test_that("can validate global resource arguments", {

test_that("can't use global resources if not enabled", {
path <- test_prepare_orderly_example("global")
writeLines(empty_config_contents(), file.path(path, "orderly_config.yml"))
unlink(file.path(path, "global"), recursive = TRUE)
env <- new.env()
path_src <- file.path(path, "src", "global")
err <- expect_error(
orderly_run("global", root = path, envir = env),
"'global_resources' is not supported; please edit orderly_config.yml")
"The global resources directory 'global' does not exist at orderly's root")
expect_error(
withr::with_dir(path_src, sys.source("orderly.R", env)),
"'global_resources' is not supported; please edit orderly_config.yml")
"The global resources directory 'global' does not exist at orderly's root")
})


Expand Down
21 changes: 5 additions & 16 deletions vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ The function `orderly2::orderly_dependency()` is designed to operate while the p
* `orderly2::orderly_description()`: Provide a longer name and description for your report; this can be reflected in tooling that uses orderly metadata to be much more informative than your short name.
* `orderly2::orderly_parameters()`: Declares parameters that can be passed in to control the behaviour of the report. Parameters are key-value pairs of simple data (booleans, numbers, strings) which your report can respond to. They can also be used in queries to `orderly2::orderly_dependency()` to find packets that satisfy some criteria.
* `orderly2::orderly_resource()`: Declares that a file is a *resource*; a file that is an input to the the report, and which comes from this source directory. By default, orderly treats all files in the directory as a resource, but it can be useful to mark these explicitly, and necessary to do so in "strict mode" (see below). Files that have been marked as a resource are **immutable** and may not be deleted or modified.
* `orderly2::orderly_global_resource()`: Copies a file from a "global resources" directory, which can be data files or source code located at the root of the orderly repository. This can be a reasonable way of sharing data or commonly used code among several reports.
* `orderly2::orderly_global_resource()`: Copies a file from the "global resources" directory `global/`, which can be data files or source code located at the root of the orderly repository. This can be a reasonable way of sharing data or commonly used code among several reports.
* `orderly2::orderly_artefact()`: Declares that a file (or set of files) will be created by this report, before it is even run. Doing this makes it easier to check that the report behaves as expected and can allow reasoning about what a related set of reports will do without running them. By declaring something as an artefact (especially in conjunction with "strict mode") it is also easier to clean up `src` directories that have been used in interactive development (see below).
* `orderly2::orderly_dependency()`: Copy files from one packet into this packet as it runs, as seen above.
* `orderly2::orderly_strict_mode()`: Declares that this report will be run in "strict mode" (see below).
Expand Down Expand Up @@ -281,25 +281,14 @@ See the outpack query documentation for much more detail on this.

Sometimes it is useful to share data between different reports, for example some common source utilities that don't warrant their own package, or some common data.

To do this, you should set up a global resources directory by editing the `orderly_config.yml` file to say:

```{r, echo = FALSE, results = "asis"}
contents <- readLines(file.path(path, "orderly_config.yml"))
writeLines(c(
contents,
"global_resources: global"),
file.path(path, "orderly_config.yml"))
fs::dir_create(file.path(path, "global"))
write.csv(data.frame(x = 1:10, y = runif(10)),
file.path(path, "global/data.csv"))
yaml_output(readLines(file.path(path, "orderly_config.yml")))
```

and creating a directory `global` at the orderly root (you can use whatever name you want, the directory and the configuration option must match, and the directory must exist).
To do this, create a directory `global` at the orderly root and put in it any files or directories you might want to share.

Suppose our global directory contains a file `data.csv`:

```{r, echo = FALSE}
fs::dir_create(file.path(path, "global"))
write.csv(data.frame(x = 1:10, y = runif(10)),
file.path(path, "global/data.csv"))
dir_tree(path)
```

Expand Down