0

I'm making a package which contains a function that calls rstudioapi::jobRunScript(), and I would like to to be able to write tests for this function that can be run normally by devtools::test(). The package is only intended for use during interactive RStudio sessions.

Here's a minimal reprex:

After calling usethis::create_package() to initialize my package, and then usethis::use_r("rstudio") to create R/rstudio.R, I put:

foo_rstudio <- function(...) {

  script.file <- tempfile()
  write("print('hello')", file = script.file)
  rstudioapi::jobRunScript(
    path = script.file,
    name = "foo",
    importEnv = FALSE,
    exportEnv = "R_GlobalEnv"
  )
}

I then call use_test() to make an accompanying test file, in which I put:

test_that("foo works", {
  foo_rstudio()
})

I then run devtools::test() and get:

RStudio not running

I think I understand the basic problem here: devtools runs a separate R session for the tests, and that session doesn't have access to RStudio. I see here that rstudioapi can work inside child R sessions, but seemingly only those "normally launched by RStudio."

I'd really like to use devtools to test my function as I develop it. I suppose I could modify my function to accept an argument passed from the test code which will simply run the job in the R session itself or in some other kind of child R process, instead of an RStudio job, but then I'm not actually testing the normal intended functionality, and if there's an issue which is specific to the rstudioapi::jobRunScript() call and which could occur during normal use, then my tests wouldn't be able to pick it up.

Is there a way to initialize an RStudio process from within a devtools::test() session, or some other solution here?

lost
  • 1,483
  • 1
  • 11
  • 19

0 Answers0