Skip to content

Commit 0c4187c

Browse files
Merge pull request #1109 from MichaelChirico/split-test
split test-public_api for better sharding
2 parents 0f4a215 + 6fefe71 commit 0c4187c

File tree

5 files changed

+545
-557
lines changed

5 files changed

+545
-557
lines changed

tests/testthat/test-public_api-0.R

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
test_that("styler can style package", {
2+
capture_output(expect_false({
3+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"))
4+
any(styled$changed)
5+
}))
6+
})
7+
8+
test_that("styler can style package and exclude some directories", {
9+
capture_output(
10+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
11+
exclude_dirs = "tests"
12+
)
13+
)
14+
expect_true(nrow(styled) == 1)
15+
expect_false(any(grepl("tests/testthat/test-package-xyz.R", styled$file)))
16+
})
17+
18+
test_that("styler can style package and exclude some sub-directories", {
19+
capture_output(
20+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
21+
exclude_dirs = "tests/testthat"
22+
)
23+
)
24+
expect_true(nrow(styled) == 2)
25+
expect_true(any(grepl("tests/testthat.R", styled$file)))
26+
expect_false(any(grepl("tests/testthat/test-package-xyz.R", styled$file)))
27+
})
28+
29+
30+
31+
test_that("styler can style package and exclude some directories and files", {
32+
capture_output(expect_true({
33+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
34+
exclude_dirs = "tests",
35+
exclude_files = ".Rprofile"
36+
)
37+
nrow(styled) == 1
38+
}))
39+
40+
capture_output(expect_true({
41+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
42+
exclude_dirs = "tests",
43+
exclude_files = "./.Rprofile"
44+
)
45+
nrow(styled) == 1
46+
}))
47+
})
48+
49+
50+
test_that("styler can style directory", {
51+
capture_output(expect_false({
52+
styled <- style_dir(testthat_file("public-api", "xyzdir"))
53+
any(styled$changed)
54+
}))
55+
})
56+
57+
test_that("styler can style directories and exclude", {
58+
capture_output(expect_true({
59+
styled <- style_dir(
60+
testthat_file("public-api", "renvpkg"),
61+
exclude_dirs = "renv"
62+
)
63+
nrow(styled) == 2
64+
}))
65+
capture_output(expect_true({
66+
styled <- style_dir(
67+
testthat_file("public-api", "renvpkg"),
68+
exclude_dirs = c("renv", "tests/testthat")
69+
)
70+
nrow(styled) == 1
71+
}))
72+
73+
capture_output(expect_true({
74+
styled <- style_dir(
75+
testthat_file("public-api", "renvpkg"),
76+
exclude_dirs = "./renv"
77+
)
78+
nrow(styled) == 2
79+
}))
80+
81+
capture_output(expect_true({
82+
styled <- style_dir(
83+
testthat_file("public-api", "renvpkg"),
84+
exclude_dirs = "./renv", recursive = FALSE
85+
)
86+
nrow(styled) == 0
87+
}))
88+
89+
capture_output(expect_true({
90+
styled <- style_dir(
91+
testthat_file("public-api", "renvpkg"),
92+
recursive = FALSE
93+
)
94+
nrow(styled) == 0
95+
}))
96+
})

