Skip to content

Commit

Permalink
Fix #421
Browse files Browse the repository at this point in the history
  • Loading branch information
chainsawriot committed May 20, 2024
1 parent 4f53898 commit 2da99a5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Bug fixes

- Fix #412, prevent double usage of `which` for archive formats
- Fix #415, both `import_list()` and `export_list()` support tar archives.
- Fix #421, tar export is only supported by R >= 4.0.3.

# rio 1.0.2

Expand Down
7 changes: 7 additions & 0 deletions R/compression.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,10 @@ parse_archive <- function(file, which, file_type, ...) {
destname <- tempfile()
R.utils::decompressFile(filename = filename, destname = destname, temporary = TRUE, remove = FALSE, overwrite = TRUE, FUN = decompression_fun, ext = file_type)
}

.check_tar_support <- function(file_type, rversion) {
if (file_type %in% c("tar", "tar.gz", "tar.bz2") && rversion < "4.0.3") {
stop("Exporting to tar formats is not supported for this version of R.", call. = FALSE)
}
NULL
}
1 change: 1 addition & 0 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export <- function(x, file, format, ...) {
file <- paste0(as.character(substitute(x)), ".", format)
compress <- NA_character_
}
.check_tar_support(compress, getRversion())
format <- .standardize_format(format)
outfile <- file
if (is.matrix(x) || inherits(x, "ArrowTabular")) {
Expand Down
1 change: 1 addition & 0 deletions R/export_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export_list <- function(x, file, archive = "", ...) {
if (inherits(x, "data.frame")) {
stop("'x' must be a list. Perhaps you want export()?", call. = FALSE)
}
.check_tar_support(archive_format$compress, getRversion())

outfiles <- .create_outfiles(file, x)

Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test_compress.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,21 @@ test_that("Prevent the reuse of `which` for zip and tar", {
})
}
})

test_that(".check_tar_support ref #421", {
expect_error(.check_tar_support("tar", R_system_version("4.0.2")), "^Exporting")
expect_error(.check_tar_support("tar.gz", R_system_version("4.0.2")), "^Exporting")
expect_error(.check_tar_support("tar.bz2", R_system_version("4.0.2")), "^Exporting")
expect_error(.check_tar_support("tar", R_system_version("4.0.3")), NA)
expect_error(.check_tar_support("tar.gz", R_system_version("4.0.3")), NA)
expect_error(.check_tar_support("tar.bz2", R_system_version("4.0.3")), NA)
expect_error(.check_tar_support("zip", R_system_version("4.0.2")), NA)
expect_error(.check_tar_support(NA, R_system_version("4.0.2")), NA)
})

test_that("tar export error for R < 4.0.3", {
skip_if(getRversion() >= "4.0.3")
withr::with_tempfile("iris_path", fileext = paste0(".csv.", "tar"), code = {
expect_error(export(iris, iris_path), "^Exporting")
})
})
9 changes: 9 additions & 0 deletions tests/testthat/test_export_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ test_that("export_list() works", {
})

test_that("archive formats, #415", {
skip_if(getRversion() <= "4.0")
withr::with_tempdir({
mylist <- list(mtcars3 = mtcars[1:10, ], mtcars2 = mtcars[11:20, ], mtcars1 = mtcars[21:32, ])
expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.csv.gz"), "specified but format is not supported")
Expand All @@ -61,3 +62,11 @@ test_that("List length of one, #385", {
expect_true(is.list(readRDS(tempfile)) && !is.data.frame(readRDS(tempfile)))
})
})

test_that("tar export error for R < 4.0.3", {
skip_if(getRversion() >= "4.0.3")
withr::with_tempdir({
mylist <- list(mtcars3 = mtcars[1:10, ], mtcars2 = mtcars[11:20, ], mtcars1 = mtcars[21:32, ])
expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.csv.tar"), "^Exporting")
})
})

0 comments on commit 2da99a5

Please sign in to comment.