test(renv): assert lockfile package names instead of grepping raw JSON#137
Merged
Merged
Conversation
The three `suggested package are not in renv prod ...` tests grepped the lockfile JSON as a flat string, so they matched any textual mention of a package name — including Suggests fields inside another package's metadata. After remotes 2.5.0 added `withr` to its Suggests, the tests became false positives outside R CMD check (`skip_on_cran()` hides them from CI). Switch to parsing the lockfile via `renv:::lockfile()` and asserting membership in `names(...$data()$Packages)`, mirroring the pattern already used at line 274 of the same file.
There was a problem hiding this comment.
Pull request overview
This PR hardens renv lockfile assertions in the test suite by switching from raw JSON string greps to structured lockfile parsing, eliminating false positives caused by package metadata (e.g., Suggests fields).
Changes:
- Parse
renv.lockviagetFromNamespace("lockfile", "renv")(... )$data()$Packagesin three tests. - Replace
grepl()checks on collapsed JSON text with%in%checks against top-level lockfile package names. - Add clarifying test comments explaining the false-positive scenario being avoided.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Three tests in
tests/testthat/test-renv_create.R(suggested package are not in renv prod, lines 316/416/467 on main) grep the lockfile JSON as a flat string:This matches any textual mention of
withrin the file — including aSuggests:field inside another package's metadata. Afterremotes 2.5.0shipped withwithrlisted in its Suggests, every prod-lockfile run pulls inremotes(it's used byattachment::set_remotes), and the grep finds:…which is a metadata mention, not a top-level package of the lockfile.
skip_on_cran()hides the failures from R CMD check, so CI stayed green; the fails only surface when runningdevtools::test()locally withNOT_CRAN=true.Fix
Switch to parsing the lockfile via
renv:::lockfile()and asserting membership innames(...$data()$Packages). This is the same idiom already used attest-renv_create.R:128-143for the dev/prod lockfile checks higher up in the file — just applied consistently to the three lower test_that blocks too.Three
expect_false(grepl(...))blocks become threeexpect_false("X" %in% pkg_in_lock)blocks. No semantic change in what the tests intend to assert; only the implementation is hardened against textual false positives.Coverage
test-renv_create.R:316—suggested package are not in renv prodtest-renv_create.R:416—suggested package are not in renv prod even from vignettes(withdocument = FALSE)test-renv_create.R:467—suggested package are not in renv prod even from vignettes(withdocument = TRUE)A fourth grepl in the same file (line 371, on
pkg_local_renvwhich is already anames(...)vector) is correct and untouched.Test plan
[ FAIL 3 | WARN 0 | SKIP 1 | PASS 63 ]fortest_file("test-renv_create.R")[ FAIL 0 | WARN 0 | SKIP 1 | PASS 61 ]devtools::test()full suite:[ FAIL 0 | WARN 0 | SKIP 1 | PASS 392 ]skip_on_cran())Independent of #136 — both branches diverge from main and don't touch each other's files.