tests/testthat/test-public_api-1.R

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
test_that("styler can style files", {
2+
# just one
3+
capture_output(expect_equal(
4+
{
5+
out <- style_file(c(
6+
testthat_file("public-api", "xyzfile", "random-script.R")
7+
), strict = FALSE)
8+
out$changed
9+
},
10+
rep(FALSE, 1),
11+
ignore_attr = TRUE
12+
))
13+
# multiple not in the same working directory
14+
capture_output(expect_equal(
15+
{
16+
out <- style_file(c(
17+
testthat_file("public-api", "xyzfile", "random-script.R"),
18+
testthat_file("public-api", "xyzfile", "subfolder", "random-script.R")
19+
), strict = FALSE)
20+
out$changed
21+
},
22+
rep(FALSE, 2),
23+
ignore_attr = TRUE
24+
))
25+
})
26+
27+
28+
test_that("styler does not return error when there is no file to style", {
29+
capture_output(expect_error(style_dir(
30+
testthat_file("public-api", "xyzemptydir"),
31+
strict = FALSE
32+
), NA))
33+
})
34+
35+
36+
37+
test_that("styler can style Rmd file", {
38+
expect_false({
39+
out <- style_file(
40+
testthat_file("public-api", "xyzfile_rmd", "random.Rmd"),
41+
strict = FALSE
42+
)
43+
out$changed
44+
})
45+
46+
styled <- style_file(
47+
testthat_file("public-api", "xyzfile_rmd", "random2.Rmd"),
48+
strict = FALSE
49+
)
50+
expect_false(styled$changed)
51+
})
52+
53+
test_that("styler can style Rmarkdown file", {
54+
expect_false({
55+
out <- style_file(
56+
testthat_file("public-api", "xyzfile_rmd", "random.Rmarkdown"),
57+
strict = FALSE
58+
)
59+
out$changed
60+
})
61+
62+
63+
styled <- style_file(
64+
testthat_file("public-api", "xyzfile_rmd", "random2.Rmarkdown"),
65+
strict = FALSE
66+
)
67+
expect_false(styled$changed)
68+
})
69+
70+
71+
test_that("styler can style qmd file", {
72+
expect_false({
73+
out <- style_file(
74+
testthat_file("public-api", "xyzfile_qmd", "new.qmd"),
75+
strict = FALSE
76+
)
77+
out$changed
78+
})
79+
80+
styled <- style_file(
81+
testthat_file("public-api", "xyzfile_rmd", "random2.Rmarkdown"),
82+
strict = FALSE
83+
)
84+
expect_false(styled$changed)
85+
})
86+
87+
test_that("styler handles malformed Rmd file and invalid R code in chunk", {
88+
capture_output(expect_warning(
89+
style_file(testthat_file("public-api", "xyzfile_rmd", "invalid4.Rmd"), strict = FALSE),
90+
"3: "
91+
))
92+
93+
capture_output(expect_warning(
94+
style_file(testthat_file("public-api", "xyzfile_rmd", "invalid7.Rmd"), strict = FALSE),
95+
"Malformed file"
96+
))
97+
})
98+
99+
100+
101+
102+
test_that("messages (via cat()) of style_file are correct", {
103+
for (encoding in ls_testable_encodings()) {
104+
withr::with_options(
105+
list(cli.unicode = encoding == "utf8"),
106+
{
107+
# Message if scope > line_breaks and code changes
108+
expect_snapshot({
109+
cat(catch_style_file_output(file.path(
110+
"public-api",
111+
"xyzdir-dirty",
112+
"dirty-sample-with-scope-tokens.R"
113+
)), sep = "\n")
114+
})
115+
116+
# No message if scope > line_breaks and code does not change
117+
expect_snapshot({
118+
cat(catch_style_file_output(file.path(
119+
"public-api", "xyzdir-dirty", "clean-sample-with-scope-tokens.R"
120+
)), sep = "\n")
121+
})
122+
123+
# No message if scope <= line_breaks even if code is changed.
124+
expect_snapshot({
125+
cat(catch_style_file_output(file.path(
126+
"public-api", "xyzdir-dirty", "dirty-sample-with-scope-spaces.R"
127+
)), sep = "\n")
128+
})
129+
}
130+
)
131+
}
132+
})
133+
134+
test_that("Messages can be suppressed", {
135+
withr::with_options(
136+
list(styler.quiet = TRUE),
137+
{
138+
output <- catch_style_file_output(file.path(
139+
"public-api", "xyzdir-dirty", "dirty-sample-with-scope-spaces.R"
140+
))
141+
expect_equal(output, character(0))
142+
}
143+
)
144+
})

