Skip to content

Commit

Permalink
Restructure tests to handle different environments
Browse files Browse the repository at this point in the history
Tests should now run when:
* OSF_SERVER is not "test"
* a PAT is not registered
  • Loading branch information
aaronwolen committed Jan 24, 2019
1 parent 704696d commit 81cfe71
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 70 deletions.
19 changes: 19 additions & 0 deletions tests/testthat/helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# for tests that must be executed on test.osf.io
on_test_server <- function() {
Sys.getenv("OSF_SERVER") == "test"
}

skip_on_production_server <- function() {
testthat::skip_if_not(
on_test_server(),
"OSF_SERVER not set to 'test'."
)
}


# for tests that require authenticated access
has_pat <- function() nzchar(Sys.getenv("OSF_PAT"))

skip_if_no_pat <- function() {
testthat::skip_if_not(has_pat(), "No PAT detected.")
}
33 changes: 26 additions & 7 deletions tests/testthat/test-authentication.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
context("Authentication")


# setup -------------------------------------------------------------------

# Record developer PAT for OSF test server and unset variable for tests
test_pat <- Sys.getenv("OSF_PAT")
Sys.unsetenv("OSF_PAT")
setup({
if (has_pat()) {
test_pat <<- Sys.getenv("OSF_PAT")
Sys.unsetenv("OSF_PAT")
}
})

# Restore variables
teardown({
Sys.unsetenv("OSF_PAT")
options(osfr.pat = NULL)

if (exists("test_pat")) {
Sys.setenv(OSF_PAT = test_pat)
osf_auth()
}
})


