tests/testthat/test-process-view.R

rscript <- function() file.path(R.home("bin"), if (.Platform$OS.type == "windows") "Rscript.exe" else "Rscript")

test_that("process_view runs a program and shows stdout and stderr", {
  skip_on_cran()
  finished <- NULL
  pv <- process_view(rscript(), c("-e", "cat('first\\n'); message('problem'); cat('last\\n')"), id = "pv")
  a <- app(pv, on("process.finished", function(event, app) finished <<- event$data))
  pilot <- test_app(a, 60, 8)
  expect_true(pv$running)
  expect_match(pilot$screen_text()[[1]], "running")
  pilot$wait_for_workers(60)
  pilot$step()
  expect_false(pv$running)
  expect_identical(finished$status, 0L)
  expect_identical(finished$state, "completed")
  expect_setequal(pv$output, c("first", "problem", "last"))
  text <- paste(pilot$screen_text(), collapse = "\n")
  expect_match(text, "finished")
  expect_match(text, "first")
  expect_match(text, "problem")
})

test_that("a failing command shows the failure", {
  skip_on_cran()
  pv <- process_view(rscript(), c("-e", "quit(status = 2)"))
  a <- app(pv)
  pilot <- test_app(a, 60, 4)
  pilot$wait_for_workers(60)
  pilot$step()
  expect_match(pilot$screen_text()[[1]], "failed")
  expect_match(pilot$screen_text()[[1]], "status 2")
})

test_that("cancel and restart", {
  skip_on_cran()
  pv <- process_view(rscript(), c("-e", "Sys.sleep(60)"), env = child_tmp(), id = "pv")
  a <- app(pv)
  pilot <- test_app(a, 60, 4)
  expect_true(pv$running)
  expect_s3_class(a$focused, "LogView")
  pilot$press("ctrl+x")
  expect_false(pv$running)
  expect_match(pilot$screen_text()[[1]], "cancelled")
  pv$restart()
  expect_true(pv$running)
  pilot$press("ctrl+r")
  expect_true(pv$running)
  pv$cancel()
})

test_that("autostart = FALSE waits for start()", {
  skip_on_cran()
  pv <- process_view(rscript(), c("-e", "cat('x\\n')"), autostart = FALSE)
  a <- app(pv)
  pilot <- test_app(a, 60, 4)
  expect_false(pv$running)
  expect_match(pilot$screen_text()[[1]], "not started")
  pv$start()
  expect_error(pv$start(), "already running")
  pilot$wait_for_workers(60)
  expect_identical(pv$output, "x")
})

test_that("removing the view cancels its process", {
  skip_on_cran()
  pv <- process_view(rscript(), c("-e", "Sys.sleep(60)"), env = child_tmp())
  host <- vertical(pv, label("x"))
  a <- app(host)
  pilot <- test_app(a, 60, 5)
  w <- pv$worker
  expect_true(w$is_running())
  pv$remove()
  pilot$step()
  expect_identical(w$state, "cancelled")
})

test_that("arguments are validated", {
  expect_error(process_view(c("a", "b")), "command")
  expect_error(process_view("x", args = 1), "character vector")
})

Try the termr package in your browser

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

termr documentation built on Oct. 21, 2026, 5:13 p.m.