r-lib / testthat

An R 📦 to make testing 😀
https://testthat.r-lib.org
Other
868 stars 313 forks source link

Guide for mocking `interactive` #1945

Closed pawelru closed 3 months ago

pawelru commented 3 months ago

Currently the guide recommends to set interactive <- NULL but I think it's safer to keep it as function and do interactive <- base::interactive instead. It's important when there is a function call in the package code, i.e. if (interactive()) .... If set to NULL this would throw.

maelle commented 3 months ago

is https://rlang.r-lib.org/reference/is_interactive.html relevant to your use case?

pawelru commented 3 months ago

Actually, I was wrong. This is fully functional.

interactive <- NULL
interactive()
#> [1] FALSE
`if`(interactive(), 1, 2)
#> [1] 2

Created on 2024-03-26 with reprex v2.1.0

And thanks for a good resource. I think it would be nice to link it somewhere in https://testthat.r-lib.org/reference/local_mocked_bindings.html?q=interactive#base-functions. For now, closing this.