tests/testthat/test-public_api-2.R

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
test_that("styler can style R, Rmd and Rmarkdown files via style_dir()", {
2+
msg <- capture_output(
3+
style_dir(testthat_file("public-api", "xyz-r-and-rmd-dir"),
4+
filetype = c("R", "Rmd", "Rmarkdown")
5+
)
6+
)
7+
expect_true(any(grepl("random-script-in-sub-dir.R", msg, fixed = TRUE)))
8+
expect_true(any(grepl("random-rmd-script.Rmd", msg, fixed = TRUE)))
9+
expect_true(any(grepl("random-rmd-script.Rmarkdown", msg, fixed = TRUE)))
10+
})
11+
12+
test_that("styler can style Rmd files only via style_dir()", {
13+
msg <- capture_output(
14+
style_dir(testthat_file("public-api", "xyz-r-and-rmd-dir"),
15+
filetype = "Rmd"
16+
)
17+
)
18+
expect_true(any(grepl("random-rmd-script.Rmd", msg, fixed = TRUE)))
19+
expect_false(any(grepl("random-script-in-sub-dir.R", msg, fixed = TRUE)))
20+
expect_false(any(grepl("random-rmd-script.Rmarkdown", msg, fixed = TRUE)))
21+
})
22+
23+
test_that("styler can style .r and .rmd files only via style_dir()", {
24+
msg <- capture_output(
25+
style_dir(testthat_file("public-api", "xyz-r-and-rmd-dir"),
26+
filetype = c(".r", ".rmd")
27+
)
28+
)
29+
expect_true(any(grepl("random-script-in-sub-dir.R", msg, fixed = TRUE)))
30+
expect_true(any(grepl("random-rmd-script.Rmd", msg, fixed = TRUE)))
31+
expect_false(any(grepl("random-rmd-script.Rmarkdown", msg, fixed = TRUE)))
32+
})
33+
34+
35+
36+
test_that("styler can style R and Rmd files via style_pkg()", {
37+
msg <- capture_output(
38+
style_pkg(testthat_file("public-api", "xyzpackage-rmd"),
39+
filetype = c("R", "Rmd", "Rmarkdown")
40+
)
41+
)
42+
expect_true(any(grepl("hello-world.R", msg, fixed = TRUE)))
43+
expect_true(any(grepl("test-package-xyz.R", msg, fixed = TRUE)))
44+
expect_true(any(grepl("random.Rmd", msg, fixed = TRUE)))
45+
expect_true(any(grepl("random.Rmarkdown", msg, fixed = TRUE)))
46+
expect_true(any(grepl("README.Rmd", msg, fixed = TRUE)))
47+
expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE)))
48+
})
49+
50+
test_that("style_pkg() styles qmd files by default", {
51+
msg <- capture_output(
52+
style_pkg(testthat_file("public-api", "xyzpackage-qmd"))
53+
)
54+
expect_true(any(grepl("hello-world.R", msg, fixed = TRUE)))
55+
expect_true(any(grepl("test-package-xyz.R", msg, fixed = TRUE)))
56+
expect_true(any(grepl("random.Rmd", msg, fixed = TRUE)))
57+
expect_true(any(grepl("random.Rmarkdown", msg, fixed = TRUE)))
58+
expect_true(any(grepl("README.Rmd", msg, fixed = TRUE)))
59+
expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE)))
60+
expect_true(any(grepl("new.qmd", msg, fixed = TRUE)))
61+
})
62+
63+
test_that("style_pkg() can find qmd anywhere", {
64+
msg <- capture_output(
65+
style_pkg(testthat_file("public-api", "xyzpackage-qmd"),
66+
filetype = ".Qmd"
67+
)
68+
)
69+
expect_no_match(msg, "hello-world.R", fixed = TRUE)
70+
expect_no_match(msg, "test-package-xyz.R", fixed = TRUE)
71+
expect_no_match(msg, "random.Rmd", fixed = TRUE)
72+
expect_no_match(msg, "random.Rmarkdown", fixed = TRUE)
73+
expect_no_match(msg, "README.Rmd", fixed = TRUE)
74+
expect_no_match(msg, "RcppExports.R", fixed = TRUE)
75+
expect_match(msg, "new.qmd", fixed = TRUE)
76+
})
77+
78+
79+
test_that("styler can style Rmd files only via style_pkg()", {
80+
msg <- capture_output(
81+
style_pkg(testthat_file("public-api", "xyzpackage-rmd"),
82+
filetype = "Rmd"
83+
)
84+
)
85+
expect_false(any(grepl("hello-world.R", msg, fixed = TRUE)))
86+
expect_false(any(grepl("test-package-xyz.R", msg, fixed = TRUE)))
87+
expect_true(any(grepl("random.Rmd", msg, fixed = TRUE)))
88+
expect_false(any(grepl("random.Rmarkdown", msg, fixed = TRUE)))
89+
expect_true(any(grepl("README.Rmd", msg, fixed = TRUE)))
90+
expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE)))
91+
})
92+
93+
test_that("styler can style Rmarkdown files only via style_pkg()", {
94+
msg <- capture_output(
95+
style_pkg(testthat_file("public-api", "xyzpackage-rmd"),
96+
filetype = "Rmarkdown"
97+
)
98+
)
99+
expect_false(any(grepl("hello-world.R", msg, fixed = TRUE)))
100+
expect_false(any(grepl("test-package-xyz.R", msg, fixed = TRUE)))
101+
expect_false(any(grepl("random.Rmd", msg, fixed = TRUE)))
102+
expect_true(any(grepl("random.Rmarkdown", msg, fixed = TRUE)))
103+
expect_false(any(grepl("README.Rmd", msg, fixed = TRUE)))
104+
expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE)))
105+
})
106+
107+
test_that("insufficient R version returns error", {
108+
expect_error(stop_insufficient_r_version())
109+
})

0 commit comments

Comments
 (0)