Repository navigation
Description
This is #773 reborn, in a way.
Yes, skip_if_not(getRversion() >= "4.2.0") is pretty straightforward if you know about getRversion() and that there's a coercion method for strings that makes this work (quite infamously recently we found many people doing getRversion() >= 4.2 & subsequently booted from CRAN 😄).
But empirically, I have seen many different versions of equivalent skips. I think it's a common enough reason to skip tests that it warrants inclusion (alongside the similar skip_if_not_installed()). I have also seen a few packages implement similar helpers; centralizing that logic in {testthat} also makes sense to me. E.g. here's {lintr}'s version:
Here are some other similar helpers:
https://github.com/cran/prt/blob/42ab58a2e712350735bd9eb54c6ff092e3192356/tests/testthat/helper-functions.R#L17-L21
https://github.com/cran/duckdb/blob/8bd532e6576edb85df225f3b7335ef90c17e92d0/tests/testthat/test-backend-dbplyr__duckdb_connection.R#L1-L5
Here are hits for a few iterations of ways this is done:
- "canonical-ish": 218 hits. Using
getRversion(). But also notice hits likeskip_if(grepl("...", getRversion())): https://github.com/search?q=org%3Acran%20path%3Atestthat%20%2Fskip.*getRversion%2F&type=code - Messing around with
R.version(). Messy. 42 hits. https://github.com/search?q=org%3Acran+path%3Atestthat+%2Fskip.*R%5C.Version%2F&type=code skip_if_not_installed()on a default package, e.g.skip_if_not_installed("base", "4.0.0"): 42 hits. Esoteric. https://github.com/search?q=org%3Acran+path%3Atestthat+%2Fskip_if_not_installed%5C%28%5Cs*%5B%22%27%5D%28base%7Cutils%7Ctools%7Cstats%7Cgraphics%7CgrDevices%7Cdatasets%7Cmethods%29%2F&type=code
Even allowing that using getRversion() is common enough & the "correct" way, a dedicated skip_if_not_r_version() also allows making a neater message in the test logs.
c.f.
testthat::skip_if_not(getRversion() >= '6.6.6')
# Error: Reason: getRversion() >= "6.6.6" is not TRUE
lintr::skip_if_not_r_version('6.6.6')
# Error: Reason: R version at least 6.6.6 is required
Activity
hadley commentedon Jul 16, 2025
I'm open to this, but I'd like to figure out a better name, ideally something that's a bit shorter and conveys
>=.MichaelChirico commentedon Jul 16, 2025
skip_for_r_before("4.1.0")?I also threw it to the LLMs:
skip_if_r_belowskip_unless_r_versionskip_if_min_rskip_on_r_older_thanskip_if_r_version_belowskip_if_r_version_ltskip_r_beforeskip_unless_rskip_if_old_rskip_if_r_belowskip_if_r_ltskip_before_rskip_r_belowskip_if_old_rhadley commentedon Jul 16, 2025
I kind of like
skip_unless_r("4.2.0")MichaelChirico commentedon Jul 16, 2025
Hm, that's among the ones I don't like, since it reads like it only works on one R version instead of a range.
MichaelChirico commentedon Jul 16, 2025
Another option that might require a {backports} dependency would be something like
That requires us to do some maintenance on the backend for interpreting strings like that, but it is pretty transparent to the reader about the nature of the skip. The other downside besides the highly manual implementation is that it's less clear when we can remove those skips over time (whereas when I do, say, update
R>=4.1.0toR>=4.2.0, I can just regex search for4[.][12][.]0instead of having to remember specific associated features).hadley commentedon Jul 16, 2025
Based on an initial misreading of your suggestion what about
skip_r_unless(">= 4.2.0")I do in principle like the idea of requiring specific functions, but sometimes it's an arg that's important, and I agree that it gets harder to maintain because you can't easily tell when to remove it.
MichaelChirico commentedon Jul 16, 2025
skip_unless_r(">= 4.2.0")sounds preferable toskip_r_unless(">= 4.2.0")to me. Would any operator besides">="be supported (or even needed)? I guess that's just likegtexcept by putting it in the string, we can use the non-syntactic symbol and not rely on user inferringgt-->greater thanhadley commentedon Jul 16, 2025
Oh yeah,
skip_unless_r()is definitely better, and I think we could fairly easily support all the operators, just for completeness.Add `skip_unless_r()` (#2094)