Skip to content

Deprecate check_cassette_names() #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vcr (development version)

### BREAKING CHANGES

* `check_cassette_names()` has been deprecated since it can't be implemented 100% correctly and diagnoses a relatively rare problem (#166).
* `RequestHandler` and its subclasses are no longer exported.
* Internal `real_http_connections_allowed()` is no longer exported and has been removed.
* Internal `Request` and `VcrResponse` classes are no longer exported and have been removed.
Expand Down
15 changes: 8 additions & 7 deletions R/check_cassette_names.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#' Check cassette names
#'
#' @description
#' This function is meant to be run during your tests, from a
#' [`helper-*.R` file](https://testthat.r-lib.org/reference/test_dir.html#special-files)
#' inside the `tests/testthat` directory.
#' `r lifecycle::badge("deprecated")`
#'
#' It only checks that cassette names are not duplicated. Note that if you do
#' need to have duplicated cassette names you can do so by using the
#' `allowed_duplicates` parameter in `check_cassette_names()`.
#' This function has been deprecated because it's not possible for it to
#' detect re-used cassette names 100% correctly and the problem of duplicated
#' cassette names relatively easy to debug by hand.
#'
#' @export
#' @param pattern (character) regex pattern for file paths to check.
Expand All @@ -23,9 +21,11 @@ check_cassette_names <- function(
behavior = "stop",
allowed_duplicates = NULL
) {
lifecycle::deprecate_warn("2.0.0", "check_cassette_names()")

assert(allowed_duplicates, "character")
files <- list.files(".", pattern = pattern, full.names = TRUE)
if (length(files) == 0) return()
if (length(files) == 0) return(invisible())
cassette_names <- function(x) {
tmp <- parse(x, keep.source = TRUE)
df <- utils::getParseData(tmp)
Expand Down Expand Up @@ -59,4 +59,5 @@ check_cassette_names <- function(
warning = warning(mssg, call. = FALSE, immediate. = TRUE)
)
}
invisible()
}
2 changes: 0 additions & 2 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ reference:
- cassette_path
- cassettes
- current_cassette
- check_cassette_names
- title: Other helpers
contents:
- vcr_last_error
- skip_if_vcr_off
- vcr_test_path
- turn_off
- use_vcr

articles:
- title: All vignettes
Expand Down
10 changes: 4 additions & 6 deletions man/check_cassette_names.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/testthat/_snaps/check_cassette_names.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# check_cassette_names is deprecated

Code
check_cassette_names(dir)
Condition
Warning:
`check_cassette_names()` was deprecated in vcr 2.0.0.

# check_cassette_names

Code
check_cassette_names()
Condition
Error:
! you should not have duplicated cassette names:
testing (found in test-catbar.R, test-single.R)

1 change: 1 addition & 0 deletions tests/testthat/helper-vcr.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ make_pkg <- function(frame = parent.frame()) {

dir_create(file.path(dir, "man"))
dir_create(file.path(dir, "R"))
dir_create(file.path(dir, "tests", "testthat"))
cat(sprintf(desc_text, basename(dir)), file = file.path(dir, "DESCRIPTION"))

dir
Expand Down
47 changes: 27 additions & 20 deletions tests/testthat/test-check_cassette_names.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
test_that("check_cassette_names", {
withr::local_options(lifecycle_verbosity = "quiet")

test_that("check_cassette_names is deprecated", {
dir <- make_pkg()
expect_snapshot(check_cassette_names(dir))
})

res <- use_vcr(dir, verbose = FALSE)
strg_dup <- 'test_that("bar", {\n vcr::use_cassette("testing", cat("bar"))\n vcr::use_cassette("testing", cat("bar"))\n})'
cat(strg_dup, file = file.path(dir, "tests/testthat/test-catbar.R"))
strg_single <- 'test_that("bar", {\n expect_true(TRUE)\n vcr::use_cassette("testing", cat("bar"))\n})'
cat(strg_single, file = file.path(dir, "tests/testthat/test-single.R"))
strg_not_used <- 'hi'
cat(strg_not_used, file = file.path(dir, "tests/testthat/cat.R"))
test_that("check_cassette_names", {
withr::local_options(lifecycle_verbosity = "quiet")

withr::local_dir(file.path(dir, "tests/testthat"))
expect_error(
check_cassette_names(),
"you should not have duplicated cassette names",
class = "error"
test_path <- file.path(make_pkg(), "tests/testthat")
writeLines(
c(
'test_that("bar", {',
' vcr::use_cassette("testing", cat("bar"))',
' vcr::use_cassette("testing", cat("bar"))',
'})'
),
file.path(test_path, "test-catbar.R")
)
mssg <- tryCatch(check_cassette_names(), error = function(e) e)
expect_match(mssg$message, "you should not have duplicated cassette names")
expect_match(mssg$message, "testing \\(found in ")
expect_match(mssg$message, "test-catbar.R, test-single.R, test-vcr_example.R")
writeLines(
c(
'test_that("bar", {',
' vcr::use_cassette("testing", cat("bar"))',
'})'
),
file.path(test_path, "test-single.R")
)
writeLines("hi", con = file.path(test_path, "cat.R"))

withr::local_dir(test_path)
expect_snapshot(check_cassette_names(), error = TRUE)

# can allow duplicated names via the allowed_duplicates param
expect_error(check_cassette_names(allowed_duplicates = "testing"), NA)
expect_no_error(check_cassette_names(allowed_duplicates = "testing"))
})
4 changes: 4 additions & 0 deletions vignettes/debugging.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,7 @@ Make sure you ejected the cassette you were using!

Unless your vcr helper/setup file tweaked more things than you would like,
you do not even need to re-start R, but you could, just to be on the safe side.

### Re-used cassestte name
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly a placeholder so I don't forget about it in the debugging vignette.


You need to make sure that every test has it's own unique cassette name, otherwise you won't find the responses that you're expecting. This is generally a fairly easy problem to diagnose if you're aware of it: if you re-record the cassette to fix the test you're currently in, a different test will break.