Skip to content

Commit

Permalink
Rename and reorganise
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Sep 29, 2023
1 parent 6f7b60b commit 232b9fe
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 38 deletions.
4 changes: 2 additions & 2 deletions R/config.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
orderly_config_read <- function(path, call = NULL) {
filename <- file.path(path, "orderly_config.yml")
assert_file_exists2(basename(filename), workdir = path,
name = "Orderly configuration", call)
assert_file_exists_relative(basename(filename), workdir = path,
name = "Orderly configuration", call = call)
raw <- yaml_read(filename)

if (!is.null(raw)) {
Expand Down
12 changes: 6 additions & 6 deletions R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ static_orderly_description <- function(args) {
orderly_resource <- function(files) {
p <- get_active_packet()
src <- if (is.null(p)) "." else p$orderly2$src
assert_file_exists2(files, workdir = src, name = "Resource file",
call = environment())
assert_file_exists_relative(files, workdir = src, name = "Resource file",
call = environment())
files_expanded <- expand_dirs(files, src)
if (!is.null(p)) {
if (p$orderly2$strict$enabled) {
Expand All @@ -161,8 +161,8 @@ orderly_resource <- function(files) {
## Above we're looking in the underlying source directory, here
## we're looking within the running directory; it's not obvious
## when this second case would fail, really.
assert_file_exists2(files, workdir = p$path, name = "Resource file",
call = environment())
assert_file_exists_relative(files, workdir = p$path,
name = "Resource file", call = environment())
}
outpack_packet_file_mark(p, files_expanded, "immutable")
p$orderly2$resources <- c(p$orderly2$resources, files_expanded)
Expand Down Expand Up @@ -358,8 +358,8 @@ copy_shared_resource <- function(path_root, path_dest, config, files, call) {
here <- names(files)
there <- unname(files)

assert_file_exists2(there, workdir = shared_path, name = "Shared resource",
call = call)
assert_file_exists_relative(there, workdir = shared_path,
name = "Shared resource file", call = call)
src <- file.path(shared_path, there)
dst <- file.path(path_dest, here)

Expand Down
2 changes: 1 addition & 1 deletion R/outpack_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ outpack_metadata_create <- function(path, name, id, time, files,
if (is.null(files)) {
files <- dir(path, recursive = TRUE, all.files = TRUE, no.. = TRUE)
} else {
assert_file_exists2(files, name = "File", workdir = path)
assert_file_exists_relative(files, name = "File", workdir = path)
}

if (length(file_ignore) > 0) {
Expand Down
4 changes: 2 additions & 2 deletions R/outpack_packet.R
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ outpack_packet_file_mark <- function(packet, files, status) {
status <- match_value(status, c("immutable", "ignored"))
packet <- check_current_packet(packet)

assert_file_exists2(files, workdir = packet$path, name = "File",
call = environment())
assert_file_exists_relative(files, workdir = packet$path, name = "File",
call = environment())

## TODO: these are exclusive categories because we later return a
## 1:1 mapping of file to status
Expand Down
4 changes: 2 additions & 2 deletions R/read.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
orderly_read <- function(path, call = NULL) {
assert_file_exists2("orderly.R", name = "Orderly file", workdir = path,
call = call)
assert_file_exists_relative("orderly.R", name = "Orderly file",
workdir = path, call = call)
orderly_read_r(file.path(path, "orderly.R"))
}

Expand Down
24 changes: 12 additions & 12 deletions R/util_assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,18 @@ assert_is <- function(x, what, name = deparse(substitute(x))) {
}
}

assert_file_exists <- function(x, workdir = NULL, name = "File") {
err <- !file_exists(x, workdir = workdir)
assert_file_exists <- function(files, name = "File", call = NULL) {
err <- !file.exists(files)
if (any(err)) {
msg <- squote(x[err])
stop(sprintf("%s does not exist: %s", name, paste(msg, collapse = ", ")),
call. = FALSE)
n <- cli::qty(sum(err))
cli::cli_abort(
"{name}{n}{?s} {?does/do} not exist: {collapseq(files[err])}",
call = call)
}
}


assert_file_exists2 <- function(files, workdir, name, call = NULL) {
assert_file_exists_relative <- function(files, workdir, name, call = NULL) {
assert_relative_path(files, name, workdir, call)

assert_character(files, call = call)
Expand Down Expand Up @@ -93,13 +94,12 @@ assert_file_exists2 <- function(files, workdir, name, call = NULL) {
}
}

assert_is_directory <- function(x, workdir = NULL, name = "Directory") {
assert_file_exists(x, workdir, name)
path <- if (is.null(workdir)) x else file.path(workdir, x)
assert_is_directory <- function(path, name = "Directory", call = NULL) {
assert_scalar_character(path)
assert_file_exists(path, name = name, call = call)
if (!is_directory(path)) {
stop(sprintf("Path exists but is not a directory: %s",
paste(x, collapse = ", ")),
call. = FALSE)
cli::cli_abort("Path exists but is not a directory: {path}",
call = call)
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/helper-outpack.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ outpack_packet_run <- function(packet, script, envir = NULL) {
envir <- new.env(parent = .GlobalEnv)
}
packet <- check_current_packet(packet)
assert_file_exists(script, workdir = packet$path, name = "Script")
withr::with_dir(packet$path,
source_echo(script, envir = envir, echo = FALSE))
}
Expand Down
23 changes: 11 additions & 12 deletions tests/testthat/test-util-assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,37 @@ test_that("assert_is", {
test_that("assert_file_exists", {
tmp <- normalise_path(tempdir())
path <- tempfile(tmpdir = tmp)
expect_error(assert_file_exists(path), "File does not exist")
expect_error(assert_file_exists(path, "File"), "File does not exist")
file.create(path)
expect_silent(assert_file_exists(path))
expect_silent(assert_file_exists(basename(path), workdir = tmp))
expect_silent(assert_file_exists(path, "File"))
})


test_that("assert_file_exists2 works checks if files exist", {
test_that("assert_file_exists_relative works checks if files exist", {
tmp <- withr::local_tempdir()
file.create(file.path(tmp, "c"))
expect_error(assert_file_exists2("a", tmp, "File"),
expect_error(assert_file_exists_relative("a", tmp, "File"),
"File does not exist: 'a'")
expect_error(assert_file_exists2(c("a", "b"), tmp, "File"),
expect_error(assert_file_exists_relative(c("a", "b"), tmp, "File"),
"Files do not exist: 'a', 'b'")
expect_error(assert_file_exists2(c("a", "b", "c", "d"), tmp, "File"),
expect_error(assert_file_exists_relative(c("a", "b", "c", "d"), tmp, "File"),
"Files do not exist: 'a', 'b', 'd'")
expect_silent(assert_file_exists2("c", tmp, "File"))
expect_silent(assert_file_exists_relative("c", tmp, "File"))
})


test_that("assert_file_exists2 informs about case mismatch", {
test_that("assert_file_exists_relative informs about case mismatch", {
testthat::skip_if_not_installed("mockery")
mock_file_exists <- mockery::mock(TRUE, cycle = TRUE)
mockery::stub(assert_file_exists2, "file_exists", mock_file_exists)
mockery::stub(assert_file_exists_relative, "file_exists", mock_file_exists)

tmp <- withr::local_tempdir()
file.create(file.path(tmp, "a"))
fs::dir_create(file.path(tmp, "b/c"))
file.create(file.path(tmp, "b/c/d"))

err <- expect_error(
assert_file_exists2("A", tmp, "File"),
assert_file_exists_relative("A", tmp, "File"),
"File does not exist: 'A'")
expect_length(err$body, 3)
expect_equal(names(err$body), c("i", "i", "i"))
Expand All @@ -84,7 +83,7 @@ test_that("assert_file_exists2 informs about case mismatch", {
expect_match(err$body[[3]], "Looked within directory '.+'")

err <- expect_error(
assert_file_exists2(c("A", "b/C/d"), tmp, "File"),
assert_file_exists_relative(c("A", "b/C/d"), tmp, "File"),
"Files do not exist: 'A', 'b/C/d'")
expect_length(err$body, 4)
expect_equal(names(err$body), c("i", "i", "i", "i"))
Expand Down

0 comments on commit 232b9fe

Please sign in to comment.