Skip to content

Commit

Permalink
Merge pull request #1031 from r-lib/issue-1027
Browse files Browse the repository at this point in the history
Run some tests sequentially
  • Loading branch information
IndrajeetPatil authored Oct 16, 2022
2 parents b80f5fb + ddba306 commit 38e4b1f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 45 deletions.
8 changes: 8 additions & 0 deletions R/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,11 @@ test_transformers_drop <- function(transformers) {
}
})
}


skip_during_parallel <- function() {
Sys.getenv("STYLER_TEST_IS_TRULY_PARALLEL", TRUE) %>%
toupper() %>%
as.logical() %>%
testthat::skip_if()
}
6 changes: 5 additions & 1 deletion tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
library(testthat)
library(styler)
test_check("styler") # checks multiple files, in parallel

test_check("styler")
# checks file one by one, not parallel
Sys.setenv(STYLER_TEST_IS_TRULY_PARALLEL = FALSE)
test_file("testthat/test-cache-high-level-api.R")
test_file("testthat/tests-cache-require-serial.R")
10 changes: 9 additions & 1 deletion tests/testthat/test-cache-high-level-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ test_that("activated cache brings speedup on style_file() API", {
local_test_setup()
skip_on_cran()
skip_on_covr()
skip_during_parallel()
n <- n_times_faster_with_cache(
test_path("reference-objects/caching.R"),
test_path("reference-objects/caching.R"),
Expand All @@ -27,6 +28,7 @@ text <- c(
test_that("activated cache brings speedup on style_text() API on character vector", {
skip_on_cran()
skip_on_covr()
skip_during_parallel()
n <- n_times_faster_with_cache(
text, text,
fun = style_text
Expand All @@ -37,6 +39,7 @@ test_that("activated cache brings speedup on style_text() API on character vecto
test_that("activated cache brings speedup on style_text() API on character scalar", {
skip_on_cran()
skip_on_covr()
skip_during_parallel()
text2 <- paste0(text, collapse = "\n")

n <- n_times_faster_with_cache(
Expand All @@ -56,6 +59,7 @@ test_that("trailing line breaks are ignored for caching", {
expect_equal(cache_info(format = "tabular")$n, 3)
skip_on_cran()
skip_on_covr()
skip_during_parallel()
n <- n_times_faster_with_cache(text1, text2)
expect_gt(n, 55)
})
Expand All @@ -69,6 +73,7 @@ test_that("trailing line breaks are ignored for caching in one scalar", {
expect_equal(cache_info(format = "tabular")$n, 3)
skip_on_cran()
skip_on_covr()
skip_during_parallel()
n <- n_times_faster_with_cache(text1, text2)
expect_gt(n, 55)
})
Expand All @@ -85,14 +90,15 @@ test_that("trailing line breaks are ignored for caching in one scalar", {
expect_equal(cache_info(format = "tabular")$n, 3)
skip_on_cran()
skip_on_covr()
skip_during_parallel()
n <- n_times_faster_with_cache(text1, text2)
expect_gt(n, 55)
})

test_that("speedup higher when cached roxygen example code is multiple expressions", {
skip_on_cran()
skip_on_covr()

skip_during_parallel()
text_long <- c(
"#' Roxygen",
"#' Comment",
Expand Down Expand Up @@ -132,6 +138,7 @@ test_that("speedup higher when cached roxygen example code is multiple expressio
test_that("no speedup when tranformer changes", {
skip_on_cran()
skip_on_covr()
skip_during_parallel()
local_test_setup()
t1 <- tidyverse_style()
first <- system.time(style_text(text, transformers = t1))
Expand All @@ -145,6 +152,7 @@ test_that("unactivated cache does not bring speedup", {
skip_on_cran()
skip_on_covr()
local_test_setup()
skip_during_parallel()
first <- system.time(style_file(test_path("reference-objects/caching.R")))
second <- system.time(style_file(test_path("reference-objects/caching.R")))
expect_false(first["elapsed"] / 4 > second["elapsed"])
Expand Down
12 changes: 0 additions & 12 deletions tests/testthat/test-cache-interaction-roxygen-code-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ test_that("roxzgen code examples are written to cache as both individual express
)
})


test_that("roxzgen code examples are written to cache as whole expressions bring speedgain", {
skip_on_cran()
local_test_setup(cache = TRUE)
text <- readLines(test_path("cache-with-r-cache/roxygen-cache-1.R"))
first <- system.time(styled <- style_text(text))
# don't use full cache, only roxygen cache
styled[1] <- "#' This is a nother text"
second <- system.time(style_text(styled))
expect_gt(first["elapsed"], 4 * second["elapsed"])
})

test_that("cache is deactivated at end of caching related testthat file", {
expect_false(cache_is_activated())
})
Expand Down
31 changes: 0 additions & 31 deletions tests/testthat/test-cache-with-r-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,6 @@ test_that("Cache management works", {
expect_error(cache_clear("testthat", ask = FALSE), NA)
})

test_that("top-level test: Caches top-level expressions efficiently on style_text()", {
local_test_setup(cache = TRUE)
text <- test_path("cache-with-r-cache/mlflow-1-in.R") %>%
readLines()
benchmark <- system.time(text_styled <- as.character(style_text(text)))
expect_equal(text, text_styled)
full_cached_benchmark <- system.time(text_styled2 <- as.character(style_text(text_styled)))
expect_equal(text, text_styled2)

# modify one function declaration
text_styled[2] <- gsub(")", " )", text_styled[2], fixed = TRUE)
partially_cached_benchmark <- system.time(
text_cached_partially <- as.character(style_text(text_styled))
)
expect_equal(text, text_cached_partially)
cache_deactivate()
not_cached_benchmark <- system.time(
text_not_cached <- as.character(style_text(text_styled))
)
expect_equal(text, text_not_cached)

skip_on_cran()
skip_on_covr()
expect_lt(
partially_cached_benchmark["elapsed"] * 2.4,
not_cached_benchmark["elapsed"]
)
expect_lt(full_cached_benchmark["elapsed"] * 45, benchmark["elapsed"])
})


test_that("cached expressions are displayed propperly", {
skip_if(getRversion() < "4.2")

Expand Down
42 changes: 42 additions & 0 deletions tests/testthat/tests-cache-require-serial.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

test_that("top-level test: Caches top-level expressions efficiently on style_text()", {
local_test_setup(cache = TRUE)
text <- test_path("cache-with-r-cache/mlflow-1-in.R") %>%
readLines()
benchmark <- system.time(text_styled <- as.character(style_text(text)))
expect_equal(text, text_styled)
full_cached_benchmark <- system.time(text_styled2 <- as.character(style_text(text_styled)))
expect_equal(text, text_styled2)

# modify one function declaration
text_styled[2] <- gsub(")", " )", text_styled[2], fixed = TRUE)
partially_cached_benchmark <- system.time(
text_cached_partially <- as.character(style_text(text_styled))
)
expect_equal(text, text_cached_partially)
cache_deactivate()
not_cached_benchmark <- system.time(
text_not_cached <- as.character(style_text(text_styled))
)
expect_equal(text, text_not_cached)

skip_on_cran()
skip_on_covr()
expect_lt(
partially_cached_benchmark["elapsed"] * 2.4,
not_cached_benchmark["elapsed"]
)
expect_lt(full_cached_benchmark["elapsed"] * 45, benchmark["elapsed"])
})


test_that("roxzgen code examples are written to cache as whole expressions bring speedgain", {
skip_on_cran()
local_test_setup(cache = TRUE)
text <- readLines(test_path("cache-with-r-cache/roxygen-cache-1.R"))
first <- system.time(styled <- style_text(text))
# don't use full cache, only roxygen cache
styled[1] <- "#' This is a nother text"
second <- system.time(style_text(styled))
expect_gt(first["elapsed"], 4 * second["elapsed"])
})

0 comments on commit 38e4b1f

Please sign in to comment.