tests/testthat/test-find-package-root.R

test_that("find_package_root", {
  tmp <- tempfile()
  on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
  mkdirp(file.path(tmp, "a", "b", "c", "d"))
  lns <- "Package: this"
  writeLines(lns, file.path(tmp, "DESCRIPTION"))

  expect_equal(
    readLines(file.path(find_package_root(tmp), "DESCRIPTION")),
    lns
  )

  expect_equal(
    readLines(file.path(
      find_package_root(file.path(tmp, "a")),
      "DESCRIPTION"
    )),
    lns
  )

  expect_equal(
    readLines(file.path(
      find_package_root(file.path(tmp, "a", "b", "c", "d")),
      "DESCRIPTION"
    )),
    lns
  )

  wd <- getwd()
  on.exit(setwd(wd), add = TRUE)
  setwd(file.path(tmp, "a", "b", "c"))
  expect_equal(
    readLines(file.path(find_package_root("."), "DESCRIPTION")),
    lns
  )
})

test_that("find_package_root errors", {
  expect_snapshot(error = TRUE, find_package_root("file1130e91427d3"))

  if (!file.exists("/DESCRIPTION")) {
    expect_snapshot(error = TRUE, find_package_root("/"))
  }
})

Try the pkgbuild package in your browser

Any scripts or data that you put into this service are public.

pkgbuild documentation built on June 8, 2025, 11:38 a.m.