Skip to content

Instantly share code, notes, and snippets.

@maurolepore
Created May 17, 2021 16:45
Show Gist options
  • Save maurolepore/2943e47ad54465de5bfe27854b725b6e to your computer and use it in GitHub Desktop.
Save maurolepore/2943e47ad54465de5bfe27854b725b6e to your computer and use it in GitHub Desktop.
library(testthat)

# R/utils.R
source_all <- function(paths, ...) {
  lapply(paths, source, ...)
  invisible(paths)
}

# test/testthat/test-utils.R
test_that("sources all files", {
  r <- withr::local_tempdir()
  file1 <- fs::file_create(fs::path(r, "x.R"))
  file2 <- fs::file_create(fs::path(r, "y.R"))
  writeLines("x <- 1", file1)
  writeLines("y <- 1", file2)

  
  e <- new.env()
  files <- c("x.R", "y.R")
  source_all(fs::path(r, files), local = e)
  
  expect_equal(ls(envir = e), c("x", "y"))
})
#> Test passed 😀

Created on 2021-05-17 by the reprex package (v2.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment