Skip to content

Commit

Permalink
Merge pull request #1109 from MichaelChirico/split-test
Browse files Browse the repository at this point in the history
split test-public_api for better sharding
  • Loading branch information
lorenzwalthert authored Apr 17, 2023
2 parents 0f4a215 + 6fefe71 commit 0c4187c
Show file tree
Hide file tree
Showing 5 changed files with 545 additions and 557 deletions.
96 changes: 96 additions & 0 deletions tests/testthat/test-public_api-0.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
test_that("styler can style package", {
capture_output(expect_false({
styled <- style_pkg(testthat_file("public-api", "xyzpackage"))
any(styled$changed)
}))
})

test_that("styler can style package and exclude some directories", {
capture_output(
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
exclude_dirs = "tests"
)
)
expect_true(nrow(styled) == 1)
expect_false(any(grepl("tests/testthat/test-package-xyz.R", styled$file)))
})

test_that("styler can style package and exclude some sub-directories", {
capture_output(
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
exclude_dirs = "tests/testthat"
)
)
expect_true(nrow(styled) == 2)
expect_true(any(grepl("tests/testthat.R", styled$file)))
expect_false(any(grepl("tests/testthat/test-package-xyz.R", styled$file)))
})



test_that("styler can style package and exclude some directories and files", {
capture_output(expect_true({
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
exclude_dirs = "tests",
exclude_files = ".Rprofile"
)
nrow(styled) == 1
}))

capture_output(expect_true({
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
exclude_dirs = "tests",
exclude_files = "./.Rprofile"
)
nrow(styled) == 1
}))
})


test_that("styler can style directory", {
capture_output(expect_false({
styled <- style_dir(testthat_file("public-api", "xyzdir"))
any(styled$changed)
}))
})

test_that("styler can style directories and exclude", {
capture_output(expect_true({
styled <- style_dir(
testthat_file("public-api", "renvpkg"),
exclude_dirs = "renv"
)
nrow(styled) == 2
}))
capture_output(expect_true({
styled <- style_dir(
testthat_file("public-api", "renvpkg"),
exclude_dirs = c("renv", "tests/testthat")
)
nrow(styled) == 1
}))

capture_output(expect_true({
styled <- style_dir(
testthat_file("public-api", "renvpkg"),
exclude_dirs = "./renv"
)
nrow(styled) == 2
}))

capture_output(expect_true({
styled <- style_dir(
testthat_file("public-api", "renvpkg"),
exclude_dirs = "./renv", recursive = FALSE
)
nrow(styled) == 0
}))

capture_output(expect_true({
styled <- style_dir(
testthat_file("public-api", "renvpkg"),
recursive = FALSE
)
nrow(styled) == 0
}))
})
144 changes: 144 additions & 0 deletions tests/testthat/test-public_api-1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
test_that("styler can style files", {
# just one
capture_output(expect_equal(
{
out <- style_file(c(
testthat_file("public-api", "xyzfile", "random-script.R")
), strict = FALSE)
out$changed
},
rep(FALSE, 1),
ignore_attr = TRUE
))
# multiple not in the same working directory
capture_output(expect_equal(
{
out <- style_file(c(
testthat_file("public-api", "xyzfile", "random-script.R"),
testthat_file("public-api", "xyzfile", "subfolder", "random-script.R")
), strict = FALSE)
out$changed
},
rep(FALSE, 2),
ignore_attr = TRUE
))
})


test_that("styler does not return error when there is no file to style", {
capture_output(expect_error(style_dir(
testthat_file("public-api", "xyzemptydir"),
strict = FALSE
), NA))
})



test_that("styler can style Rmd file", {
expect_false({
out <- style_file(
testthat_file("public-api", "xyzfile_rmd", "random.Rmd"),
strict = FALSE
)
out$changed
})

styled <- style_file(
testthat_file("public-api", "xyzfile_rmd", "random2.Rmd"),
strict = FALSE
)
expect_false(styled$changed)
})

test_that("styler can style Rmarkdown file", {
expect_false({
out <- style_file(
testthat_file("public-api", "xyzfile_rmd", "random.Rmarkdown"),
strict = FALSE
)
out$changed
})


styled <- style_file(
testthat_file("public-api", "xyzfile_rmd", "random2.Rmarkdown"),
strict = FALSE
)
expect_false(styled$changed)
})


test_that("styler can style qmd file", {
expect_false({
out <- style_file(
testthat_file("public-api", "xyzfile_qmd", "new.qmd"),
strict = FALSE
)
out$changed
})

styled <- style_file(
testthat_file("public-api", "xyzfile_rmd", "random2.Rmarkdown"),
strict = FALSE
)
expect_false(styled$changed)
})

test_that("styler handles malformed Rmd file and invalid R code in chunk", {
capture_output(expect_warning(
style_file(testthat_file("public-api", "xyzfile_rmd", "invalid4.Rmd"), strict = FALSE),
"3: "
))

capture_output(expect_warning(
style_file(testthat_file("public-api", "xyzfile_rmd", "invalid7.Rmd"), strict = FALSE),
"Malformed file"
))
})




test_that("messages (via cat()) of style_file are correct", {
for (encoding in ls_testable_encodings()) {
withr::with_options(
list(cli.unicode = encoding == "utf8"),
{
# Message if scope > line_breaks and code changes
expect_snapshot({
cat(catch_style_file_output(file.path(
"public-api",
"xyzdir-dirty",
"dirty-sample-with-scope-tokens.R"
)), sep = "\n")
})

# No message if scope > line_breaks and code does not change
expect_snapshot({
cat(catch_style_file_output(file.path(
"public-api", "xyzdir-dirty", "clean-sample-with-scope-tokens.R"
)), sep = "\n")
})

# No message if scope <= line_breaks even if code is changed.
expect_snapshot({
cat(catch_style_file_output(file.path(
"public-api", "xyzdir-dirty", "dirty-sample-with-scope-spaces.R"
)), sep = "\n")
})
}
)
}
})

