Skip to content

Commit

Permalink
Merge pull request #729 from m-muecke/mock
Browse files Browse the repository at this point in the history
tests: move to testthat mocking
  • Loading branch information
gaborcsardi authored Oct 5, 2024
2 parents fbec918 + 62f1e8c commit 524f7d7
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 170 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ Suggests:
htmlwidgets,
knitr,
methods,
mockery,
processx,
ps (>= 1.3.4.9000),
rlang (>= 1.0.2.9003),
rmarkdown,
rprojroot,
rstudioapi,
testthat,
testthat (>= 3.2.0),
tibble,
whoami,
withr
Expand Down
10 changes: 10 additions & 0 deletions R/mocks.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.Call <- NULL
Sys.time <- NULL
commandArgs <- NULL
get <- NULL
getRversion <- NULL
isatty <- NULL
l10n_info <- NULL
loadedNamespaces <- NULL
system <- NULL
system2 <- NULL
31 changes: 16 additions & 15 deletions tests/testthat/test-ansi-hyperlink.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,38 +286,39 @@ test_that("ansi_has_hyperlink_support", {
)

# if no ansi support, then no
mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L)
local_mocked_bindings(num_ansi_colors = function() 256L)
expect_false(ansi_has_hyperlink_support())

# are we in rstudio with support?
mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L)
mockery::stub(ansi_has_hyperlink_support, "rstudio_detect",
list(type = "rstudio_console", hyperlink = TRUE))
local_mocked_bindings(num_ansi_colors = function() 257L)
local_mocked_bindings(
rstudio_detect = function() list(type = "rstudio_console", hyperlink = TRUE)
)
expect_true(ansi_has_hyperlink_support())
})

test_that("ansi_has_hyperlink_support 2", {
local_clean_cli_context()
mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L)
local_mocked_bindings(num_ansi_colors = function() 256L)

mockery::stub(ansi_has_hyperlink_support, "isatty", FALSE)
local_mocked_bindings(isatty = function(...) FALSE)
expect_false(ansi_has_hyperlink_support())
})

test_that("ansi_has_hyperlink_support 3", {
local_clean_cli_context()
mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L)
local_mocked_bindings(num_ansi_colors = function() 256L)

mockery::stub(ansi_has_hyperlink_support, "isatty", TRUE)
mockery::stub(ansi_has_hyperlink_support, "is_windows", TRUE)
local_mocked_bindings(isatty = function(...) TRUE)
local_mocked_bindings(is_windows = function() TRUE)
withr::local_envvar(WT_SESSION = "4c464723-f51f-4612-83f7-31e1c75abd83")
expect_true(ansi_has_hyperlink_support())
})

