Skip to content

Commit 85a81fb

Browse files
Merge pull request #548 from michaelquinn32/patch-1
- Don't write to the test directory during testing (#548).
2 parents 8f42440 + 6bd9f75 commit 85a81fb

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- blank lines in function calls and headers are now removed, for the former only
66
when there are no comments before or after the blank line (#629, #630, #635).
7+
- added an option (`styler.test_dir_writeable`) that changes test behavior
8+
to not directly modify test files in the current directory (#548).
79
- `style_file()` and friends gain argument `dry` to control if changes should
810
be applied to files or not (#634).
911

@@ -13,7 +15,6 @@
1315
- always strip trailing spaces and make cache insensitive to it (#626).
1416
- typos in documentation (#618, #614).
1517

16-
1718
# styler 1.3.2
1819

1920
Release upon request by the CRAN team.

R/testing.R

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ test_collection <- function(test, sub_test = NULL,
4141

4242
out_names <- construct_out(in_names)
4343

44-
out_items <- file.path(path, out_names)
45-
in_items <- file.path(path, in_names)
46-
47-
out_trees <- construct_tree(in_items)
44+
if (getOption("styler.test_dir_writable", TRUE)) {
45+
out_items <- file.path(path, out_names)
46+
in_items <- file.path(path, in_names)
47+
out_trees <- construct_tree(in_items)
48+
} else {
49+
in_items <- file.path(path, in_names)
50+
out_items <- file.path(tempdir(), out_names)
51+
ref_items <- file.path(path, out_names)
52+
file.copy(ref_items, out_items, overwrite = TRUE, copy.mode = FALSE)
53+
out_trees <- file.path(tempdir(), construct_tree(in_names))
54+
}
4855

4956
pwalk(list(in_items, out_items, in_names, out_names, out_trees),
5057
transform_and_check,
@@ -106,7 +113,9 @@ transform_and_check <- function(in_item, out_item,
106113
read_in <- xfun::read_utf8(in_item)
107114
if (write_tree) {
108115
create_tree(read_in) %>%
109-
write.table(out_tree, col.names = FALSE, row.names = FALSE, quote = FALSE, fileEncoding = "UTF-8")
116+
write.table(out_tree, col.names = FALSE, row.names = FALSE, quote = FALSE,
117+
fileEncoding = "UTF-8"
118+
)
110119
}
111120
transformed_text <- read_in %>%
112121
transformer(...) %>%

R/zzz.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
styler.cache_name = styler_version,
77
styler.addins_style_transformer = "styler::tidyverse_style()",
88
styler.ignore_start = "# styler: off",
9-
styler.ignore_stop = "# styler: on"
9+
styler.ignore_stop = "# styler: on",
10+
styler.test_dir_writable = TRUE
1011
)
1112
toset <- !(names(op.styler) %in% names(op))
1213
if (any(toset)) options(op.styler[toset])

tests/testthat/test-cache-with-r-cache.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ capture.output(test_that("cached expressions are displayed propperly", {
6464
cache_info <- cache_info("testthat", format = "tabular")
6565
expect_known_value(
6666
cache_info[, c("n", "size", "last_modified", "activated")],
67-
file = test_path("reference-objects/cache-info-1")
67+
file = test_path("reference-objects/cache-info-1"),
68+
update = getOption("styler.test_dir_writable", TRUE)
6869
)
6970

7071
activate_testthat_cache()
@@ -73,14 +74,18 @@ capture.output(test_that("cached expressions are displayed propperly", {
7374
cache_info$size <- round(cache_info$size, -2)
7475
expect_known_value(
7576
cache_info[, c("n", "size", "activated")],
76-
file = test_path("reference-objects/cache-info-2")
77+
file = test_path("reference-objects/cache-info-2"),
78+
update = getOption("styler.test_dir_writable", TRUE)
79+
7780
)
7881
style_text("a <-function() NULL")
7982
cache_info <- cache_info(format = "tabular")
8083
cache_info$size <- round(cache_info$size, -2)
8184
expect_known_value(
8285
cache_info[, c("n", "size", "activated")],
83-
file = test_path("reference-objects/cache-info-3")
86+
file = test_path("reference-objects/cache-info-3"),
87+
update = getOption("styler.test_dir_writable", TRUE)
88+
8489
)
8590
}))
8691

tests/testthat/test-public_api.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ test_that("messages (via cat()) of style_file are correct", {
163163
testthat_file(paste0(
164164
"public-api/xyzdir-dirty/dirty-reference-with-scope-tokens-",
165165
encoding
166-
))
166+
)),
167+
update = getOption("styler.test_dir_writable", TRUE)
167168
)
168169

169170
# No message if scope > line_breaks and code does not change
@@ -175,7 +176,8 @@ test_that("messages (via cat()) of style_file are correct", {
175176
testthat_file(paste0(
176177
"public-api/xyzdir-dirty/clean-reference-with-scope-tokens-",
177178
encoding
178-
))
179+
)),
180+
update = getOption("styler.test_dir_writable", TRUE)
179181
)
180182

181183
# No message if scope <= line_breaks even if code is changed.
@@ -187,7 +189,8 @@ test_that("messages (via cat()) of style_file are correct", {
187189
testthat_file(paste0(
188190
"public-api/xyzdir-dirty/dirty-reference-with-scope-spaces-",
189191
encoding
190-
))
192+
)),
193+
update = getOption("styler.test_dir_writable", TRUE)
191194
)
192195
}
193196
)

tests/testthat/test-utils.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ test_that("non-comment-helpers", {
1010
test_that("files with and without blank EOF line are read correctly", {
1111
expect_known_value(
1212
read_utf8(test_path("reference-objects/missing-blank-at-EOF.R")),
13-
test_path("reference-objects/return-read-utf8-missing-EOF")
13+
test_path("reference-objects/return-read-utf8-missing-EOF"),
14+
update = getOption("styler.test_dir_writable", TRUE)
1415
)
1516

1617
expect_known_value(
1718
read_utf8(test_path("reference-objects/non-missing-blank-at-EOF.R")),
18-
test_path("reference-objects/return-read-utf8-non-missing-EOF")
19+
test_path("reference-objects/return-read-utf8-non-missing-EOF"),
20+
update = getOption("styler.test_dir_writable", TRUE)
1921
)
2022
})

0 commit comments

Comments
 (0)