tests/testthat/helper.R

# Adapted from Jenny Bryan's tutorial
# https://www.tidyverse.org/blog/2020/04/self-cleaning-test-fixtures/
create_local_project <- function(dir = fs::file_temp(), env = parent.frame()) {
  # --- Record starting state ---
  old_project <- usethis::proj_get()

  withr::defer({
    # --- Defer The Undoing ---
    # restore active usethis project (-C)
    usethis::proj_set(old_project, force = TRUE)
    # restore working directory      (-B)
    setwd(old_project)
    # delete the temporary package   (-A)
    fs::dir_delete(dir)
  }, envir = env)

  # --- Do The Doing ---

  # create new folder and package  (A)
  usethis::create_project(dir, open = FALSE)
  # change working directory       (B)
  setwd(dir)
  # switch to new usethis project  (C)
  usethis::proj_set(dir)
  invisible(dir)
}
tjmahr/usedrake documentation built on Aug. 16, 2020, 9:38 p.m.