Skip to content

Commit

Permalink
Bugfix: fix snapshots of text_write() and text_read() on Windows and …
Browse files Browse the repository at this point in the history
…macOS.

Absolute paths are not included in error messages anymore, only the value
of argument path is.

Some snapshotting expectations were tweaked.
  • Loading branch information
jeanmathieupotvin committed Jan 20, 2025
1 parent f88a262 commit 59d6cd6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
16 changes: 8 additions & 8 deletions R/text-io.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ text_read <- function(path = "", encoding = "UTF-8") {
assert_chr1(encoding)

# Not super useful, but a little safer.
path <- normalizePath(path, mustWork = FALSE)
path_norm <- normalizePath(path, mustWork = FALSE)

if (!utils::file_test("-f", path) ||
!utils::file_test("-r", path)) {
if (!utils::file_test("-f", path_norm) ||
!utils::file_test("-r", path_norm)) {
stopf(
"'path' '%s' does not exist, is a directory, or is not readable.",
path)
}

# This connection re-encodes input
# to UTF-8 from supplied encoding.
con <- file(path, "r", encoding = encoding)
con <- file(path_norm, "r", encoding = encoding)
on.exit(close(con, "r"))

# Setting encoding to UTF-8 explicitly marks
Expand All @@ -68,14 +68,14 @@ text_write <- function(x = character(), path = "", encoding = "UTF-8") {
assert_chr1(encoding)

# Not super useful, but a little safer.
path <- normalizePath(path, mustWork = FALSE)
path_norm <- normalizePath(path, mustWork = FALSE)

if (utils::file_test("-d", path) || (
utils::file_test("-f", path) && !utils::file_test("-w", path))) {
if (utils::file_test("-d", path_norm) || (
utils::file_test("-f", path_norm) && !utils::file_test("-w", path_norm))) {
stopf("'path' '%s' is a directory, or is not writable.", path)
}

con <- file(path, "wb", encoding = encoding)
con <- file(path_norm, "wb", encoding = encoding)
on.exit(close(con, "wb"))

# Character elements are explicitly re-encoded to UTF-8
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/text-io.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
---

Code
# Temporary directory changes whenever tests are ran. It is
# substituted with a constant value for snapshotting purposes.
# Input temporary directory is random. It is replaced
# by a constant for in this snapshot for reproducibility.
text_write("Hello, world!", temp_dir)
Condition
Error:
! 'path' '/tmp/Rtmp/directory' is a directory, or is not writable.
! 'path' 'a-test-directory' is a directory, or is not writable.

# text_write() validates encoding

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-text-io.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ test_that("text_write() validates path", {
expect_error(text_write("Hello, world!", temp_file))
expect_snapshot(text_write("Hello, world!", 1L), error = TRUE)
expect_snapshot({
"Temporary directory changes whenever tests are ran. It is"
"substituted with a constant value for snapshotting purposes."
"Input temporary directory is random. It is replaced"
"by a constant for in this snapshot for reproducibility."
text_write("Hello, world!", temp_dir)
},
error = TRUE,
transform = \(x) gsub("'/tmp/(.*?)'", "'/tmp/Rtmp/directory'", x))
transform = \(x) gsub("'path' '(.*?)'", "'path' 'a-test-directory'", x))
})

test_that("text_write() validates encoding", {
Expand Down

0 comments on commit 59d6cd6

Please # to comment.