tests/testthat/test-validation_env.R

context("validation environment table")

# Note: when running tests via a CRAN-like environment the testing
#       folder structure is valtools.Rcheck/tests w/o DESCRIPTION,
#       which causes package_file to fail.

test_that("works on valtools (no Depends)", {


  skip_if(grepl(dirname(rprojroot::is_testthat$find_file()), pattern = "\\.Rcheck"))

  testthat::expect_false(all(grepl(readLines(rprojroot::is_r_package$find_file("DESCRIPTION")), pattern = "Depends")))

  validation_env <- vt_scrape_val_env()
  testthat::expect_true(!all(is.na(validation_env)))


})


test_that("temp", {

  withr::with_tempdir({

  captured_output <- capture.output({usethis::create_package(path = ".", open = FALSE,
                                            rstudio = TRUE)})

  # bare
  validation_env0 <- vt_scrape_val_env()

  testthat::expect_true(all(c("OS", "R")%in%
                         validation_env0[validation_env0$type != "session", "resource"]))
  kable_output_env0 <- vt_kable_val_env(validation_env0, format = "latex")
  expected_kable_output_env0 <- c(
    "\\begin{tabular}{|>{}l|>{}l||l}",
    "\\hline",
    "Type & Resource & Version Detail\\\\",
    "\\hline",
    paste0("& OS & ", sessionInfo()[["running"]], "\\\\"),
    "\\cline{2-3}",
    paste0("\\multirow{-2}{*}{\\raggedright\\arraybackslash system} & R & ",
      gsub(R.version.string, pattern = "R\\sversion\\s(.*)\\s.*", replacement = "\\1"), "\\\\"),
    "\\cline{1-3}",
    paste0("& desc & ", packageVersion("testthat"), "\\\\"),
    "\\cline{2-3}",
    paste0("& here & ", packageVersion("valtools"), "\\\\"),
    "\\cline{2-3}",
    "\\hline",
    "\\end{tabular}"
  )

  # Only Suggests
  fields <- usethis::use_description_defaults()
  suggests_field <- "covr, testthat, here"
  fields$Suggests <- suggests_field
  desc_contents <- desc::desc(text = glue::glue("{names(fields)}: {fields}"))
  writeLines(con = normalizePath(file.path(".", "DESCRIPTION")),
             text = desc_contents$str(by_field = TRUE, normalize = FALSE, mode = "file"))
  validation_env1 <- vt_scrape_val_env()
  testthat::expect_equal(0, nrow(validation_env1[validation_env1$type == "package_req",]))
  testthat::expect_identical(sort(strsplit(suggests_field, split = ", ")[[1]]),
                             validation_env1[validation_env1$type == "extended_req", "resource"])
  # Suggest + Depends
  depends_field <- "tidyverse"
  if(depends_field %in% row.names(installed.packages())){
    fields$Depends <- depends_field
    desc_contents2 <- desc::desc(text = glue::glue("{names(fields)}: {fields}"))
    writeLines(con = normalizePath(file.path(".", "DESCRIPTION")),
             text = desc_contents2$str(by_field = TRUE, normalize = FALSE, mode = "file"))
    validation_env2 <- vt_scrape_val_env()
    testthat::expect_equal(3, nrow(validation_env2[validation_env2$type == "extended_req",]))
    testthat::expect_equal(depends_field, validation_env2[validation_env2$type == "package_req", "resource"])
  }

  # all
  imports_field <- "magrittr, plotly"
  if(all(strsplit(imports_field, split = ", ")[[1]] %in% row.names(installed.packages()))){
    fields$Imports <- imports_field
    desc_contents3 <- desc::desc(text = glue::glue("{names(fields)}: {fields}"))
    writeLines(con = normalizePath(file.path(".", "DESCRIPTION")),
             text = desc_contents3$str(by_field = TRUE, normalize = FALSE, mode = "file"))
    validation_env3 <- vt_scrape_val_env()
    testthat::expect_equal(3, nrow(validation_env3[validation_env3$type == "extended_req",]))
    testthat::expect_equal(c(strsplit(imports_field, split = ", ")[[1]], depends_field),
                         validation_env3[validation_env3$type == "package_req", "resource"])
  }

  # package that doesn't exist
  fake_pkg <- "myFakePackage"
  fields$Imports <- fake_pkg
  desc_contents4 <- desc::desc(text = glue::glue("{names(fields)}: {fields}"))
  writeLines(con = normalizePath(file.path(".", "DESCRIPTION")),
             text = desc_contents4$str(by_field = TRUE, normalize = FALSE, mode = "file"))
  testthat::expect_error(vt_scrape_val_env()
                         # ,"there is no package called 'myFakePackage'", perl = TRUE
                         )

  })
})
phuse-org/valtools documentation built on Nov. 10, 2024, 1:36 a.m.