test_that("Messages can be suppressed", {
withr::with_options(
list(styler.quiet = TRUE),
{
output <- catch_style_file_output(file.path(
"public-api", "xyzdir-dirty", "dirty-sample-with-scope-spaces.R"
))
expect_equal(output, character(0))
}
)
})
109 changes: 109 additions & 0 deletions tests/testthat/test-public_api-2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
test_that("styler can style R, Rmd and Rmarkdown files via style_dir()", {
msg <- capture_output(
style_dir(testthat_file("public-api", "xyz-r-and-rmd-dir"),
filetype = c("R", "Rmd", "Rmarkdown")
)
)
expect_true(any(grepl("random-script-in-sub-dir.R", msg, fixed = TRUE)))
expect_true(any(grepl("random-rmd-script.Rmd", msg, fixed = TRUE)))
expect_true(any(grepl("random-rmd-script.Rmarkdown", msg, fixed = TRUE)))
})

test_that("styler can style Rmd files only via style_dir()", {
msg <- capture_output(
style_dir(testthat_file("public-api", "xyz-r-and-rmd-dir"),
filetype = "Rmd"
)
)
expect_true(any(grepl("random-rmd-script.Rmd", msg, fixed = TRUE)))
expect_false(any(grepl("random-script-in-sub-dir.R", msg, fixed = TRUE)))
expect_false(any(grepl("random-rmd-script.Rmarkdown", msg, fixed = TRUE)))
})

test_that("styler can style .r and .rmd files only via style_dir()", {
msg <- capture_output(
style_dir(testthat_file("public-api", "xyz-r-and-rmd-dir"),
filetype = c(".r", ".rmd")
)
)
expect_true(any(grepl("random-script-in-sub-dir.R", msg, fixed = TRUE)))
expect_true(any(grepl("random-rmd-script.Rmd", msg, fixed = TRUE)))
expect_false(any(grepl("random-rmd-script.Rmarkdown", msg, fixed = TRUE)))
})



test_that("styler can style R and Rmd files via style_pkg()", {
msg <- capture_output(
style_pkg(testthat_file("public-api", "xyzpackage-rmd"),
filetype = c("R", "Rmd", "Rmarkdown")
)
)
expect_true(any(grepl("hello-world.R", msg, fixed = TRUE)))
expect_true(any(grepl("test-package-xyz.R", msg, fixed = TRUE)))
expect_true(any(grepl("random.Rmd", msg, fixed = TRUE)))
expect_true(any(grepl("random.Rmarkdown", msg, fixed = TRUE)))
expect_true(any(grepl("README.Rmd", msg, fixed = TRUE)))
expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE)))
})

test_that("style_pkg() styles qmd files by default", {
msg <- capture_output(
style_pkg(testthat_file("public-api", "xyzpackage-qmd"))
)
expect_true(any(grepl("hello-world.R", msg, fixed = TRUE)))
expect_true(any(grepl("test-package-xyz.R", msg, fixed = TRUE)))
expect_true(any(grepl("random.Rmd", msg, fixed = TRUE)))
expect_true(any(grepl("random.Rmarkdown", msg, fixed = TRUE)))
expect_true(any(grepl("README.Rmd", msg, fixed = TRUE)))
expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE)))
expect_true(any(grepl("new.qmd", msg, fixed = TRUE)))
})

test_that("style_pkg() can find qmd anywhere", {
msg <- capture_output(
style_pkg(testthat_file("public-api", "xyzpackage-qmd"),
filetype = ".Qmd"
)
)
expect_no_match(msg, "hello-world.R", fixed = TRUE)
expect_no_match(msg, "test-package-xyz.R", fixed = TRUE)
expect_no_match(msg, "random.Rmd", fixed = TRUE)
expect_no_match(msg, "random.Rmarkdown", fixed = TRUE)
expect_no_match(msg, "README.Rmd", fixed = TRUE)
expect_no_match(msg, "RcppExports.R", fixed = TRUE)
expect_match(msg, "new.qmd", fixed = TRUE)
})


test_that("styler can style Rmd files only via style_pkg()", {
msg <- capture_output(
style_pkg(testthat_file("public-api", "xyzpackage-rmd"),
filetype = "Rmd"
)
)
expect_false(any(grepl("hello-world.R", msg, fixed = TRUE)))
expect_false(any(grepl("test-package-xyz.R", msg, fixed = TRUE)))
expect_true(any(grepl("random.Rmd", msg, fixed = TRUE)))
expect_false(any(grepl("random.Rmarkdown", msg, fixed = TRUE)))
expect_true(any(grepl("README.Rmd", msg, fixed = TRUE)))
expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE)))
})

test_that("styler can style Rmarkdown files only via style_pkg()", {
msg <- capture_output(
style_pkg(testthat_file("public-api", "xyzpackage-rmd"),
filetype = "Rmarkdown"
)
)
expect_false(any(grepl("hello-world.R", msg, fixed = TRUE)))
expect_false(any(grepl("test-package-xyz.R", msg, fixed = TRUE)))
expect_false(any(grepl("random.Rmd", msg, fixed = TRUE)))
expect_true(any(grepl("random.Rmarkdown", msg, fixed = TRUE)))
expect_false(any(grepl("README.Rmd", msg, fixed = TRUE)))
expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE)))
})

test_that("insufficient R version returns error", {
expect_error(stop_insufficient_r_version())
})
Loading

0 comments on commit 0c4187c

Please sign in to comment.