tests/testthat/test-failure-output.R

test_that("output is printed on failure", {
  skip_on_cran()
  skip_on_covr()

  # Full output in interactive sessions
  badcompile <- paste0(
    "local::",
    normalizePath(test_path("fixtures/badcompile"))
  )
  tmp <- tempfile()
  on.exit(unlink(tmp), add = TRUE)

  expect_error(
    callr::r(stdout = tmp, stderr = tmp, function(pkg) {
      pkgload::load_all()
      options(rlib_interactive = TRUE)
      pak::pkg_install(pkg)
    }, list(pkg = badcompile))
  )

  lines <- readLines(tmp)
  expect_true(any(grepl("Failed to build badcompile", lines)))
  expect_true(any(grepl("Full installation output", lines)))
  expect_true(any(grepl(
    "compilation failed for package .*badcompile.*",
    lines
  )))

  expect_error(
    callr::r(stdout = tmp, stderr = tmp, function(pkg) {
      pkgload::load_all()
      options(rlib_interactive = FALSE)
      pak::pkg_install(pkg)
    }, list(pkg = badcompile))
  )

  lines <- readLines(tmp)
  expect_true(any(grepl("Failed to build badcompile", lines)))
  expect_true(any(grepl("Full installation output", lines)))
  expect_true(any(grepl(
    "compilation failed for package .*badcompile.*",
    lines
  )))
})
r-lib/pkg documentation built on May 6, 2024, 3:04 p.m.