# tests -------------------------------------------------------------------
test_that("osf_auth() warns no PAT is found", {
expect_warning(osf_auth(), "No PAT found")
expect_null(getOption("osfr.pat"))
Expand All @@ -13,21 +33,20 @@ test_that("osf_auth() defines osfr.pat from token arg", {
options(osfr.pat = NULL)

expect_message(
osf_auth(test_pat),
osf_auth("fake_token"),
"Registered PAT from the provided token"
)
expect_equal(test_pat, getOption("osfr.pat"))
expect_equal("fake_token", getOption("osfr.pat"))
})

# Restore variables
Sys.setenv(OSF_PAT = test_pat)

test_that("osf_auth() defines osfr.pat from OSF_PAT", {
Sys.setenv(OSF_PAT = "fake_token")
options(osfr.pat = NULL)

expect_message(
osf_auth(),
"Registered PAT from the OSF_PAT environment variable"
)
expect_equal(test_pat, getOption("osfr.pat"))
expect_equal("fake_token", getOption("osfr.pat"))
})
30 changes: 24 additions & 6 deletions tests/testthat/test-directories.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,44 @@ context("Directories")


# setup -------------------------------------------------------------------
p1 <- osf_create_project(title = "osfr-component-tests")
setup({
if (has_pat()) {
p1 <<- osf_create_project(title = "osfr-test-directories")
}
})

teardown({
if (has_pat()) {
osf_rm(p1, recursive = TRUE, check = FALSE)
}
})

# tests -------------------------------------------------------------------

# tests -------------------------------------------------------------------
test_that("empty project/folder returns a osf_tbl_file with 0 rows", {
skip_if_no_pat()

out <- osf_ls_files(p1)
expect_s3_class(out, "osf_tbl_file")
expect_equal(nrow(out), 0)
})

test_that("listing a non-existent folder errors", {
skip_if_no_pat()
expect_error(osf_ls_files(p1, path = "data"), "Can't find directory")
})

test_that("create a top-level directory", {
skip_if_no_pat()

d1 <- osf_mkdir(p1, path = "dir1")
expect_s3_class(d1, "osf_tbl_file")
expect_equal(d1$name, "dir1")
})

test_that("list a top-level directory", {
skip_if_no_pat()

out <- osf_ls_files(p1)
expect_s3_class(out, "osf_tbl_file")
expect_equal(nrow(out), 1)
Expand All @@ -32,12 +48,16 @@ test_that("list a top-level directory", {


test_that("create a subdirectory within an existing directory", {
skip_if_no_pat()

d11 <- osf_mkdir(p1, path = "dir1/dir11")
expect_s3_class(d11, "osf_tbl_file")
expect_equal(d11$name, "dir11")
})

test_that("list a subdirectory", {
skip_if_no_pat()

out <- osf_ls_files(p1)
expect_equal(nrow(out), 1)
expect_equal(out$name, "dir1")
Expand All @@ -48,14 +68,12 @@ test_that("list a subdirectory", {
})

test_that("create a subdirectory within a non-existent parent directory", {
skip_if_no_pat()

d21 <- osf_mkdir(p1, path = "dir2/dir21")
expect_s3_class(d21, "osf_tbl_file")
expect_equal(d21$name, "dir21")

d2_attrs <- d21$meta[[1]]$attributes
expect_equal(d2_attrs$materialized_path, "/dir2/dir21/")
})


# cleanup -----------------------------------------------------------------
osf_rm(p1, recursive = TRUE, check = FALSE)
86 changes: 62 additions & 24 deletions tests/testthat/test-files.R
Original file line number Diff line number Diff line change
@@ -1,83 +1,122 @@
context("Uploading")


# setup -------------------------------------------------------------------
txt.file <- file.path(tempdir(), "osfr-test-file.txt")
writeLines("Lorem ipsum dolor sit amet, consectetur", txt.file)
infile <- tempfile("osfr-local-file-", fileext = ".txt")
outfile <- basename(infile)

setup({
writeLines("Lorem ipsum dolor sit amet, consectetur", infile)
if (has_pat()) {
p1 <<- osf_create_project(title = "osfr-test-files")
}
})

p1 <- osf_create_project("File Tests")
teardown({
unlink(outfile)

if (has_pat()) {
osf_rm(p1, recursive = TRUE, check = FALSE)
}
})


# tests -------------------------------------------------------------------
test_that("non-existent file is detected", {
expect_error(osf_upload(p1, "non-existent-file"), "Can't find file")
})

f1 <- osf_upload(p1, txt.file)

test_that("file is uploaded to project root", {
skip_if_no_pat()

f1 <<- osf_upload(p1, infile)
expect_s3_class(f1, "osf_tbl_file")
expect_match(f1$name, basename(txt.file))
expect_match(f1$name, outfile)
})

test_that("uploaded file can be retrieved", {
skip_if_no_pat()

f2 <- osf_retrieve_file(as_id(f1))
expect_identical(f1, f2)
})

test_that("upload fails if the file already exists", {
expect_error(osf_upload(p1, txt.file), "File already exists at destination")
skip_if_no_pat()
expect_error(osf_upload(p1, infile), "File already exists at destination")
})

writeLines("Lorem ipsum dolor sit amet, consectetur, ea duo posse", txt.file)

test_that("upload can overwrite existing files", {
f1 <- osf_upload(p1, txt.file, overwrite = TRUE)
skip_if_no_pat()

writeLines("Lorem ipsum dolor sit amet, consectetur, ea duo posse", infile)
skip_if_no_pat()

f1 <- osf_upload(p1, infile, overwrite = TRUE)
expect_equal(f1$meta[[1]]$attributes$current_version, 2)
expect_s3_class(f1, "osf_tbl_file")
})

test_that("user is warned that path info is removed from upload name", {
skip_if_no_pat()

expect_warning(
osf_upload(p1, txt.file, name = "path/file.txt"),
osf_upload(p1, infile, name = "path/file.txt"),
"Removing path information"
)
})

d1 <- osf_mkdir(p1, "d1")
f2 <- osf_upload(d1, txt.file)

test_that("file can be uploaded to a directory", {
skip_if_no_pat()

d1 <<- osf_mkdir(p1, "data")
f2 <<- osf_upload(d1, infile)
expect_s3_class(f2, "osf_tbl_file")
})

test_that("attempting to list an osf_tbl_file with a file errors", {
skip_if_no_pat()
expect_error(osf_ls_files(f1), "Listing an `osf_tbl_file` requires a dir")
})


context("Downloading")
test_that("a file can be downloaded from a project", {

outfile <- tempfile(fileext = ".txt")
skip_if_no_pat()

test_that("a file can be downloaded from a project", {
out <- osf_download(f1, path = outfile)
expect_s3_class(out, "osf_tbl_file")
expect_identical(out$local_path, outfile)
expect_true(file.exists(outfile))
})

test_that("an existing file won't be overwritten", {
expect_error(osf_download(f1, path = outfile), "A file exists at the specified")
expect_s3_class(osf_download(f1, path = outfile, overwrite = TRUE), "osf_tbl_file")
skip_if_no_pat()

expect_error(
osf_download(f1, path = outfile),
"A file exists at the specified"
)
expect_s3_class(
osf_download(f1, path = outfile, overwrite = TRUE),
"osf_tbl_file"
)
})

test_that("a non-existant path throws an error", {
skip_if_no_pat()

expect_error(
osf_download(f1, path = "ddd/test.txt"),
"The directory specified in `path` does not exist.")
})

test_that("a file can be downloaded from a directory", {
skip_if_no_pat()

outfile <- tempfile(fileext = ".txt")
out <- osf_download(f2, path = outfile)
expect_s3_class(out, "osf_tbl_file")
Expand All @@ -86,14 +125,18 @@ test_that("a file can be downloaded from a directory", {
})

test_that("a directory can be downloaded as a zip file", {
skip_if_no_pat()

d1_files <- osf_ls_files(d1, n_max = Inf)
outfile <- tempfile(fileext = ".zip")

out <- osf_download(d1, path = outfile)
expect_s3_class(out, "osf_tbl_file")
expect_true(file.exists(outfile))

expect_match(
unzip(outfile, list = TRUE)$Name[1],
basename(txt.file)
expect_equal(
sort(unzip(outfile, list = TRUE)$Name),
sort(d1_files$name)
)
})

Expand Down Expand Up @@ -168,8 +211,3 @@ test_that("a non-empty directory can be deleted", {
d3 <- osf_mkdir(p1, "d1/d2/d3")
expect_true(osf_rm(d3, check = FALSE))
})


# cleanup -----------------------------------------------------------------
osf_rm(p1, check = FALSE)
osf_rm(p2, check = FALSE)
1 change: 1 addition & 0 deletions tests/testthat/test-identifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ test_that("GUIDs and Waterbutler IDs are detected in OSF URLs", {
})

test_that("special identifier 'me' is recognized", {
skip_if_no_pat()
expect_match(id_type("me"), "users")
})
Loading

0 comments on commit 81cfe71

Please sign in to comment.