test_that("ansi_has_hyperlink_support 4", {
local_clean_cli_context()
mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L)
mockery::stub(ansi_has_hyperlink_support, "isatty", TRUE)
local_mocked_bindings(num_ansi_colors = function() 256L)
local_mocked_bindings(isatty = function(...) TRUE)

withr::local_envvar("CI" = "true")
expect_false(ansi_has_hyperlink_support())
Expand All @@ -328,8 +329,8 @@ test_that("ansi_has_hyperlink_support 4", {

test_that("ansi_has_hyperlink_support 5", {
local_clean_cli_context()
mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L)
mockery::stub(ansi_has_hyperlink_support, "isatty", TRUE)
local_mocked_bindings(num_ansi_colors = function() 256L)
local_mocked_bindings(isatty = function(...) TRUE)

withr::local_envvar(
TERM_PROGRAM = "iTerm.app",
Expand All @@ -340,8 +341,8 @@ test_that("ansi_has_hyperlink_support 5", {

test_that("ansi_has_hyperlink_support 5", {
local_clean_cli_context()
mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L)
mockery::stub(ansi_has_hyperlink_support, "isatty", TRUE)
local_mocked_bindings(num_ansi_colors = function() 256L)
local_mocked_bindings(isatty = function(...) TRUE)

withr::local_envvar(VTE_VERSION = "0.51.1")
expect_true(ansi_has_hyperlink_support())
Expand Down
32 changes: 10 additions & 22 deletions tests/testthat/test-format-conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,29 @@ test_that_cli("format_message", {
})

test_that_cli(configs = "ansi", "color in RStudio", {
mockery::stub(
get_rstudio_fg_color0,
"rstudio_detect",
list(type = "rstudio_console", num_colors = 256)
local_mocked_bindings(
rstudio_detect = function() list(type = "rstudio_console", num_colors = 256)
)
mockery::stub(
get_rstudio_fg_color0,
"get_rstudio_theme",
list(foreground = "rgb(0, 0, 0)")
local_mocked_bindings(
get_rstudio_theme = function() list(foreground = "rgb(0, 0, 0)")
)
expect_snapshot({
col <- get_rstudio_fg_color0()
cat(col("this is the new color"))
})

mockery::stub(
get_rstudio_fg_color0,
"get_rstudio_theme",
list()
)
local_mocked_bindings(get_rstudio_theme = function() list())
expect_null(get_rstudio_fg_color0())

mockery::stub(
get_rstudio_fg_color0,
"rstudio_detect",
list(type = "rstudio_console", num_colors = 1)
)
local_mocked_bindings(
rstudio_detect = function() list(type = "rstudio_console", num_colors = 1)
)
expect_null(get_rstudio_fg_color0())
})

test_that_cli(configs = "ansi", "update_rstudio_color", {
mockery::stub(
update_rstudio_color,
"get_rstudio_fg_color",
function() make_ansi_style("#008800")
local_mocked_bindings(
get_rstudio_fg_color = function() make_ansi_style("#008800")
)
expect_snapshot(cat(update_rstudio_color("color me interested")))
})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-links.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ test_that_cli(configs = c("plain", "fancy"), links = c("all", "none"),
cli_text("{.file {paths}}")
}, transform = function(x) sanitize_home(sanitize_wd(x)))

mockery::stub(abs_path1, "is_windows", TRUE)
local_mocked_bindings(is_windows = function() TRUE)
expect_equal(
abs_path1("c:/foo/bar"),
"file://c:/foo/bar"
Expand Down
83 changes: 39 additions & 44 deletions tests/testthat/test-num-ansi-colors.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
test_that("win10_build works for different osVersion", {
mockery::stub(
win10_build, "utils::sessionInfo",
list(running = NULL)
)
expect_identical(win10_build(), 0L)

mockery::stub(
win10_build, "utils::sessionInfo",
list(running = "Debian GNU/Linux 11 (bullseye)")
)
expect_identical(win10_build(), 0L)

mockery::stub(
win10_build, "utils::sessionInfo",
list(running = "Windows 10 x64 (build 16299)")
)
expect_identical(win10_build(), 16299L)
local_mocked_bindings(
sessionInfo = function() list(running = NULL), .package = "utils"
)
expect_identical(win10_build(), 0L)

local_mocked_bindings(
sessionInfo = function() list(running = "Debian GNU/Linux 11 (bullseye)"),
.package = "utils"
)
expect_identical(win10_build(), 0L)

local_mocked_bindings(
sessionInfo = function() list(running = "Windows 10 x64 (build 16299)"),
.package = "utils"
)
expect_identical(win10_build(), 16299L)
})

test_that("cli.default_num_colors #1", {
Expand Down Expand Up @@ -49,9 +48,9 @@ test_that("cli.default_num_colors #2", {
cli.default_num_colors = NULL
)

mockery::stub(num_ansi_colors, "os_type", "windows")
mockery::stub(num_ansi_colors, "commandArgs", "--ess")
mockery::stub(num_ansi_colors, "is_emacs_with_color", TRUE)
local_mocked_bindings(os_type = function() "windows")
local_mocked_bindings(commandArgs = function() "--ess")
local_mocked_bindings(is_emacs_with_color = function() TRUE)

expect_equal(num_ansi_colors(), 8L)

Expand All @@ -76,8 +75,8 @@ test_that("cli.default_num_colors #4", {
# Unix emacs with color
withr::local_envvar(COLORTERM = NA_character_)

mockery::stub(detect_tty_colors, "os_type", "unix")
mockery::stub(detect_tty_colors, "is_emacs_with_color", TRUE)
local_mocked_bindings(os_type = function() "unix")
local_mocked_bindings(is_emacs_with_color = function() TRUE)

withr::local_options(cli.default_num_colors = NULL)

Expand All @@ -92,14 +91,12 @@ test_that("cli.default_num_colors #5", {
# rstudio terminal on Windows
withr::local_envvar(COLORTERM = NA_character_)

mockery::stub(detect_tty_colors, "os_type", "windows")
mockery::stub(detect_tty_colors, "win10_build", 10586)
mockery::stub(
detect_tty_colors,
"rstudio_detect",
list(type = "rstudio_terminal")
local_mocked_bindings(os_type = function() "windows")
local_mocked_bindings(win10_build = function() 10586)
local_mocked_bindings(
rstudio_detect = function() list(type = "rstudio_terminal")
)
mockery::stub(detect_tty_colors, "system2", TRUE)
local_mocked_bindings(system2 = function(...) TRUE)

withr::local_options(cli.default_num_colors = NULL)
expect_equal(detect_tty_colors(), 8L)
Expand All @@ -114,17 +111,15 @@ test_that("cli.default_num_colors #6", {
withr::local_envvar(COLORTERM = NA_character_)
withr::local_options(cli.default_num_colors = NULL)

mockery::stub(detect_tty_colors, "os_type", "windows")
mockery::stub(detect_tty_colors, "win10_build", 10586)
mockery::stub(
detect_tty_colors,
"rstudio_detect",
list(type = "not_rstudio")
local_mocked_bindings(os_type = function() "windows")
local_mocked_bindings(win10_build = function() 10586)
local_mocked_bindings(
rstudio_detect = function() list(type = "not_rstudio")
)
mockery::stub(detect_tty_colors, "system2", TRUE)
local_mocked_bindings(system2 = function(...) TRUE)
expect_equal(detect_tty_colors(), 256L)

mockery::stub(detect_tty_colors, "win10_build", 14931)
local_mocked_bindings(win10_build = function() 14931)
expect_equal(detect_tty_colors(), truecolor)

withr::local_options(cli.default_num_colors = 123L)
Expand All @@ -139,8 +134,8 @@ test_that("cli.default_num_colors #7", {
ConEmuANSI = "ON"
)
withr::local_options(cli.default_num_colors = NULL)
mockery::stub(detect_tty_colors, "os_type", "windows")
mockery::stub(detect_tty_colors, "win10_build", 1)
local_mocked_bindings(os_type = function() "windows")
local_mocked_bindings(win10_build = function() 1)

expect_equal(detect_tty_colors(), 8L)
withr::local_options(cli.default_num_colors = 123L)
Expand All @@ -155,9 +150,9 @@ test_that("cli.default_num_colors #8", {
TERM = "xterm"
)

mockery::stub(detect_tty_colors, "os_type", "unix")
mockery::stub(detect_tty_colors, "is_emacs_with_color", FALSE)
mockery::stub(detect_tty_colors, "system", "8")
local_mocked_bindings(os_type = function() "unix")
local_mocked_bindings(is_emacs_with_color = function() FALSE)
local_mocked_bindings(system = function(...) "8")

withr::local_options(cli.default_num_colors = NULL)
expect_equal(detect_tty_colors(), 256L)
Expand All @@ -172,8 +167,8 @@ test_that("ESS_BACKGROUND_MODE", {
ESS_BACKGROUND_MODE = NA_character_
)

mockery::stub(detect_dark_theme, "is_iterm", FALSE)
mockery::stub(detect_dark_theme, "is_emacs", TRUE)
local_mocked_bindings(is_iterm = function() FALSE)
local_mocked_bindings(is_emacs = function() TRUE)

expect_false(detect_dark_theme("auto"))

Expand Down
20 changes: 7 additions & 13 deletions tests/testthat/test-prettycode.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,14 @@ test_that("code themes", {
withr::local_options(cli.code_theme_terminal = "solarized_light")
expect_equal(code_theme_default()$reserved, "#859900")

mockery::stub(
code_theme_default,
"rstudio_detect",
list(type = "rstudio_console")
local_mocked_bindings(
rstudio_detect = function() list(type = "rstudio_console")
)
withr::local_options(cli.code_theme_rstudio = "Xcode")
expect_equal(code_theme_default()$reserved, "#C800A4")

withr::local_options(cli.code_theme_rstudio = NULL)
mockery::stub(code_theme_default, "code_theme_default_rstudio", "foo")
local_mocked_bindings(code_theme_default_rstudio = function() "foo")
expect_equal(code_theme_default(), "foo")
})

Expand All @@ -199,17 +197,13 @@ test_that("code themes 2", {
})

test_that("code_theme_default_rstudio", {
mockery::stub(
code_theme_default_rstudio,
"get_rstudio_theme",
list(editor = "Solarized Dark")
local_mocked_bindings(
get_rstudio_theme = function() list(editor = "Solarized Dark")
)
expect_equal(code_theme_default_rstudio()$reserved, "#859900")

mockery::stub(
code_theme_default_rstudio,
"get_rstudio_theme",
list(editor = "Not really")
local_mocked_bindings(
get_rstudio_theme = function() list(editor = "Not really")
)
expect_warning(
cth <- code_theme_default_rstudio(),
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-progress-along.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test_that("old R is just seq_along", {
# It is tricky to check that we get seq_along(), because
# identical(cli_progress_along(1:10), seq_along(1:10)) holds,
# so we just check that no progress bar is created.
mockery::stub(cli_progress_along, "getRversion", package_version("3.4.0"))
local_mocked_bindings(getRversion = function() package_version("3.4.0"))
snapshot <- names(clienv$progress)
it <- cli_progress_along(1:10)
expect_identical(snapshot, names(clienv$progress))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-progress-handler-logger.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ test_that("loggerr_out", {
bar$id <- "id"
bar$current <- 13
bar$total <- 113
mockery::stub(logger_out, "Sys.time", .POSIXct(1623325865, tz = "CET"))
local_mocked_bindings(Sys.time = function() .POSIXct(1623325865, tz = "CET"))
expect_snapshot(logger_out(bar, "updated"))
})
2 changes: 1 addition & 1 deletion tests/testthat/test-progress-handler-say.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test_that("say_out", {

test_that("say_update", {
withr::local_options(cli.progress_say_frequency = 1e-9)
mockery::stub(say_update, "say_out", function(text) text)
local_mocked_bindings(say_out = function(text) text)
bar <- new.env(parent = emptyenv())
bar$current <- 10
bar$total <- NA
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-progress-handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test_that("cli_progress_select_handlers #2", {
baz = list(),
forced = list()
)
mockery::stub(cli_progress_select_handlers, "builtin_handlers", fake)
local_mocked_bindings(builtin_handlers = function() fake)
expect_equal(cli_progress_select_handlers(), fake["bar"])
})

Expand All @@ -49,7 +49,7 @@ test_that("cli_progress_select_handlers #3", {
baz = list(),
forced = list()
)
mockery::stub(cli_progress_select_handlers, "builtin_handlers", fake)
local_mocked_bindings(builtin_handlers = function() fake)
expect_equal(cli_progress_select_handlers(), fake[c("bar", "forced")])
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-progress-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test_that("should_run_progress_examples", {
withr::local_envvar(NOT_CRAN = "true")
expect_true(should_run_progress_examples())

mockery::stub(should_run_progress_examples, "is_rcmd_check", TRUE)
local_mocked_bindings(is_rcmd_check = function() TRUE)
expect_false(should_run_progress_examples())
})

Expand Down
Loading

0 comments on commit 524f7d7

Please sign in to comment.