Skip to content

Commit

Permalink
Handle encodings better:
Browse files Browse the repository at this point in the history
- Always convert output to UTF-8.
- Use the <xx> notation for bytes that are invalid in the native encoding.

Closes #152.
  • Loading branch information
gaborcsardi committed Sep 18, 2021
1 parent a809fd4 commit da0ab2e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
8 changes: 4 additions & 4 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ parse_checkdir <- function(entries) {
perl = TRUE
)
}
get_test_fail <- function(path) {
get_test_output(path, pattern = "\\.Rout\\.fail")
get_test_fail <- function(path, encoding = "") {
get_test_output(path, pattern = "\\.Rout\\.fail", encoding = encoding)
}

get_test_output <- function(path, pattern) {
get_test_output <- function(path, pattern, encoding = "") {
test_path <- file.path(path, dir(path, pattern = "^tests"))
paths <- dir(test_path, paste0(pattern, "$"), full.names = TRUE)

Expand All @@ -97,7 +97,7 @@ get_test_output <- function(path, pattern) {
substr(x, first_gt, nchar(x))
}

tests <- lapply(paths, read_char)
tests <- lapply(paths, read_char, encoding = encoding)
tests <- lapply(tests, win2unix)
lapply(tests, trim_header)
}
Expand Down
9 changes: 5 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

read_char <- function(path, ...) {
readChar(path, nchars = file.info(path)$size, ...)
read_char <- function(path, encoding = "", ...) {
txt <- readChar(path, nchars = file.info(path)$size, useBytes = TRUE, ...)
iconv(txt, encoding, "UTF-8", sub = "byte")
}

win2unix <- function(str) {
Expand Down Expand Up @@ -98,10 +99,10 @@ cat0 <- function(..., sep = "") {
cat(..., sep = "")
}

get_install_out <- function(path) {
get_install_out <- function(path, encoding = "") {
install_out <- file.path(path, "00install.out")
if (is_string(install_out) && file.exists(install_out)) {
win2unix(read_char(install_out))
win2unix(read_char(install_out, encoding = encoding))
} else {
"<00install.out file does not exist>"
}
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/fixtures/badenc.fail
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(testthat)
> library(ipc)
>
> test_check("ipc")
...
Error: package or namespace load failed for ‘stats�During startup - Warning messages:
1: package ‘utils’ in options("defaultPackages") was not found
2: package ‘graphics’ in options("defaultPackages") was not found
3: In get(Info[i, 1], envir = env) : internal error -3 in R_decompress1
4: package ‘stats’ in options("defaultPackages") was not found
...
6 changes: 6 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ test_that("should_use_rs_pandoc", {
withr::local_envvar(RSTUDIO_PANDOC = "yes")
expect_false(should_use_rs_pandoc())
})

test_that("read_char and files with invalid encodings", {
expect_silent(
txt <- read_char(test_path("fixtures", "badenc.fail"), encoding = "UTF-8")
)
})

0 comments on commit da0ab2e

Please sign in to comment.