Skip to content

Commit

Permalink
Store the absolute path in ReloadedArraySeed constructor.
Browse files Browse the repository at this point in the history
This protects saveObject() calls from changes in the working directory, which
would break if a relative path was stored in the seed.
  • Loading branch information
LTLA committed Feb 25, 2024
1 parent 62b3175 commit 1b8b9d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: alabaster.matrix
Title: Load and Save Artifacts from File
Version: 1.3.10
Date: 2024-02-21
Version: 1.3.11
Date: 2024-02-24
Authors@R: person("Aaron", "Lun", role=c("aut", "cre"), email="[email protected]")
License: MIT + file LICENSE
Description:
Expand Down
5 changes: 4 additions & 1 deletion R/ReloadedArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ ReloadedArraySeed <- function(path, seed=NULL, ...) {
while (is(seed, "DelayedArray")) {
seed <- seed@seed
}
new("ReloadedArraySeed", path=path, seed=seed)

# Need to obtain an absolute path in order for this to be safe in
# saveObject(), possibly after changes to the working directory has.
new("ReloadedArraySeed", path=normalizePath(path, mustWork=TRUE), seed=seed)
}

#' @export
Expand Down
14 changes: 12 additions & 2 deletions tests/testthat/test-ReloadedArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ obj2 <- readObject(dir2)

test_that("ReloadedArrays work correctly", {
expect_s4_class(obj, "ReloadedArray")
expect_identical(BiocGenerics::path(obj), dir)
expect_identical(BiocGenerics::path(obj), normalizePath(dir))
expect_identical(dim(obj), dim(arr))
expect_identical(extract_array(obj, vector("list", 3)), arr)
expect_identical(as.array(obj), arr)
expect_false(is_sparse(obj))

expect_s4_class(obj2, "ReloadedMatrix")
expect_identical(BiocGenerics::path(obj2), dir2)
expect_identical(BiocGenerics::path(obj2), normalizePath(dir2))
expect_identical(dim(obj2), dim(x))
expect_identical(as(extract_sparse_array(obj2, vector("list", 2)), "dgCMatrix"), x)
expect_true(is_sparse(obj2))
Expand All @@ -30,6 +30,16 @@ test_that("ReloadedArrays work correctly", {
expect_identical(as(obj2@seed, "dgCMatrix"), x)
})

test_that("ReloadedArrays store the absolute path", {
cwd <- getwd()
setwd(dirname(dir))
on.exit(setwd(cwd))

obj <- readObject(basename(dir))
expect_identical(BiocGenerics::path(obj), normalizePath(dir))
expect_identical(as.array(obj), arr)
})

test_that("ReloadedArrays save correctly", {
tmp <- tempfile()
saveObject(obj, tmp, ReloadedArray.reuse.files="none")
Expand Down

0 comments on commit 1b8b9d5

Please sign in to comment.