tests/testthat/test-config.R

test_that("can set path to local config", {
  tmp <- tempdir()
  test.pkg <- fs::dir_create(tmp, "test.proj")
  expect_equal(
    set_config_source(NULL, root = test.pkg),
    system.file("pre-commit-config-proj.yaml", package = "precommit")
  )
  expect_error(
    set_config_source(tempfile()),
    "does not exist"
  )
})

test_that("can set path to remote config", {
  skip_on_cran()
  path <- set_config_source(
    example_remote_config()
  )
  expect_equal(fs::path_ext(path), "yaml")
  expect_silent(yaml::read_yaml(path))

  expect_error(set_config_source("https://apple.com"), "valid yaml")
})

test_that("defaults to right config depending on whether or not root is a pkg", {
  tmp <- tempdir()
  test.pkg <- fs::dir_create(tmp, "test.pkg")
  withr::with_dir(test.pkg, {
    desc <- desc::description$new("!new")
    desc$set(Package = "test.pkg")
    desc$write("DESCRIPTION")
  })
  expect_message(
    set_config_source(NULL, root = test.pkg),
    "pkg\\.yaml"
  )
  fs::file_delete(fs::path(test.pkg, "DESCRIPTION"))
  expect_message(
    set_config_source(NULL, root = test.pkg),
    "proj\\.yaml"
  )
})

Try the precommit package in your browser

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

precommit documentation built on Oct. 23, 2020, 6:18 p.m.