tests/testthat/test-list_files.R

test_that("list_files works as expected", {

  # prep: write some files to tmp directory
  out <- data.frame(x = 1:2, y = 1:2)
  tmp <- tempdir()
  tmp_write <- file.path(tmp, "tmp_write")
  if (!dir.exists(tmp_write)) dir.create(tmp_write)

  files_out <- c(
    "test-file.csv",
    "test-file2.csv",
    "my-test-file_2020-03-05.csv",
    "my-test-file_2020-03-12.csv",
    "my-test-file_2020-03-19.csv"
  )

  for (filename in files_out) {
    write.csv(out, file.path(tmp_write, filename))
  }


  # tests
  x1 <- list_files(tmp_write, full.names = TRUE)
  y1 <- list.files(tmp_write, full.names = TRUE)
  expect_equal(x1, y1)

  x2 <- list_files(tmp_write, full.names = FALSE, pattern = "^test-file")
  y2 <- list.files(tmp_write, full.names = FALSE, pattern = "^test-file")
  expect_equal(x2, y2)

  x3 <- list_files(tmp_write, full.names = FALSE, pattern = "^my-test", select = "latest")
  y3 <- "my-test-file_2020-03-19.csv"
  expect_equal(x3, y3)

  expect_error(list_files(tmp_write, pattern = "^test-file", select = "latest"))

  # clean up temp files
  unlink(tmp_write, recursive = TRUE)
})
epicentre-msf/llutils documentation built on Nov. 9, 2020, 8:24 p.m.