Skip to content

Commit

Permalink
vcr_config() is no longer needed
Browse files Browse the repository at this point in the history
Resolved by ropensci/vcr#153
  • Loading branch information
aaronwolen committed Jan 3, 2021
1 parent bca12f1 commit 3e98cb7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 58 deletions.
40 changes: 15 additions & 25 deletions tests/testthat/helper-vcr.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
library(vcr)

# set to 'once' when recording new requests
record <- "once"
# make path relative to test directory
cassette_path <- function(x) {
file.path(rprojroot::find_testthat_root_file(), "cassettes", x)
}

# redefine same global settings until the following issue is resolved
# https://github.com/ropensci/vcr/issues/136
vcr_config <- function(dir, record = "new_episodes", write_disk_path = NULL, ...) {
rootdir <- rprojroot::find_testthat_root_file()

if (!is.null(write_disk_path)) {
write_disk_path <- file.path(rootdir, write_disk_path)
}

invisible(
vcr::vcr_configure(
dir = file.path(rootdir, dir),
record = record,
match_requests_on = c("method", "uri"),
write_disk_path = write_disk_path,
filter_sensitive_data = list("<totallyrealpat>" = Sys.getenv("OSF_PAT")),
log = nzchar(Sys.getenv("VCR_LOG")),
log_opts = list(
file = Sys.getenv("VCR_LOG"),
log_prefix = "Cassette",
date = TRUE
)
vcr::vcr_configure(
# set to 'once' when recording new requests
record = "once",
match_requests_on = c("method", "uri", "body"),
filter_sensitive_data = list("<totallyrealpat>" = Sys.getenv("OSF_PAT")),
log = nzchar(Sys.getenv("VCR_LOG")),
log_opts = list(
file = Sys.getenv("VCR_LOG"),
log_prefix = "Cassette",
date = TRUE
)
)}
)
28 changes: 15 additions & 13 deletions tests/testthat/test-directories.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
context("Directories")

# setup -------------------------------------------------------------------
vcr_config("cassettes/directories")
vcr::vcr_configure(
dir = cassette_path("directories"),
)

setup({
if (has_pat()) {
vcr::use_cassette("create-p1", record = record, {
vcr::use_cassette("create-p1", {
p1 <<- osf_create_project(title = "osfr-test-directories")
})
}
})

teardown({
if (has_pat()) {
vcr::use_cassette("delete-p1", record = record, {
vcr::use_cassette("delete-p1", {
osf_rm(p1, recurse = TRUE, check = FALSE)
})
}
Expand All @@ -23,7 +25,7 @@ teardown({
# tests -------------------------------------------------------------------
test_that("empty project/folder returns an empty osf_tbl_file", {
skip_if_no_pat()
vcr::use_cassette("list-empty-p1", record = record, {
vcr::use_cassette("list-empty-p1", {
out <- osf_ls_files(p1)
})

Expand All @@ -34,7 +36,7 @@ test_that("empty project/folder returns an empty osf_tbl_file", {
test_that("listing a non-existent folder errors", {
skip_if_no_pat()
expect_error(
vcr::use_cassette("list-nonexistent-dir", record = record, {
vcr::use_cassette("list-nonexistent-dir", {
osf_ls_files(p1, path = "nonexistent")
}),
"Can't find directory"
Expand All @@ -43,7 +45,7 @@ test_that("listing a non-existent folder errors", {

test_that("create a top-level directory", {
skip_if_no_pat()
vcr::use_cassette("create-dir1", record = record, {
vcr::use_cassette("create-dir1", {
d1 <- osf_mkdir(p1, path = "dir1")
})

Expand All @@ -53,7 +55,7 @@ test_that("create a top-level directory", {

test_that("list a top-level directory", {
skip_if_no_pat()
vcr::use_cassette("list-p1-with-one-dir", record = record, {
vcr::use_cassette("list-p1-with-one-dir", {
out <- osf_ls_files(p1)
})

Expand All @@ -65,7 +67,7 @@ test_that("list a top-level directory", {

test_that("create a subdirectory within an existing directory", {
skip_if_no_pat()
vcr::use_cassette("create-subdir-dir11", record = record, {
vcr::use_cassette("create-subdir-dir11", {
d11 <- osf_mkdir(p1, path = "dir1/dir11")
})

Expand All @@ -75,7 +77,7 @@ test_that("create a subdirectory within an existing directory", {

test_that("list within a subdirectory", {
skip_if_no_pat()
vcr::use_cassette("list-within-subdir-dir1", record = record, {
vcr::use_cassette("list-within-subdir-dir1", {
out <- osf_ls_files(p1, path = "dir1")
})

Expand All @@ -85,7 +87,7 @@ test_that("list within a subdirectory", {

test_that("create a subdirectory within a non-existent parent directory", {
skip_if_no_pat()
vcr::use_cassette("create-subdir-dir21", record = record, {
vcr::use_cassette("create-subdir-dir21", {
d21 <- osf_mkdir(p1, path = "dir2/dir21")
})

Expand All @@ -98,19 +100,19 @@ test_that("create a subdirectory within a non-existent parent directory", {

test_that("'path' isn't confused by dir names with a shared substring (#95)", {
skip_if_no_pat()
vcr::use_cassette("create-dir", record = record, {
vcr::use_cassette("create-dir", {
d3 <- osf_mkdir(p1, path = "dir")
})

vcr::use_cassette("list-dir", record = record, {
vcr::use_cassette("list-dir", {
out <- osf_ls_files(p1, path = "dir")
})
expect_equal(nrow(out), 0)
})

test_that("directory names can start with a dot", {
skip_if_no_pat()
vcr::use_cassette("create-dir-with-dot-prefix", record = record, {
vcr::use_cassette("create-dir-with-dot-prefix", {
dotdir <- osf_mkdir(p1, path = ".dir")
})

Expand Down
13 changes: 8 additions & 5 deletions tests/testthat/test-downloading.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ context("Downloading files")


# setup -------------------------------------------------------------------
vcr_config("cassettes/downloading", write_disk_path = "cassettes/downloading/files")
vcr::vcr_configure(
dir = cassette_path("downloading"),
write_disk_path = cassette_path("downloading/files")
)

setup({
# Retrieve public OSF project and components required for tests
# (created using data-raw/create-test-project.R)
if (on_test_server()) {
vcr::use_cassette("load-guids", record = record, {
vcr::use_cassette("load-guids", {
guids <- get_guids()
p1 <<- osf_retrieve_node(guids[, "p1"])
c1 <<- osf_retrieve_node(guids[, "c1"])
Expand All @@ -30,7 +33,7 @@ teardown({

test_that("a file can be downloaded from a project", {
skip_if_no_pat()
vcr::use_cassette("f1-from-project", record = record, {
vcr::use_cassette("f1-from-project", {
out <- osf_download(f1, path = outdir)
})

Expand All @@ -44,7 +47,7 @@ test_that("a file can be downloaded from a project", {
test_that("by default an error is thrown if a conflicting file exists", {
skip_if_no_pat()

vcr::insert_cassette("f1-from-project-conflict", record = record)
vcr::insert_cassette("f1-from-project-conflict")
expect_error(
out <- osf_download(f1, path = outdir),
paste0("Can't download file '", f1$name, "' from OSF.")
Expand All @@ -54,7 +57,7 @@ test_that("by default an error is thrown if a conflicting file exists", {

test_that("a file can be overwritten when conflicts='overwrite'", {
skip_if_no_pat()
vcr::insert_cassette("f1-from-project-overwrite", record = record)
vcr::insert_cassette("f1-from-project-overwrite")
expect_silent(
out <- osf_download(f1, path = outdir, conflict = "overwrite")
)
Expand Down
32 changes: 17 additions & 15 deletions tests/testthat/test-uploading-multiple-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ context("Uploading multiple files")


# setup -------------------------------------------------------------------
vcr_config("cassettes/uploading-multiple-files")
vcr::vcr_configure(
dir = cassette_path("uploading-multiple-files")
)

setup({
# copy directory for testing multifile uploads to pwd
Expand All @@ -12,7 +14,7 @@ setup({
)

if (has_pat()) {
vcr::use_cassette("create-p1", record = record, {
vcr::use_cassette("create-p1", {
p1 <<- osf_create_project(title = "osfr-multifile-upload-test")
})
}
Expand All @@ -21,7 +23,7 @@ setup({
teardown({
fs::dir_delete(multidir)
if (has_pat()) {
vcr::use_cassette("delete-p1", record = record, {
vcr::use_cassette("delete-p1", {
osf_rm(p1, recurse = TRUE, check = FALSE)
})
}
Expand All @@ -35,7 +37,7 @@ test_that("multiple files can be uploaded", {

infiles <- fs::dir_ls(multidir, type = "file")

vcr::use_cassette("upload-infiles", record = record, {
vcr::use_cassette("upload-infiles", {
out <- osf_upload(p1, infiles)
})

Expand All @@ -46,7 +48,7 @@ test_that("multiple files can be uploaded", {
test_that("a directory can be uploaded", {
skip_if_no_pat()

vcr::use_cassette("create-and-populate-c1", record = record, {
vcr::use_cassette("create-and-populate-c1", {
c1 <- osf_create_component(p1, "dir-upload")
out <- osf_upload(c1, multidir)
})
Expand All @@ -67,7 +69,7 @@ test_that("a subdirectory can be uploaded", {

# upload the subdirectory with no children
indir <- file.path(multidir, "subdir1", "subdir1_1")
vcr::use_cassette("create-and-populate-c2", record = record, {
vcr::use_cassette("create-and-populate-c2", {
c2 <- osf_create_component(p1, "subdir-upload")
out <- osf_upload(c2, indir, verbose = TRUE)
})
Expand Down Expand Up @@ -98,7 +100,7 @@ test_that("a subdirectory can be uploaded", {
test_that("recurse argument respects specified levels", {
skip_if_no_pat()

vcr::use_cassette("create-and-populate-c3", record = record, {
vcr::use_cassette("create-and-populate-c3", {
c3 <- osf_create_component(p1, "recurse=1")
out <- osf_upload(c3, path = multidir, recurse = 1)
})
Expand Down Expand Up @@ -129,7 +131,7 @@ test_that("recurse argument respects specified levels", {
test_that("recurse=TRUE uploads the entire OSF directory structure", {
skip_if_no_pat()

vcr::use_cassette("create-and-populate-c4", record = record, {
vcr::use_cassette("create-and-populate-c4", {
c4 <- osf_create_component(p1, "recurse=TRUE")
out <- osf_upload(c4, path = multidir, recurse = TRUE)
})
Expand All @@ -150,17 +152,17 @@ test_that("files in parent directories can be uploaded", {
setwd(fs::path(multidir, "subdir1/subdir1_1"))
skip_if_no_pat()

vcr::use_cassette("create-c5", record = record, {
vcr::use_cassette("create-c5", {
c5 <- osf_create_component(p1, "parent-directories")
})

vcr::use_cassette("c5-upload-f1", record = record, {
vcr::use_cassette("c5-upload-f1", {
upf1 <- osf_upload(c5, path = "../d.txt")
})
vcr::use_cassette("c5-upload-f2", record = record, {
vcr::use_cassette("c5-upload-f2", {
upf2 <- osf_upload(c5, path = "../../a.txt")
})
vcr::use_cassette("c5-upload-updir", record = record, {
vcr::use_cassette("c5-upload-updir", {
updir <- osf_upload(c5, path = "../../subdir2")
})

Expand All @@ -177,14 +179,14 @@ test_that("conflicting files are skipped or overwritten", {
skip_if_no_pat()

infiles <- fs::dir_ls(multidir, type = "file")
vcr::use_cassette("create-and-populate-c6", record = record, {
vcr::use_cassette("create-and-populate-c6", {
c6 <- osf_create_component(p1, "conflicts")
out1 <- osf_upload(c6, path = infiles[1])
})

# overwrite contents of first file
writeLines("foo", infiles[1])
vcr::use_cassette("c6-conflict-skip", record = record, {
vcr::use_cassette("c6-conflict-skip", {
out2 <- osf_upload(c6, path = infiles[1], conflicts = "skip")
})

Expand All @@ -193,7 +195,7 @@ test_that("conflicting files are skipped or overwritten", {
expect_true(all(versions) == 1)

# overwriting with new file increments version
vcr::use_cassette("c6-conflict-overwrite", record = record, {
vcr::use_cassette("c6-conflict-overwrite", {
out3 <- osf_upload(c6, path = infiles[1], conflicts = "overwrite")
})
expect_equal(get_meta(out3, "attributes", "current_version"), 2)
Expand Down

0 comments on commit 3e98cb7

Please sign in to comment.