Skip to content

Commit 6b68f65

Browse files
Merge pull request #962 from r-lib/issue-961
Don't tolerate empty non-zero byte files
2 parents af0e468 + 2e0181d commit 6b68f65

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

NEWS.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,23 @@ editor_options:
3030
there is no literal code immediately following after the end of the example
3131
section (#940).
3232

33+
- Files with no tokens in it are now transformed into zero-byte files (#962).
34+
3335
**Other**
3436

35-
- Old (and outdated) vignettes have been removed (\@IndrajeetPatil, #955).
36-
To access them, do `git checkout v1.0.0`.
37+
- Old (and outdated) vignettes have been removed (\@IndrajeetPatil, #955). To
38+
access them, do `git checkout v1.0.0`.
3739
- Upgrade testing infra to testthat 3e (\@IndrajeetPatil, #949).
3840
- Minor improvements to the documentation (\@IndrajeetPatil, #958).
3941
- All (R)md files in this project's source code are now formatted with
4042
default pandoc markdown formatter. This conversion is required when using
4143
the visual mode in RStudio (#941).
4244
- Update {pkgdown} action to always build, but only deploy on default branch
4345
(#946).
44-
- turned off `styler.print.Vertical` in vignettes so ANSI output of {prettycode}
45-
not messing with {pkgdown} (\@IndrajeetPatil, #956, #957).
46+
- turned off `styler.print.Vertical` in vignettes so ANSI output of
47+
{prettycode} not messing with {pkgdown} (\@IndrajeetPatil, #956, #957).
4648
- Improved code quality by fixing {lintr} warnings (#960).
4749

48-
4950
# styler 1.7.0
5051

5152
- if `else` follows directly after `if`, line breaks are removed (#935).

R/io.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ transform_utf8_one <- function(path, fun, dry) {
2727
{
2828
file_with_info <- read_utf8(path)
2929
# only write back when changed OR when there was a missing newline
30-
new <- fun(file_with_info$text)
31-
identical_content <- identical(unclass(file_with_info$text), unclass(new))
30+
new <- unclass(fun(file_with_info$text))
31+
if (identical(new, "")) {
32+
new <- character(0)
33+
}
34+
identical_content <- identical(file_with_info$text, new)
3235
identical <- identical_content && !file_with_info$missing_EOF_line_break
3336
if (!identical) {
3437
if (dry == "fail") {

tests/testthat/test-exception_handling.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test_that("style_text with no tokens returns empty string and warning", {
1818

1919
test_that("style_file with no tokens returns empty string and warning", {
2020
capture_output(expect_warning(
21-
style_file(testthat_file("exception_handling", "empty_file.R")),
21+
style_file(testthat_file("exception_handling", "empty_file.R"), dry = "on"),
2222
"not contain any tokens."
2323
))
2424
})

tests/testthat/test-encoding.R renamed to tests/testthat/test-io.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
test_that("non-ASCII characters are handled properly for text styling", {
42
expect_equal(
53
style_text("glück <-3") %>% unclass(), "glück <- 3"
@@ -26,3 +24,13 @@ test_that("non-ASCII characters are handled properly for file styling", {
2624
}
2725
)
2826
})
27+
28+
test_that("empty files are converted to zero-bite files", {
29+
local_test_setup()
30+
for (file_content in list(character(), "", c("", ""))) {
31+
tmp <- tempfile(fileext = ".R")
32+
write_utf8(file_content, tmp)
33+
suppressWarnings(style_file(tmp))
34+
expect_true(file.size(tmp) == 0)
35+
}
36+
})

0 commit comments

Comments
 (0)