Skip to content

Commit

Permalink
Fix #916, related to parseUTF8 return value attribute srcfile on …
Browse files Browse the repository at this point in the history
…Windows.
  • Loading branch information
meztez committed Nov 18, 2023
1 parent 0c59025 commit 4a926ac
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# plumber (development version)

* Fix #916, related to `parseUTF8` return value attribute `srcfile` on Windows. (#930)

# plumber 1.2.1

Expand Down
6 changes: 4 additions & 2 deletions R/utf8.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ parseUTF8 <- function(file) {
# **can** be encoded natively on Windows (might be a bug in base R); we
# rewrite the source code in a natively encoded temp file and parse it in this
# case (the source reference is still pointed to the original file, though)
keepsource <- TRUE
if (isWindows() && enc == 'unknown') {
file <- tempfile(); on.exit(unlink(file), add = TRUE)
writeLines(lines, file)
keepsource <- FALSE
}

# keep the source locations within the file while parsing
exprs <- try(parse(file, keep.source = TRUE, srcfile = src, encoding = enc))
exprs <- try(parse(file, keep.source = keepsource, srcfile = src, encoding = enc))
if (inherits(exprs, "try-error")) {
stop("Error sourcing ", file)
}
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-utf8.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test_that("parseUTF8 has same srcfile attribute as file arg input", {
filename <- test_path("files/plumber.R")
expect_equal(attr(parseUTF8(file), "srcfile")$filename, filename)
})

0 comments on commit 4a926ac

Please sign in to comment.