# abs_path
testthat::test_that("Absolute paths", {
testthat::expect_equal(
abs_path("~", "R"),
fs::path_abs(fs::path_expand(fs::path("~", "R")))
)
testthat::expect_equal(
abs_path(),
fs::path_wd()
)
})
# capture_cli
testthat::test_that("Capture cli single message", {
pkg_var <- "testPkg"
err_msg <- c("The package {.pkg {pkg_var}} is not installed.")
testthat::expect_equal(
capture_cli(msg = cli::cli_text(err_msg), remove_cli_style = TRUE),
"The package testPkg is not installed.\n"
)
formated_output <- capture_cli(
cli::cli_text(err_msg), remove_cli_style = FALSE
)
testthat::expect_equal(
formated_output,
"The package testPkg is not installed.\n"
)
})
testthat::test_that("Capture cli multi-line message", {
fun_var <- "testFun"
cmd_var <- "ls"
formated_output <- capture_cli({
cli::cli_ul("The {.fun { fun_var }} not.")
cli::cli_li("Check {.url test.com}.")
cli::cli_li("Exec {.code { cmd_var }}.")
cli::cli_end()
})
testthat::expect_equal(
formated_output,
"* The `testFun()` not.\n* Check <test.com>.\n* Exec `ls`.\n"
)
})
# cli_abort
testthat::test_that("cli alert with abort", {
pkg_var <- "testPkg"
err_msg <- "Package {.pkg { pkg_var }} is not installed."
testthat::expect_error(
cli_abort(err_msg),
regexp = "Package testPkg is not installed."
)
})
# get_proj_path
testthat::test_that("Get project path", {
# both empty
testthat::expect_equal(
get_proj_path(),
usethis::proj_get()
)
# "."
testthat::expect_equal(
get_proj_path("."),
usethis::proj_get()
)
# empty name and valid project dir
testthat::expect_error(
get_proj_path(project_dir = fs::path_temp()),
regexp = "`pkg_name` must be supplied with `project_dir`"
)
# valid name that exist in current path and empty project dir
tmp_prj <- fs::path_temp()
prj_name <- "testPrj"
suppressMessages({withr::with_dir(
new = tmp_prj,
code = {
usethis::create_project(
path = fs::path(tmp_prj, prj_name),
rstudio = TRUE,
open = FALSE
)
prj_res <- get_proj_path(pkg_name = prj_name)
}
)})
testthat::expect_equal(
prj_res,
abs_path(tmp_prj, prj_name)
)
# Project dir is not null and don't exist
testthat::expect_error(
get_proj_path(project_dir = fs::path_temp("dont", "exist")),
regexp = glue::glue(
"{ fs::path_temp('dont', 'exist') } is not a valid path"
)
)
# valid name that don't exist in current path and empty project dir
testthat::expect_error(
get_proj_path(pkg_name = "testePkg"),
regexp = "`pkg_name` is invalid"
)
testthat::expect_error(
get_proj_path(pkg_name = fs::path_temp("testePkgFake")),
regexp = "`pkg_name` is invalid"
)
# Both exists and are valid
tmp_root <- fs::path_temp()
testthat::expect_equal(
get_proj_path("testPkg", tmp_root),
fs::path(tmp_root, "testPkg")
)
})
# is_bioc
testthat::test_that("Package is from BioC", {
testthat::expect_error(
is_bioc(fs::path_temp("path", "dont", "exist")),
regexp = "`pkg_name` is invalid"
)
tmp_prj <- fs::path_temp()
prj_name <- "testCran"
suppressMessages({withr::with_dir(
new = tmp_prj,
code = {
usethis::create_package(
path = prj_name,
rstudio = TRUE,
open = FALSE
)
}
)})
testthat::expect_false(is_bioc(fs::path(tmp_prj, "testCran")))
prj_name <- "testBioc"
suppressMessages({withr::with_dir(
new = tmp_prj,
code = {
usethis::create_package(
path = prj_name,
fields = list(
biocViews = "Genomics"
),
rstudio = TRUE,
open = FALSE
)
}
)})
testthat::expect_true(is_bioc(fs::path(tmp_prj, "testBioc")))
})
# is_available for package
testthat::test_that("R package is available", {
suppressMessages({
testthat::expect_error(
is_available(),
regexp = "Argument `package_name` must not be empty\\."
)
testthat::expect_true(is_available("fs"))
testthat::expect_message(
is_available("fs"),
regexp = "Using .*\\."
)
testthat::expect_true(is_available("fs", type = "R"))
testthat::expect_true(is_available("base", type = "RsTaTs"))
testthat::expect_true(is_available("fs", type = "R_PKG"))
testthat::expect_false(is_available("fakePKG", type = "rstats"))
testthat::expect_message(
is_available("fakePKG", type = "rstats"),
regexp = "Package .* is not installed\\."
)
})
})
# is_available for python modules
testthat::test_that("Python module is available", {
# TODO skip python on CRAN?
# testthat::skip_on_cran()
suppressMessages({
testthat::expect_true(is_available("os", "py"))
testthat::expect_false(is_available("fakemoduletest", "py"))
testthat::expect_message(
is_available("fakemoduletest", "PyThOn"),
regexp = "Python module .* is not installed\\."
)
})
})
# text_files_are_equal
testthat::test_that("File compare", {
testthat::expect_error(text_files_are_equal())
})
# shell_exec
testthat::test_that("cli exec", {
sh_res <- shell_exec("ls { get_proj_path() }")
testthat::expect_true(stringr::str_detect(sh_res$stdout, "DESCRIPTION"))
# TODO force processx package to don't be reached
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.