Skip to content

Commit f1094bf

Browse files
authored
Fix #458 (#461)
* Fix #458 The S3 method can be dispatched. But the problem is that it can't be tested with testthat. Why? * Make test work * Reduce line count * Simplify .standardize_format
1 parent 727d50f commit f1094bf

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: rio
22
Type: Package
33
Title: A Swiss-Army Knife for Data I/O
4-
Version: 1.2.3
4+
Version: 1.2.4
55
Authors@R: c(person("Jason", "Becker", role = "aut", email = "[email protected]"),
66
person("Chung-hong", "Chan", role = c("aut", "cre"), email = "[email protected]",
77
comment = c(ORCID = "0000-0002-6232-7530")),

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# rio 1.2.4
2+
3+
Bug fixes
4+
5+
* Fix #458, custom S3 import and export functions work again
6+
17
# rio 1.2.3
28

39
* Fix #453, don't nudge the user to install all suggested packages

R/extensions.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
}
44

55
.import.default <- function(file, ...) {
6-
fileinfo <- get_info(file)
7-
if (is.na(fileinfo$type) || is.na(fileinfo$import_function) || fileinfo$import_function == "") {
6+
fileinfo <- get_info(file) ## S3 can't be dispatched
7+
if (fileinfo$type == "unknown" || is.na(fileinfo$import_function) || fileinfo$import_function == "") {
88
stop("Format not supported", call. = FALSE)
99
}
1010
if (fileinfo$type == "known") {
@@ -24,7 +24,7 @@
2424

2525
.export.default <- function(file, x, ...) {
2626
fileinfo <- get_info(file)
27-
if (is.na(fileinfo$type) || is.na(fileinfo$export_function) || fileinfo$export_function == "") {
27+
if (fileinfo$type == "unknown" || is.na(fileinfo$export_function) || fileinfo$export_function == "") {
2828
stop("Format not supported", call. = FALSE)
2929
}
3030
if (fileinfo$type == "known") {

R/utils.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,15 @@ get_ext <- function(file) {
5454
## TODO google sheets
5555
matched_formats <- unique_rio_formats[unique_rio_formats$input == input, ]
5656
if (nrow(matched_formats) == 0) {
57-
return(list(input = input, format = NA, type = NA, format_name = NA, import_function = NA, export_function = NA, file = file))
57+
return(list(input = input, format = input, type = "unknown", format_name = NA, import_function = NA, export_function = NA, file = file))
5858
}
5959
output <- as.list(matched_formats)
6060
output$file <- file
6161
return(output)
6262
}
6363

6464
.standardize_format <- function(input) {
65-
info <- .query_format(input, "")
66-
if (is.na(info$format)) {
67-
return(input)
68-
}
69-
info$format
65+
.query_format(input, "")$format
7066
}
7167

7268
twrap <- function(value, tag) {

tests/testthat/test_extensions.R

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
library("datasets")
2+
skip_on_cran()
23

34
test_that("S3 extension mechanism works for imports", {
45
withr::with_tempdir({
56
write.csv(iris, "iris.custom")
6-
expect_error(import("iris.custom"))
7+
expect_error(import("iris.custom"), "Format not supported")
78
.import.rio_custom <- function(file, ...) {
89
read.csv(file, ...)
910
}
10-
##expect_true(is.data.frame(import('iris.custom')))
11-
rm(.import.rio_custom)
11+
expect_error(.import.rio_custom("iris.custom"), NA)
12+
assign(".import.rio_custom", .import.rio_custom, envir = .GlobalEnv)
13+
expect_true(is.data.frame(import("iris.custom")))
14+
rm(list = ".import.rio_custom", envir = .GlobalEnv)
1215
})
1316
})
1417

1518
test_that("S3 extension mechanism works for exports", {
1619
withr::with_tempdir({
17-
expect_error(export("iris.custom"))
18-
.export.rio_custom <- function(file, data, ...) {
19-
write.csv(data, file, ...)
20+
expect_error(export(iris, "iris.custom"))
21+
.export.rio_custom <- function(file, x, ...) {
22+
write.csv(x, file, ...)
2023
invisible(file)
2124
}
22-
expect_error(is.character(export(iris, "iris.custom")))
23-
rm(.export.rio_custom)
25+
expect_error(.export.rio_custom("iris.custom", iris), NA)
26+
assign(".export.rio_custom", .export.rio_custom, envir = .GlobalEnv)
27+
expect_true(is.character(export(iris, "iris.custom")))
28+
rm(list = ".export.rio_custom", envir = .GlobalEnv)
2429
})
2530
})

0 commit comments

Comments
 (0)