Skip to content

Commit

Permalink
Add test for missing outpack.json
Browse files Browse the repository at this point in the history
  • Loading branch information
plietar committed Mar 19, 2024
1 parent a5c06d8 commit cfb4810
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
7 changes: 7 additions & 0 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ orderly_import_zip <- function(path, root = NULL, locate = TRUE) {
src <- withr::local_tempfile()
zip::unzip(path, exdir = src)

if (!file.exists(file.path(src, "outpack.json"))) {
cli::cli_abort(
c("Zip file does not contain an 'outpack.json' file at its root",
i = "Are you sure this file was produced by orderly2::orderly_export_zip?"),
call = environment())
}

contents <- jsonlite::read_json(file.path(src, "outpack.json"),
simplifyVector = TRUE)

Expand Down
8 changes: 0 additions & 8 deletions R/outpack_hash.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,3 @@ rehash <- function(data, hash_function, expected) {
algorithm <- hash_parse(expected)$algorithm
hash_function(data, algorithm)
}

## metadata files are hashed by ignoring leading and trailing newline
## characters.
hash_metadata_files <- function(path, hash_algorithm) {
vcapply(path, function(p) {
hash_data(read_string(p), hash_algorithm)
})
}
36 changes: 25 additions & 11 deletions tests/testthat/test-export.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test_that("Exporting a packet includes its transitive dependencies", {
ids <- create_random_packet_chain(root, 3)
other <- create_random_packet(root)

path <- tempfile()
path <- withr::local_tempfile()
orderly_export_zip(path, ids[[3]], root = root)

info <- export_info(path)
Expand All @@ -33,7 +33,7 @@ test_that("Can export multiple packets", {
second <- create_random_packet(root)
ids <- c(first, second)

path <- tempfile()
path <- withr::local_tempfile()
orderly_export_zip(path, ids, root = root)

info <- export_info(path)
Expand All @@ -45,7 +45,7 @@ test_that("Can export from a file store", {
root <- create_temporary_root(use_file_store = TRUE)
ids <- create_random_packet_chain(root, 3)

path <- tempfile()
path <- withr::local_tempfile()
orderly_export_zip(path, ids[[3]], root = root)

info <- export_info(path)
Expand All @@ -57,21 +57,35 @@ test_that("Packet files are de-duplicated when exported", {
root <- create_temporary_root()
ids <- c(create_deterministic_packet(root), create_deterministic_packet(root))

path <- tempfile()
path <- withr::local_tempfile()
orderly_export_zip(path, ids, root = root)

info <- export_info(path)
expect_setequal(info$metadata, ids)
expect_equal(length(info$files), 1)
})

test_that("Importing an invalid zip fails", {
dir <- withr::local_tempfile()
fs::dir_create(dir)
fs::file_create(file.path(dir, "hello.txt"))

zipfile <- withr::local_tempfile()
zip::zip(zipfile, files=c("hello.txt"), root=dir)

root <- create_temporary_root()
expect_error(
orderly_import_zip(zipfile, root = root),
"Zip file does not contain an 'outpack.json' file at its root")
})

test_that("Can import a zip file", {
upstream <- create_temporary_root()
downstream <- create_temporary_root()

id <- create_random_packet(upstream)

path <- tempfile()
path <- withr::local_tempfile()
orderly_export_zip(path, id, root = upstream)

imported <- orderly_import_zip(path, root = downstream)
Expand All @@ -97,7 +111,7 @@ test_that("Can import a zip file to a file store", {

ids <- create_random_packet_chain(upstream, 3)

path <- tempfile()
path <- withr::local_tempfile()
orderly_export_zip(path, ids[[3]], root = upstream)
orderly_import_zip(path, root = downstream)

Expand All @@ -118,7 +132,7 @@ test_that("Importing a zip file is idempotent", {

id <- create_random_packet(upstream)

path <- tempfile()
path <- withr::local_tempfile()
orderly_export_zip(path, id, root = upstream)
imported_once <- orderly_import_zip(path, root = downstream)
imported_twice <- orderly_import_zip(path, root = downstream)
Expand All @@ -136,11 +150,11 @@ test_that("New packets are imported", {
upstream <- create_temporary_root()

first_id <- create_random_packet(upstream)
first_zip <- tempfile()
first_zip <- withr::local_tempfile()
orderly_export_zip(first_zip, first_id, root = upstream)

second_id <- create_random_packet(upstream)
second_zip <- tempfile()
second_zip <- withr::local_tempfile()
orderly_export_zip(second_zip, c(first_id, second_id), root = upstream)

downstream <- create_temporary_root()
Expand Down Expand Up @@ -173,7 +187,7 @@ test_that("Can import packet with existing metadata", {
expect_setequal(names(index$metadata), id)
expect_equal(length(index$unpacked), 0)

path <- tempfile()
path <- withr::local_tempfile()
orderly_export_zip(path, id, root = upstream)
orderly_import_zip(path, root = downstream)

Expand All @@ -190,7 +204,7 @@ test_that("Importing a zip file with mismatching metadata fails", {
create_random_packet(upstream, id = id)
create_random_packet(downstream, id = id)

path <- tempfile()
path <- withr::local_tempfile()
orderly_export_zip(path, id, root = upstream)

expect_error(
Expand Down

0 comments on commit cfb4810

Please sign in to comment.