Skip to content

Commit 95635f6

Browse files
authored
Skipping overhaul (#1761)
* Use `IS_BIOC_BUILD_MACHINE` for `skip_on_bioc()`. Fixes #1712. * `skip()` gains default message. Fixes #1586. * Overhaul testing and get as close as possible to 100% test coverage. * Replace mockery with `local_mocked_bindings()`. * Move superseded skips to own doc.
1 parent d6c4b1d commit 95635f6

File tree

8 files changed

+269
-125
lines changed

8 files changed

+269
-125
lines changed

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Suggests:
4242
curl (>= 0.9.5),
4343
diffviewer (>= 0.1.0),
4444
knitr,
45-
mockery,
4645
rmarkdown,
4746
rstudioapi,
4847
shiny,

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# testthat (development version)
22

3+
* `skip_on_bioc()` now uses the documented environment variable
4+
(`IS_BIOC_BUILD_MACHINE`) (#1712).
5+
36
* `test_path()` now works when called within helper files (#1562).
47

58
* `it()` now calls `local_test_context()` so that it behaves more

R/expect-self-test.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ expect_failure <- function(expr, message = NULL, ...) {
4747
invisible(NULL)
4848
}
4949

50-
expect_skip <- function(code, regexp = NULL) {
51-
expect_condition(code, regexp, class = "skip")
50+
expect_snapshot_skip <- function(x) {
51+
expect_snapshot_error(x, class = "skip")
5252
}
53+
expect_no_skip <- function(code) {
54+
expect_no_condition(code, class = "skip")
55+
}
56+
5357

5458
#' @export
5559
#' @rdname expect_success

R/skip.R

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
#'
2323
#' * `skip_if_translated("msg")` skips tests if the "msg" is translated.
2424
#'
25-
#' * `skip_on_bioc()` skips on Bioconductor (using the `BBS_HOME` env var).
25+
#' * `skip_on_bioc()` skips on Bioconductor (using the `IS_BIOC_BUILD_MACHINE`
26+
#' env var).
2627
#'
2728
#' * `skip_on_cran()` skips on CRAN (using the `NOT_CRAN` env var set by
2829
#' devtools and friends).
2930
#'
3031
#' * `skip_on_covr()` skips when covr is running (using the `R_COVR` env var).
3132
#'
3233
#' * `skip_on_ci()` skips on continuous integration systems like GitHub Actions,
33-
#' travis, and appveyor (using the `CI` env var). It supersedes the older
34-
#' `skip_on_travis()` and `skip_on_appveyor()` functions.
34+
#' travis, and appveyor (using the `CI` env var).
3535
#'
3636
#' * `skip_on_os()` skips on the specified operating system(s) ("windows",
3737
#' "mac", "linux", or "solaris").
@@ -48,7 +48,7 @@
4848
#' expect_equal(1, 2) # this one skipped
4949
#' expect_equal(1, 3) # this one is also skipped
5050
#' })
51-
skip <- function(message) {
51+
skip <- function(message = "Skipping") {
5252
message <- paste0(message, collapse = "\n")
5353
cond <- structure(
5454
list(message = paste0("Reason: ", message)),
@@ -76,6 +76,8 @@ skip_if_not <- function(condition, message = NULL) {
7676
}
7777
if (!isTRUE(condition)) {
7878
skip(message)
79+
} else {
80+
invisible()
7981
}
8082
}
8183

@@ -87,6 +89,8 @@ skip_if <- function(condition, message = NULL) {
8789
}
8890
if (isTRUE(condition)) {
8991
skip(message)
92+
} else {
93+
invisible()
9094
}
9195
}
9296

@@ -109,18 +113,18 @@ skip_if_not_installed <- function(pkg, minimum_version = NULL) {
109113
}
110114
}
111115

112-
return(invisible(TRUE))
116+
invisible()
113117
}
114118

115119
#' @export
116120
#' @rdname skip
117121
skip_if_offline <- function(host = "r-project.org") {
118122
skip_on_cran()
119123
skip_if_not_installed("curl")
120-
has_internet <- !is.null(curl::nslookup(host, error = FALSE))
121-
if (!has_internet) {
122-
skip("offline")
123-
}
124+
skip_if_not(has_internet(host), "offline")
125+
}
126+
has_internet <- function(host) {
127+
!is.null(curl::nslookup(host, error = FALSE))
124128
}
125129

126130
#' @export
@@ -129,8 +133,6 @@ skip_on_cran <- function() {
129133
skip_if(on_cran(), "On CRAN")
130134
}
131135

132-
on_cran <- function() !identical(Sys.getenv("NOT_CRAN"), "true")
133-
134136
#' @export
135137
#' @param os Character vector of one or more operating systems to skip on.
136138
#' Supported values are `"windows"`, `"mac"`, `"linux"`, and `"solaris"`.
@@ -175,62 +177,22 @@ skip_on_os <- function(os, arch = NULL) {
175177
system_os <- function() tolower(Sys.info()[["sysname"]])
176178
system_arch <- function() R.version$arch
177179

178-
#' @export
179-
#' @rdname skip
180-
skip_on_travis <- function() {
181-
if (!identical(Sys.getenv("TRAVIS"), "true")) {
182-
return(invisible(TRUE))
183-
}
184-
185-
skip("On Travis")
186-
}
187-
188-
#' @export
189-
#' @rdname skip
190-
skip_on_appveyor <- function() {
191-
if (!identical(Sys.getenv("APPVEYOR"), "True")) {
192-
return()
193-
}
194-
195-
skip("On Appveyor")
196-
}
197-
198180
#' @export
199181
#' @rdname skip
200182
skip_on_ci <- function() {
201-
if (!on_ci()) {
202-
return(invisible(TRUE))
203-
}
204-
205-
skip("On CI")
206-
}
207-
208-
on_ci <- function() {
209-
isTRUE(as.logical(Sys.getenv("CI")))
210-
}
211-
212-
in_covr <- function() {
213-
identical(Sys.getenv("R_COVR"), "true")
183+
skip_if(on_ci(), "On CI")
214184
}
215185

216186
#' @export
217187
#' @rdname skip
218188
skip_on_covr <- function() {
219-
if (! in_covr()) {
220-
return(invisible(TRUE))
221-
}
222-
223-
skip("On covr")
189+
skip_if(in_covr(), "On covr")
224190
}
225191

226192
#' @export
227193
#' @rdname skip
228194
skip_on_bioc <- function() {
229-
if (identical(Sys.getenv("BBS_HOME"), "")) {
230-
return(invisible(TRUE))
231-
}
232-
233-
skip("On Bioconductor")
195+
skip_if(on_bioc(), "On Bioconductor")
234196
}
235197

236198
#' @export
@@ -239,9 +201,47 @@ skip_on_bioc <- function() {
239201
#' [`R-base.pot`](https://github.com/wch/r-source/blob/master/src/library/base/po/R-base.pot).
240202
#' @rdname skip
241203
skip_if_translated <- function(msgid = "'%s' not found") {
242-
if (gettext(msgid, domain = "R") == msgid) {
243-
return(invisible(TRUE))
244-
}
204+
skip_if(
205+
gettext(msgid, domain = "R") != msgid,
206+
paste0("\"", msgid, "\" is translated")
207+
)
208+
}
209+
210+
#' Superseded skip functions
211+
#'
212+
#' @description
213+
#' `r lifecycle::badge("superseded")`
214+
#'
215+
#' * `skip_on_travis()` and `skip_on_appveyor()` have been superseded by
216+
#' [skip_on_ci()].
217+
#'
218+
#' @export
219+
#' @keywords internal
220+
skip_on_travis <- function() {
221+
skip_if(env_var_is_true("TRAVIS"), "On Travis")
222+
}
223+
224+
#' @export
225+
#' @rdname skip_on_travis
226+
skip_on_appveyor <- function() {
227+
skip_if(env_var_is_true("APPVEYOR"), "On Appveyor")
228+
}
229+
230+
# helpers -----------------------------------------------------------------
231+
232+
on_ci <- function() {
233+
env_var_is_true("CI")
234+
}
235+
in_covr <- function() {
236+
env_var_is_true("R_COVR")
237+
}
238+
on_bioc <- function() {
239+
env_var_is_true("IS_BIOC_BUILD_MACHINE")
240+
}
241+
on_cran <- function() {
242+
!env_var_is_true("NOT_CRAN")
243+
}
245244

246-
skip(paste0("\"", msgid, "\" is translated"))
245+
env_var_is_true <- function(x) {
246+
isTRUE(as.logical(Sys.getenv(x, "false")))
247247
}

man/skip.Rd

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

man/skip_on_travis.Rd

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

tests/testthat/_snaps/skip.md

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
# skip on os checks os names
1+
# basic skips work as expected
22

3-
Code
4-
skip_on_os("amiga")
5-
Condition
6-
Error in `match.arg()`:
7-
! 'arg' should be one of "windows", "mac", "linux", "solaris"
3+
Reason: Skipping
4+
5+
---
6+
7+
Reason: TRUE is TRUE
8+
9+
---
10+
11+
Reason: FALSE is not TRUE
12+
13+
---
14+
15+
Reason: empty test
816

917
# autogenerated message is always single line
1018

@@ -16,3 +24,67 @@
1624
a_very_long_argument_name || a_very_long_argument_name ||
1725
a_very_long_argument_name is not TRUE
1826

27+
# skip_if_not_installed() works as expected
28+
29+
Reason: doesntexist cannot be loaded
30+
31+
---
32+
33+
Reason: Installed testthat is version 3.1.7.9000; but 9999.9999.999 is required
34+
35+
---
36+
37+
Reason: offline
38+
39+
# skip_on_cran() works as expected
40+
41+
Reason: On CRAN
42+
43+
# skip_on_ci() works as expected
44+
45+
Reason: On CI
46+
47+
# skip_on_covr() works as expected
48+
49+
Reason: On covr
50+
51+
# skip_on_bioc() works as expected
52+
53+
Reason: On Bioconductor
54+
55+
# superseded CI skips still work
56+
57+
Reason: On Travis
58+
59+
---
60+
61+
Reason: On Appveyor
62+
63+
# skip_if_translated() works as expected
64+
65+
Reason: "'%s' not found" is translated
66+
67+
# skip on os checks os names
68+
69+
Code
70+
skip_on_os("amiga")
71+
Condition
72+
Error in `match.arg()`:
73+
! 'arg' should be one of "windows", "mac", "linux", "solaris"
74+
75+
# can skip on multiple oses
76+
77+
Reason: On Windows
78+
79+
---
80+
81+
Reason: On Windows
82+
83+
# can refine os with arch
84+
85+
Reason: On Windows
86+
87+
---
88+
89+
Reason: On Windows i386
90+

0 commit comments

Comments
 (0)