Skip to content

Make desc filtering more flexible and support it in test_dir() - #2379

Open
MichaelChirico wants to merge 3 commits into
r-lib:mainfrom
MichaelChirico:desc-filter
Open

MichaelChirico wants to merge 3 commits into
r-lib:mainfrom
MichaelChirico:desc-filter

Conversation

@MichaelChirico

@MichaelChirico MichaelChirico commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2328; written by Gemini. The tests look roughly correct to me, though I don't follow the internals of snapshotting too too well; and the R/ implementation also makes sense, though I am even less familiar with any subtleties to be on the watch for there.

  • Match desc via fixed substring matching (grepl(..., fixed = TRUE)) rather than exact equality, and allow zero or multiple matches instead of erroring.
  • Support desc in test_dir(), test_local(), test_package(), test_check(), source_dir(), and parallel test execution, skipping files with zero matches before invoking reporters.
  • Update SnapshotReporter to clear old snapshots per executed test in start_test() when desc is active and to skip snapshot_cleanup() in end_reporter(), preventing snapshot duplication or deletion of untouched file snapshots.

Here is also a minimal end-to-end example using --test_filter to demonstrate how that workflow looks like

MODULE.bazel

module(name = "testthat_filter_demo")

bazel_dep(name = "rules_shell", version = "0.6.1")

BUILD.bazel

load("@rules_shell//shell:sh_test.bzl", "sh_test")

sh_test(
    name = "r_tests",
    size = "small",
    srcs = ["tests/testthat.R"],
    data = glob(["tests/testthat/test-*.R"]),
)

tests/testthat.R (chmod +x; for an installed R package, replace test_dir(...) with test_check("mypkg", desc = if (nzchar(desc)) desc))

#!/usr/bin/env Rscript
library(testthat)

desc <- Sys.getenv("TESTBRIDGE_TEST_ONLY")
test_dir(
  "tests/testthat",
  desc = if (nzchar(desc)) desc,
  reporter = "progress"
)

tests/testthat/test-math.R

test_that("addition handles positive numbers", {
  expect_equal(1 + 2, 3)
})

test_that("addition handles negative numbers", {
  expect_equal(-1 + -2, -3)
})

test_that("multiplication works", {
  expect_equal(2 * 3, 6)
})

tests/testthat/test-strings.R

test_that("paste0 handles empty strings", {
  expect_equal(paste0("a", ""), "a")
})

test_that("nchar counts characters", {
  expect_equal(nchar("abc"), 3L)
})

Verified Bazel Runs

# 1. All tests across both files -> math (3) + strings (2) = PASS 5
bazel test //:r_tests --test_output=all

# 2. Partial match in one file (test-strings.R skipped cleanly) -> math (2) = PASS 2
bazel test //:r_tests --test_filter="addition" --test_output=all

# 3. Partial match across both files -> math (2) + strings (1) = PASS 3
bazel test //:r_tests --test_filter="handles" --test_output=all

# 4. Exact single test -> math (1) = PASS 1
bazel test //:r_tests --test_filter="multiplication works" --test_output=all

# 5. Zero matches (exits 0 so multi-target `bazel test //... --test_filter=...` works) -> PASS 0
bazel test //:r_tests --test_filter="nonexistent" --test_output=all

* Match `desc` via fixed substring matching (`grepl(..., fixed = TRUE)`)
  rather than exact equality, and allow zero or multiple matches instead of
  erroring.
* Support `desc` in `test_dir()`, `test_local()`, `test_package()`,
  `test_check()`, `source_dir()`, and parallel test execution, skipping files
  with zero matches before invoking reporters.
* Update `SnapshotReporter` to clear old snapshots per executed test in
  `start_test()` when `desc` is active and to skip `snapshot_cleanup()` in
  `end_reporter()`, preventing snapshot duplication or deletion of untouched
  file snapshots.

Closes r-lib#2328
- Preserve untouched tests in .new.md under desc filtering by seeding
  new_snaps alongside cur_snaps and resetting new_snaps on skip/error
- Lazily seed non-default snapshot variants in take_snapshot() so
  untouched variant files (e.g. other OS / R versions) are never
  modified, preserving snapshots for skipped/errored tests via tests_reset
- Pre-filter test_paths by desc in test_dir() before spawning parallel
  workers so 0-1 matching files run serially
- Validate desc up front in source_dir() and test_dir()

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make desc filter more flexible?

1 participant