From d91343ea7e9abee047716ea942d03aa286d4463c Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Sun, 3 May 2020 10:14:55 -0500 Subject: [PATCH] Start building out some testing infrastructure --- R/pkg.R | 21 +++++++++++++++++++++ tests/testthat/test-pkg.R | 17 +++++++++++++++++ tests/testthat/test-todo.R | 13 +++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 tests/testthat/test-pkg.R create mode 100644 tests/testthat/test-todo.R diff --git a/R/pkg.R b/R/pkg.R index 1aad75a..1c2b9a9 100644 --- a/R/pkg.R +++ b/R/pkg.R @@ -25,3 +25,24 @@ pkg_version <- function(pkgdir) { pkg_bug_reports <- function(pkgdir) { read.dcf(file.path(pkgdir, "DESCRIPTION"))[, "BugReports"][[1]] } + +# Helper for testing ------------------------------------------------------ + +local_package <- function(fields = list(), env = parent.frame()) { + dir <- tempfile() + dir.create(dir) + dir.create(file.path(dir, "revdep")) + withr::defer(unlink(dir, recursive = TRUE), envir = env) + + defaults <- list( + Package = "Test", + Version = "1.0.0" + ) + + fields <- utils::modifyList(defaults, fields) + write.dcf(fields, file.path(dir, "DESCRIPTION")) + + db_setup(dir) + + dir +} diff --git a/tests/testthat/test-pkg.R b/tests/testthat/test-pkg.R new file mode 100644 index 0000000..9a6f703 --- /dev/null +++ b/tests/testthat/test-pkg.R @@ -0,0 +1,17 @@ +test_that("can retrieve basic package metadata", { + pkg <- local_package(list( + Package = "test", + Version = "0.0.1", + BugReports = "BUGS" + )) + + expect_equal(pkg_name(pkg), "test") + expect_equal(pkg_version(pkg), "0.0.1") + expect_equal(pkg_bug_reports(pkg), "BUGS") +}) + +test_that("pkg_check validates its inputs", { + expect_error(pkg_check(1), "string") + expect_error(pkg_check("NOT-FOUND"), "existing directory") + expect_error(pkg_check("."), "DESCRIPTION") +}) diff --git a/tests/testthat/test-todo.R b/tests/testthat/test-todo.R new file mode 100644 index 0000000..451f297 --- /dev/null +++ b/tests/testthat/test-todo.R @@ -0,0 +1,13 @@ +test_that("can add and remove from db", { + pkg <- local_package() + + todo <- revdep_todo(pkg) + expect_equal(nrow(todo), 0) + + todo <- revdep_add(pkg, c("x", "y")) + expect_equal(todo$package, c("x", "y")) + + todo <- revdep_rm(pkg, "x") + expect_equal(todo$package, c("x", "y")) + expect_equal(todo$status, c("ignore", "todo")) +})