Skip to content

Commit f7cea77

Browse files
Merge pull request #798 from lorenzwalthert/testing-infra
- Improve testing infra (#798)
2 parents 2b1eeac + b6df41a commit f7cea77

16 files changed

+193
-106
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
* Break the line between `%>%` and `{` inside and outside function calls (#825).
1818

19+
* improved test setup with fixtures and similar (#798).
20+
1921
# styler 1.5.1
2022

2123
## Alignment detection

R/testing.R

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,24 +257,19 @@ n_times_faster_with_cache <- function(x1, x2 = x1, ...,
257257
out <- purrr::map(1:n, n_times_faster_bench,
258258
x1 = x1, x2 = x2, fun = fun,
259259
..., n = n, clear = clear
260-
) %>%
260+
)
261+
out <- out %>%
261262
purrr::map_dbl(
262263
~ unname(.x$first["elapsed"] / .x$second["elapsed"])
263264
) %>%
264265
mean()
265266

266-
if (clear %in% c("always", "final")) {
267-
clear_testthat_cache()
268-
}
269267
out
270268
}
271269

272270

273271
n_times_faster_bench <- function(i, x1, x2, fun, ..., n, clear) {
274-
fresh_testthat_cache()
275-
if ((clear == "always") || (clear == "all but last" & n != i)) {
276-
on.exit(clear_testthat_cache())
277-
}
272+
local_test_setup(cache = TRUE)
278273
first <- system.time(fun(x1, ...))
279274

280275
if (is.null(x2)) {
@@ -284,7 +279,8 @@ n_times_faster_bench <- function(i, x1, x2, fun, ..., n, clear) {
284279
}
285280
list(
286281
first = first,
287-
second = second
282+
second = second,
283+
cache = cache_info(format = "tabular")
288284
)
289285
}
290286

@@ -333,9 +329,34 @@ generate_test_samples <- function() {
333329
#' @include ui-caching.R
334330
clear_testthat_cache <- purrr::partial(cache_clear, "testthat", ask = FALSE)
335331
activate_testthat_cache <- purrr::partial(cache_activate, "testthat")
336-
fresh_testthat_cache <- function() {
337-
clear_testthat_cache()
338-
activate_testthat_cache()
332+
333+
#' Establish testing setup for current environment
334+
#'
335+
#' @param cache Whether or not to create and activate a cache in a temporary
336+
#' directory.
337+
#' @param .local_envir The environment to use for scoping.
338+
#' @details
339+
#' * make styler quiet.
340+
local_test_setup <- function(cache = FALSE,
341+
.local_envir = parent.frame()) {
342+
current_cache <- cache_info(format = "tabular")
343+
withr::local_options(
344+
list("styler.quiet" = TRUE, "R.cache.rootPath" = tempfile()),
345+
.local_envir = .local_envir
346+
)
347+
if (cache) {
348+
withr::defer(
349+
{
350+
clear_testthat_cache()
351+
cache_activate(basename(current_cache$location))
352+
if (!current_cache$activated) {
353+
cache_deactivate()
354+
}
355+
},
356+
envir = .local_envir
357+
)
358+
activate_testthat_cache()
359+
}
339360
}
340361

341362
cache_more_specs_default <- function() {

R/ui-caching.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ cache_info <- function(cache_name = NULL, format = "both") {
112112
#' function is doing.
113113
#' @family cache managers
114114
#' @export
115-
cache_activate <- function(cache_name = NULL, verbose = !getOption("styler.quiet", FALSE)) {
115+
cache_activate <- function(cache_name = NULL,
116+
verbose = !getOption("styler.quiet", FALSE)) {
116117
if (!is.null(cache_name)) {
117118
options("styler.cache_name" = cache_name)
118119
} else {

man/local_test_setup.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-cache-high-level-api.R

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
test_that("activated cache brings speedup on style_file() API", {
2+
local_test_setup()
23
skip_on_cran()
34
n <- n_times_faster_with_cache(
45
test_path("reference-objects/caching.R"),
@@ -44,32 +45,41 @@ test_that("activated cache brings speedup on style_text() API on character scala
4445

4546

4647
test_that("trailing line breaks are ignored for caching", {
48+
local_test_setup(cache = TRUE)
4749
text1 <- paste0(text, collapse = "\n")
4850
text2 <- c(paste0(text, collapse = "\n"), "\n", "\n", "\n", "\n")
49-
n <- n_times_faster_with_cache(text1, text2, clear = "all but last")
50-
expect_equal(cache_info()$n, 3)
51+
style_text(text1)
52+
style_text(text2)
53+
expect_equal(cache_info(format = "tabular")$n, 3)
5154
skip_on_cran()
55+
n <- n_times_faster_with_cache(text1, text2)
5256
expect_gt(n, 55)
5357
})
5458

5559
test_that("trailing line breaks are ignored for caching in one scalar", {
60+
local_test_setup(cache = TRUE)
5661
text1 <- paste0(text, collapse = "\n")
5762
text2 <- c(paste0(text, collapse = "\n"), "\n", "\n", "\n", "\n")
58-
n <- n_times_faster_with_cache(text1, text2, clear = "all but last")
59-
expect_equal(cache_info()$n, 3)
63+
style_text(text1)
64+
style_text(text2)
65+
expect_equal(cache_info(format = "tabular")$n, 3)
6066
skip_on_cran()
67+
n <- n_times_faster_with_cache(text1, text2)
6168
expect_gt(n, 55)
6269
})
6370

6471
test_that("trailing line breaks are ignored for caching in one scalar", {
72+
local_test_setup(cache = TRUE)
6573
text1 <- paste0(text, collapse = "\n")
6674
text2 <- paste0(
6775
paste0(text, collapse = "\n"), "\n", "\n", "\n", "\n",
6876
collapse = ""
6977
)
70-
n <- n_times_faster_with_cache(text1, text2, clear = "all but last")
71-
expect_equal(cache_info()$n, 3)
78+
style_text(text1)
79+
style_text(text2)
80+
expect_equal(cache_info(format = "tabular")$n, 3)
7281
skip_on_cran()
82+
n <- n_times_faster_with_cache(text1, text2)
7383
expect_gt(n, 55)
7484
})
7585

@@ -112,31 +122,27 @@ test_that("speedup higher when cached roxygen example code is multiple expressio
112122

113123

114124

115-
capture.output(test_that("no speedup when tranformer changes", {
125+
test_that("no speedup when tranformer changes", {
116126
skip_on_cran()
117-
on.exit(clear_testthat_cache())
118-
fresh_testthat_cache()
127+
local_test_setup()
119128
t1 <- tidyverse_style()
120129
first <- system.time(style_text(text, transformers = t1))
121130
t1 <- tidyverse_style(indent_by = 4)
122131
second <- system.time(style_text(text, transformers = t1))
123132
expect_false(first["elapsed"] / 1.3 > second["elapsed"])
124-
}))
133+
})
125134

126135

127-
capture.output(test_that("unactivated cache does not bring speedup", {
128-
skip_on_cran()
129-
on.exit(clear_testthat_cache())
130-
clear_testthat_cache()
136+
test_that("unactivated cache does not bring speedup", {
137+
skip_on_cran
138+
local_test_setup()
131139
first <- system.time(style_file(test_path("reference-objects/caching.R")))
132140
second <- system.time(style_file(test_path("reference-objects/caching.R")))
133141
expect_false(first["elapsed"] / 4 > second["elapsed"])
134-
}))
142+
})
135143

136-
capture.output(test_that("avoid deleting comments #584 (see commit messages)", {
137-
on.exit(clear_testthat_cache())
138-
clear_testthat_cache()
139-
activate_testthat_cache()
144+
test_that("avoid deleting comments #584 (see commit messages)", {
145+
local_test_setup()
140146
text <- c(
141147
"1 + 1",
142148
"# Comment",
@@ -151,12 +157,10 @@ capture.output(test_that("avoid deleting comments #584 (see commit messages)", {
151157
"NULL"
152158
)
153159
expect_equal(as.character(style_text(text2)), text2)
154-
}))
160+
})
155161

156-
capture.output(test_that("avoid removing roxygen mask (see commit messages in #584)", {
157-
on.exit(clear_testthat_cache())
158-
clear_testthat_cache()
159-
activate_testthat_cache()
162+
test_that("avoid removing roxygen mask (see commit messages in #584)", {
163+
local_test_setup()
160164
text <- c(
161165
"c(",
162166
" 1, 2,",
@@ -176,12 +180,10 @@ capture.output(test_that("avoid removing roxygen mask (see commit messages in #5
176180
"NULL"
177181
)
178182
expect_equal(as.character(style_text(text2)), text2)
179-
}))
183+
})
180184

181-
capture.output(test_that("partial caching of multiple expressions on one line works", {
182-
on.exit(clear_testthat_cache())
183-
clear_testthat_cache()
184-
activate_testthat_cache()
185+
test_that("partial caching of multiple expressions on one line works", {
186+
local_test_setup()
185187
text <- "1"
186188
style_text(text)
187189
text2 <- "1 # comment"
@@ -195,7 +197,7 @@ capture.output(test_that("partial caching of multiple expressions on one line wo
195197
style_text(c("mtcars %>%", "f()"))
196198
final_text <- c("mtcars %>%", " f() #")
197199
expect_equal(as.character(style_text(final_text)), final_text)
198-
}))
200+
})
199201

200202
test_that("cache is deactivated at end of caching related testthat file", {
201203
expect_false(cache_is_activated())

tests/testthat/test-cache-interaction-base-indention.R

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
test_that("base_indention is respected in caching", {
2-
on.exit(clear_testthat_cache())
32
text <- c("1 + 1")
4-
fresh_testthat_cache()
3+
local_test_setup(cache = TRUE)
54
without_indention <- style_text(text)
6-
fresh_testthat_cache()
5+
local_test_setup(cache = TRUE)
76
style_text(text, base_indention = 5)
87
expect_equal(
98
style_text(text),
@@ -12,11 +11,10 @@ test_that("base_indention is respected in caching", {
1211
})
1312

1413
test_that("include_roxygen_exmples is respected in caching", {
15-
on.exit(clear_testthat_cache())
1614
text <- c("#' Roxygen", "#'", "#' @examplesIf", "#' 1+1", "1 + 1")
17-
fresh_testthat_cache()
15+
local_test_setup(cache = TRUE)
1816
with_examples <- style_text(text)
19-
fresh_testthat_cache()
17+
local_test_setup(cache = TRUE)
2018
style_text(text, include_roxygen_examples = FALSE)
2119
expect_equal(
2220
style_text(text, include_roxygen_examples = TRUE),
@@ -26,8 +24,7 @@ test_that("include_roxygen_exmples is respected in caching", {
2624

2725

2826
test_that("expression caching when first expression does not comply", {
29-
on.exit(clear_testthat_cache())
30-
fresh_testthat_cache()
27+
local_test_setup(cache = TRUE)
3128
more <- 'x<- 1
3229
"multi
3330
line string"
@@ -86,8 +83,7 @@ line string"
8683
})
8784

8885
test_that("expression caching when last expression does not comply", {
89-
on.exit(clear_testthat_cache())
90-
fresh_testthat_cache()
86+
local_test_setup(cache = TRUE)
9187
more <- ' x <- 1
9288
"multi
9389
line string"
@@ -120,8 +116,7 @@ line string"
120116
})
121117

122118
test_that("expression caching when middle expression does not comply", {
123-
on.exit(clear_testthat_cache())
124-
fresh_testthat_cache()
119+
local_test_setup(cache = TRUE)
125120
more <- ' x <- 1
126121
"multi
127122
line string"

tests/testthat/test-cache-interaction-more-specs.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
test_that("base_indention is respected in caching", {
2-
on.exit(clear_testthat_cache())
2+
local_test_setup(cache = TRUE)
33
text <- c("1 + 1")
4-
fresh_testthat_cache()
54
without_indention <- style_text(text)
6-
fresh_testthat_cache()
5+
local_test_setup(cache = TRUE)
76
style_text(text, base_indention = 5)
87
expect_equal(
98
style_text(text),

tests/testthat/test-cache-interaction-roxygen-code-examples.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
test_that("roxzgen code examples are written to cache as both individual expressions and as whole text", {
2-
on.exit(clear_testthat_cache())
3-
fresh_testthat_cache()
2+
local_test_setup(cache = TRUE)
43
more_specs <- cache_more_specs_default()
54
text <- c(
65
"#' Comment",
@@ -13,7 +12,7 @@ test_that("roxzgen code examples are written to cache as both individual express
1312
"103"
1413
)
1514
styled <- style_text(text)
16-
expect_equal(cache_info()$n, 6)
15+
expect_equal(cache_info(format = "tabular")$n, 6)
1716
# 1 whole (with comments)
1817
# 1code whole
1918
# 1 code by expr
@@ -46,8 +45,7 @@ test_that("roxzgen code examples are written to cache as both individual express
4645

4746
test_that("roxzgen code examples are written to cache as whole expressions bring speedgain", {
4847
skip_on_cran()
49-
on.exit(clear_testthat_cache())
50-
fresh_testthat_cache()
48+
local_test_setup(cache = TRUE)
5149
text <- readLines(test_path("cache-with-r-cache/roxygen-cache-1.R"))
5250
first <- system.time(styled <- style_text(text))
5351
# don't use full cache, only roxygen cache

tests/testthat/test-cache-low-level-api.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ test_that("caching utils make right blocks with comments", {
5757

5858

5959
test_that("blank lines are correctly identified", {
60-
on.exit(clear_testthat_cache())
61-
fresh_testthat_cache()
60+
local_test_setup(cache = TRUE)
6261

6362
text <- c(
6463
"1 + 1",
@@ -121,10 +120,9 @@ test_that("caching utils make right blocks with comments", {
121120
################################################################################
122121

123122
test_that("Individual comment expressions are not cached", {
124-
on.exit(clear_testthat_cache())
125-
fresh_testthat_cache()
123+
local_test_setup(cache = TRUE)
126124
style_text(c("# g", "1"))
127-
cache_info <- cache_info()
125+
cache_info <- cache_info(format = "tabular")
128126
# because output text is cached as a whole, there should be 2 cached
129127
# expressions now
130128
expect_equal(cache_info$n, 2)

0 commit comments

Comments
 (0)