Skip to content

Ensure styled files also have EOF line break if no tokens were changed #540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ transform_utf8 <- function(path, fun, write_back = TRUE) {

#' @importFrom rlang with_handlers warn
transform_utf8_one <- function(path, fun, write_back) {
old <- xfun::read_utf8(path)
with_handlers({
new <- fun(old)
identical <- identical(unclass(old), unclass(new))
file_with_info <- read_utf8(path)
# only write back when changed OR when there was a missing newline
new <- fun(file_with_info$text)
identical_content <- identical(unclass(file_with_info$text), unclass(new))
identical <- identical_content && !file_with_info$missing_EOF_line_break
if (!identical && write_back) {
xfun::write_utf8(new, path)
}
Expand All @@ -28,3 +30,58 @@ transform_utf8_one <- function(path, fun, write_back) {
NA
})
}

#' Read UTF-8
#'
#' Reads an UTF-8 file, returning the content and whether or not the final line
#' was blank. This information is required higher up in the callstack because
#' we should write back if contents changed or if there is no blank line at the
#' EOF. A perfectly styled file with no EOF blank line will gain such a line
#' with this implementation.
#' @param path A path to a file to read.
#' @keywords internal
read_utf8 <- function(path) {
out <- rlang::with_handlers(
read_utf8_bare(path),
warning = function(w) w,
error = function(e) e
)
if (inherits(out, "character")) {
list(
text = out,
missing_EOF_line_break = FALSE
)
} else if (inherits(out, "error")) {
rlang::abort(out$message)
} else if (inherits(out, "warning")) {
list(
text = read_utf8_bare(path, warn = FALSE),
missing_EOF_line_break = grepl("incomplete", out$message)
)
}
}

#' Drop-in replacement for [xfun::read_utf8()], with an optional `warn`
#' argument.
#' @keywords internal
read_utf8_bare <- function(con, warn = TRUE) {
x <- readLines(con, encoding = "UTF-8", warn = warn)
i <- invalid_utf8(x)
n <- length(i)
if (n > 0) {
stop(
c(
"The file ", con, " is not encoded in UTF-8. ",
"These lines contain invalid UTF-8 characters: "
),
paste(c(head(i), if (n > 6) "..."), collapse = ", ")
)
}
x
}

#' Drop-in replacement for [xfun:::invalid_utf8()]
#' @keywords internal
invalid_utf8 <- function(x) {
which(!is.na(x) & is.na(iconv(x, "UTF-8", "UTF-8")))
}
19 changes: 19 additions & 0 deletions man/read_utf8.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/read_utf8_bare.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/reference-objects/missing-blank-at-EOF.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x
Binary file not shown.
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,15 @@ test_that("non-comment-helpers", {
expect_equal(previous_non_comment(child, 4), 2)
expect_equal(next_non_comment(child, 2), 4)
})

test_that("files with and without blank EOF line are read correctly", {
expect_known_value(
read_utf8(test_path("reference-objects/missing-blank-at-EOF.R")),
test_path("reference-objects/return-read-utf8-missing-EOF")
)

expect_known_value(
read_utf8(test_path("reference-objects/non-missing-blank-at-EOF.R")),
test_path("reference-objects/return-read-utf8-non-missing-EOF")
)
})