ropensci / vcr

Record HTTP calls and replay them
https://docs.ropensci.org/vcr
Other
77 stars 12 forks source link

serializers: put in safeguards for changing/different serializers vs. whats on disk #188

Closed sckott closed 3 years ago

sckott commented 3 years ago

With new serializers, we need to make sure the one declared is the same as the cassettes being used (if any are present)

scenarios:

  1. cassettes on disk - serializer chosen same as what's used in associated cassette(s)
  2. cassettes on disk - serializer chosen different from what's used in associated cassette(s)
  3. NO cassettes on disk - doesn't matter what serializer used

notes:

sckott commented 3 years ago

Interesting. So Ruby vcr just treats a different serializer option with the same cassette name in the same directory to be a different thing, so creates a new cassette with the new extension.

So no code changes but just document this

code i was playing around with for archiving sake

valid_json <- function(x) {
  !inherits(try(jsonlite::fromJSON(x), silent = TRUE), "try-error")
}
valid_yaml <- function(x) {
  readable <- !inherits(try(suppressWarnings(yaml::yaml.load_file(x)),
    silent = TRUE), "try-error")
  lns <- suppressWarnings(readLines(x))
  seems_like_json <- lns[nzchar(lns)][1] == "{"
  readable && !seems_like_json
}
serializer_type <- function(x) {
  vj <- valid_json(x)
  vy <- valid_yaml(x)
  if ((vj && vy) || !vj && !vy) stop("could not detect serializer used")
  if (vj) return("json")
  if (vy) return("yaml")
}
file_empty <- function(x) !nzchar(readLines(x))
check_serializers_match <- function(serializer, file) {
  if (file.exists(file) && file_empty(file)) return()
  type <- serializer_type(file)
  if (type != serializer) {
    stop(
      sprintf("'serialize_with' option ('%s') does not", serializer),
      sprintf("match serializer of the cassette on disk ('%s')", type),
      call. = FALSE
    )
  }
}
# file_yml <- "/var/folders/fc/n7g_vrvn0sx_st0p8lxb3ts40000gn/T//RtmpgWGsHD/asdfasdfsd/testing1.yml"
# file_json <- "/var/folders/fc/n7g_vrvn0sx_st0p8lxb3ts40000gn/T//RtmpgWGsHD/asdfasdfsd/testing1.json"
# yy <- yaml::yaml.load_file(file_yml)
# cat(jsonlite::toJSON(yy, auto_unbox = TRUE, pretty = TRUE), 
#   file = "/var/folders/fc/n7g_vrvn0sx_st0p8lxb3ts40000gn/T//RtmpgWGsHD/asdfasdfsd/testing1.json")