From 376768c20f6fccde42c3541d84840535885ee428 Mon Sep 17 00:00:00 2001 From: andrie Date: Wed, 27 Mar 2024 09:43:18 +0000 Subject: [PATCH] Refactor tests --- tests/testthat/test-3-makeRepo.R | 79 +++++++++---------- .../testthat/test-4-makeRepo-from-localCRAN.R | 12 +-- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/tests/testthat/test-3-makeRepo.R b/tests/testthat/test-3-makeRepo.R index 7c27ec8..5a82fbc 100644 --- a/tests/testthat/test-3-makeRepo.R +++ b/tests/testthat/test-3-makeRepo.R @@ -21,53 +21,52 @@ types <- set_test_types() pkg_type <- types[1] -test_for_type <- function(pkg_type) { - # skip_if_not_installed("mockr") - test_that(sprintf("makeRepo downloads %s files and builds PACKAGES", - pkg_type), { - - skip_on_cran() - skip_if_offline() - skip_if_not_installed("mockr") - +test_that("makeRepo downloads files and builds PACKAGES", { + + skip_on_cran() + skip_if_offline() + skip_if_not_installed("mockr") + types <- set_test_types() + for (pkg_type in types) { + # pdb <- pkgAvail(repos = revolution, type = pkg_type, quiet = TRUE) pdb <- pkgAvail(repos = revolution, type = pkg_type, Rversion = rvers, quiet = TRUE) pkgList <- pkgDep(pkgs, availPkgs = pdb, repos = revolution, type = pkg_type, - suggests = FALSE, Rversion = rvers, quiet = FALSE) - prefix <- repoPrefix(pkg_type, Rversion = rvers) - dir.create(repo_root, recursive = TRUE, showWarnings = FALSE) - - ret <- mockr::with_mock( - download_packages = mock_download_packages, - write_packages = mock_write_packages, - .env = "miniCRAN", - { - makeRepo(pkgList, path = repo_root, repos = revolution, - type = pkg_type, quiet = TRUE, Rversion = rvers) - } + suggests = FALSE, Rversion = rvers, quiet = FALSE) + prefix <- repoPrefix(pkg_type, Rversion = rvers) + dir.create(repo_root, recursive = TRUE, showWarnings = FALSE) + + ret <- mockr::with_mock( + download_packages = mock_download_packages, + write_packages = mock_write_packages, + .env = "miniCRAN", + { + makeRepo(pkgList, path = repo_root, repos = revolution, + type = pkg_type, quiet = TRUE, Rversion = rvers) + } + ) + + expect_type(ret, "character") + expect_equal(length(ret), length(pkgList)) + + expect_true( + .checkForRepoFiles(repo_root, pkgList, prefix) + ) + expect_true( + file.exists(file.path(repo_root, prefix, "PACKAGES.gz")) + ) + expect_true( + all( + pkgList %in% pkgAvail(repos = repo_root, + type = pkg_type, + Rversion = rvers)[, "Package"] + ) ) + } +}) - expect_type(ret, "character") - expect_equal(length(ret), length(pkgList)) - expect_true( - .checkForRepoFiles(repo_root, pkgList, prefix) - ) - expect_true( - file.exists(file.path(repo_root, prefix, "PACKAGES.gz")) - ) - expect_true( - all( - pkgList %in% pkgAvail(repos = repo_root, - type = pkg_type, - Rversion = rvers)[, "Package"] - ) - ) - }) -} # types are defined in env var `minicran_test_scope` -types <- set_test_types() -for (pkg_type in types) {test_for_type(pkg_type = pkg_type)} unlink(repo_root, recursive = TRUE) diff --git a/tests/testthat/test-4-makeRepo-from-localCRAN.R b/tests/testthat/test-4-makeRepo-from-localCRAN.R index bc9511e..104ece7 100644 --- a/tests/testthat/test-4-makeRepo-from-localCRAN.R +++ b/tests/testthat/test-4-makeRepo-from-localCRAN.R @@ -22,10 +22,10 @@ names(types) <- c("win.binary") pkg_type <- names(types) -for (pkg_type in names(types)) { - test_that(sprintf("makeRepo downloads %s files and builds PACKAGES file", pkg_type), { - # skip_on_cran() - skip_if_offline(revolution_mran) +test_that("makeRepo downloads files and builds PACKAGES file", { + skip_if_offline(revolution_mran) + # skip_on_cran() + for (pkg_type in names(types)) { # Create local miniCRAN # mockery::stub(makeRepo, "download_packages", mock_download_packages, depth = 1) @@ -83,7 +83,7 @@ for (pkg_type in names(types)) { ) ) - }) -} + } +}) unlink(repo_root, recursive = TRUE)