From ab4a2eacc107a446b29c34a9e27772206871a39f Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Thu, 15 Aug 2024 10:40:51 -0700 Subject: [PATCH 1/6] Remove mockery Fixes #57 I don't love the implementation for package.R. If there's a less-kludgey way to do that, I'd be happy to do so! --- .Rbuildignore | 1 + DESCRIPTION | 1 - R/colors.R | 6 ++++- R/debug.R | 6 ++++- R/package.R | 5 +++- debugme.Rproj | 18 +++++++++++++++ tests/testthat/test-colors.R | 14 ++++++++---- tests/testthat/test-debug.R | 43 ++++++++++++++++++----------------- tests/testthat/test-levels.R | 2 +- tests/testthat/test-package.R | 10 ++++---- 10 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 debugme.Rproj diff --git a/.Rbuildignore b/.Rbuildignore index 4548e5d..34918a1 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -12,3 +12,4 @@ ^codecov\.yml$ ^LICENSE\.md$ ^screencast.gif$ +^debugme\.Rproj$ diff --git a/DESCRIPTION b/DESCRIPTION index dba6d43..747f4a9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,7 +18,6 @@ Imports: grDevices Suggests: covr, - mockery, R6, testthat (>= 3.0.0), withr diff --git a/R/colors.R b/R/colors.R index 99c5b03..c8a09a2 100644 --- a/R/colors.R +++ b/R/colors.R @@ -15,7 +15,11 @@ initialize_colors <- function(debug_pkgs) { names = debug_pkgs ) - assign("palette", palette, envir = debug_data) + assign_debug("palette", palette) +} + +assign_debug <- function(x, value) { + assign(x, value, envir = debug_data) } #' @importFrom crayon make_style diff --git a/R/debug.R b/R/debug.R index 1afdeda..d480d4f 100644 --- a/R/debug.R +++ b/R/debug.R @@ -149,7 +149,11 @@ get_timestamp_diff <- function() { } get_timestamp_stamp <- function() { - paste0(format_date(Sys.time()), " ") + paste0(format_date(systime()), " ") +} + +systime <- function() { + Sys.time() } format_date <- function(date) { diff --git a/R/package.R b/R/package.R index 6169d36..8ce3252 100644 --- a/R/package.R +++ b/R/package.R @@ -100,7 +100,7 @@ debugme <- function(env = topenv(parent.frame()), refresh_pkg_info() - if (!is_debugged(pkg)) return() + if (!is_debugged2(pkg)) return() should_instrument <- function(x) { obj <- get(x, envir = env) @@ -118,6 +118,9 @@ debugme <- function(env = topenv(parent.frame()), is_debugged <- function(pkg) { pkg %in% names(debug_data$palette) } +# To allow targetted mocking. +is_debugged2 <- is_debugged + debug_data <- new.env() debug_data$timestamp <- NULL diff --git a/debugme.Rproj b/debugme.Rproj new file mode 100644 index 0000000..5d79cb7 --- /dev/null +++ b/debugme.Rproj @@ -0,0 +1,18 @@ +Version: 1.0 + +RestoreWorkspace: No +SaveWorkspace: No +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace diff --git a/tests/testthat/test-colors.R b/tests/testthat/test-colors.R index 4167122..34dcd6e 100644 --- a/tests/testthat/test-colors.R +++ b/tests/testthat/test-colors.R @@ -2,8 +2,11 @@ test_that("color palette is fine", { val <- NULL - mockery::stub(initialize_colors, "assign", - function(x, value, envir, ...) val <<- value) + local_mocked_bindings( + assign_debug = function(x, value) { + val <<- value + } + ) initialize_colors(c("foo", "bar")) expect_equal(names(val), c("foo", "bar")) expect_true(all(val %in% grDevices::colors())) @@ -16,9 +19,10 @@ test_that("color palette is fine", { ## Quite an artificial test case... test_that("get a package style", { - - mockery::stub(get_package_style, "is_debugged", function(...) TRUE) - mockery::stub(get_package_style, "make_style", function(x) substitute(x)) + local_mocked_bindings( + is_debugged = function(...) TRUE, + make_style = function(x) substitute(x) + ) ret <- get_package_style("pkg") expect_equal(ret, quote(debug_data$palette[pkg])) }) diff --git a/tests/testthat/test-debug.R b/tests/testthat/test-debug.R index 22eff85..0796a14 100644 --- a/tests/testthat/test-debug.R +++ b/tests/testthat/test-debug.R @@ -1,20 +1,20 @@ test_that("debug indent", { f1 <- function() { - debug("f1") - f2() + debug("f1") + f2() } - + f2 <- function() { - debug("f2.1") - f3() - debug("f2.2") + debug("f2.1") + f3() + debug("f2.2") } f3 <- function() { - debug("f3") + debug("f3") } - + out <- capture_output(eval({ debug("f0.1"); f1(); f2(); debug("f0.2")})) - + expect_match(out, 'debugme f0.1', fixed = TRUE) expect_match(out, 'debugme +-f1', fixed = TRUE) expect_match(out, 'debugme +-f2.1', fixed = TRUE) @@ -23,12 +23,12 @@ test_that("debug indent", { expect_match(out, 'debugme +-f2.1', fixed = TRUE) expect_match(out, 'debugme -f2.2', fixed = TRUE) expect_match(out, 'debugme f0.2', fixed = TRUE) - + out <- withr::with_envvar( c(DEBUGME_SHOW_STACK = "no"), capture_output(eval({ debug("f0.1"); f1(); f2(); debug("f0.2")})) ) - + expect_match(out, 'debugme f0.1', fixed = TRUE) expect_match(out, 'debugme f1', fixed = TRUE) expect_match(out, 'debugme f2.1', fixed = TRUE) @@ -40,8 +40,9 @@ test_that("debug indent", { }) test_that("debug levels", { - - mockery::stub(debug, "get_package_debug_level", 1) + local_mocked_bindings( + get_package_debug_level = function(...) 1 + ) env <- new.env() env$f1 <- function() debug("foobar", level = 1) env$f2 <- function() debug("baz", level = 2) @@ -65,10 +66,10 @@ test_that("format_date", { }) test_that("get_timestamp_stamp", { - + mytime <- structure(1477967634, class = c("POSIXct", "POSIXt"), tzone = "UTC") - mockery::stub(get_timestamp_stamp, "Sys.time", mytime) + local_mocked_bindings(systime = function() mytime) expect_equal( get_timestamp_stamp(), "2016-11-01T02:33:54.54.000+00:00 " @@ -78,16 +79,16 @@ test_that("get_timestamp_stamp", { test_that("debugging to a file", { tmp <- tempfile() on.exit(unlink(tmp), add = TRUE) - + on.exit(initialize_output_file(), add = TRUE) on.exit(try(close(debug_data$output_fd), silent = TRUE), add = TRUE) - + withr::with_envvar(c(DEBUGME_OUTPUT_FILE = tmp), { initialize_output_file() }) debug("hello world!", "foobar") debug("hello again!", "foo") - + log <- readLines(tmp) expect_match(log[1], "^foobar hello world!") expect_match(log[2], "^foo hello again!") @@ -97,16 +98,16 @@ test_that("debugging to a directory", { tmp <- tempfile() dir.create(tmp) on.exit(unlink(tmp, recursive = TRUE), add = TRUE) - + on.exit(initialize_output_file(), add = TRUE) on.exit(try(close(debug_data$output_fd), silent = TRUE), add = TRUE) - + withr::with_envvar(c(DEBUGME_OUTPUT_DIR = tmp), { initialize_output_file() }) debug("hello world!", "foobar") debug("hello again!", "foo") - + log <- readLines(file.path(tmp, paste0("debugme-", Sys.getpid(), ".log"))) expect_match(log[1], "^foobar hello world!") expect_match(log[2], "^foo hello again!") diff --git a/tests/testthat/test-levels.R b/tests/testthat/test-levels.R index b91cfac..0a5d128 100644 --- a/tests/testthat/test-levels.R +++ b/tests/testthat/test-levels.R @@ -27,7 +27,7 @@ test_that("log levels work properly", { ) for (pkg_level in 1:6) { - mockery::stub(debug, "get_package_debug_level", pkg_level) + local_mocked_bindings(get_package_debug_level = function(...) pkg_level) for (idx in seq_along(fs)) { if (idx <= pkg_level) { expect_output(fs[[idx]][[2]](), fs[[idx]][[1]]) diff --git a/tests/testthat/test-package.R b/tests/testthat/test-package.R index b3f30a0..cacc5b4 100644 --- a/tests/testthat/test-package.R +++ b/tests/testthat/test-package.R @@ -2,7 +2,9 @@ test_that(".onLoad", { val <- NULL - mockery::stub(refresh_pkg_info, "initialize_colors", function(pkgs) val <<- pkgs) + local_mocked_bindings( + initialize_colors = function(pkgs) val <<- pkgs + ) withr::with_envvar( c("DEBUGME" = c("foo,bar")), refresh_pkg_info() @@ -20,7 +22,7 @@ test_that("debugme", { expect_silent(debugme(env)) - mockery::stub(debugme, "is_debugged", TRUE) + local_mocked_bindings(is_debugged2 = function(...) TRUE) debugme(env) expect_silent(env$f1()) @@ -35,7 +37,7 @@ test_that("instrument environments", { env$env <- new.env() env$env$fun <- function() { "!DEBUG coocoo" } - mockery::stub(debugme, "is_debugged", TRUE) + local_mocked_bindings(is_debugged2 = function(...) TRUE) expect_silent(debugme(env)) expect_output(env$env$fun(), "coocoo") @@ -61,7 +63,7 @@ test_that("instrument R6 classes", { ) ) - mockery::stub(debugme, "is_debugged", TRUE) + local_mocked_bindings(is_debugged2 = function(...) TRUE) expect_silent(debugme(env)) expect_output(x <- env$class$new("mrx"), "debugme.*creating mrx") From 07262ba9f1da697061e86c1f0d542dda17131138 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Thu, 15 Aug 2024 13:01:19 -0700 Subject: [PATCH 2/6] Address review comments. --- R/debug.R | 6 +----- tests/testthat/test-debug.R | 13 +------------ tests/testthat/test-package.R | 12 ------------ 3 files changed, 2 insertions(+), 29 deletions(-) diff --git a/R/debug.R b/R/debug.R index d480d4f..1afdeda 100644 --- a/R/debug.R +++ b/R/debug.R @@ -149,11 +149,7 @@ get_timestamp_diff <- function() { } get_timestamp_stamp <- function() { - paste0(format_date(systime()), " ") -} - -systime <- function() { - Sys.time() + paste0(format_date(Sys.time()), " ") } format_date <- function(date) { diff --git a/tests/testthat/test-debug.R b/tests/testthat/test-debug.R index 0796a14..1da462a 100644 --- a/tests/testthat/test-debug.R +++ b/tests/testthat/test-debug.R @@ -3,7 +3,6 @@ test_that("debug indent", { debug("f1") f2() } - f2 <- function() { debug("f2.1") f3() @@ -12,9 +11,7 @@ test_that("debug indent", { f3 <- function() { debug("f3") } - out <- capture_output(eval({ debug("f0.1"); f1(); f2(); debug("f0.2")})) - expect_match(out, 'debugme f0.1', fixed = TRUE) expect_match(out, 'debugme +-f1', fixed = TRUE) expect_match(out, 'debugme +-f2.1', fixed = TRUE) @@ -28,7 +25,6 @@ test_that("debug indent", { c(DEBUGME_SHOW_STACK = "no"), capture_output(eval({ debug("f0.1"); f1(); f2(); debug("f0.2")})) ) - expect_match(out, 'debugme f0.1', fixed = TRUE) expect_match(out, 'debugme f1', fixed = TRUE) expect_match(out, 'debugme f2.1', fixed = TRUE) @@ -66,10 +62,9 @@ test_that("format_date", { }) test_that("get_timestamp_stamp", { - mytime <- structure(1477967634, class = c("POSIXct", "POSIXt"), tzone = "UTC") - local_mocked_bindings(systime = function() mytime) + local_mocked_bindings(Sys.time = function() mytime, .package = "base") expect_equal( get_timestamp_stamp(), "2016-11-01T02:33:54.54.000+00:00 " @@ -79,16 +74,13 @@ test_that("get_timestamp_stamp", { test_that("debugging to a file", { tmp <- tempfile() on.exit(unlink(tmp), add = TRUE) - on.exit(initialize_output_file(), add = TRUE) on.exit(try(close(debug_data$output_fd), silent = TRUE), add = TRUE) - withr::with_envvar(c(DEBUGME_OUTPUT_FILE = tmp), { initialize_output_file() }) debug("hello world!", "foobar") debug("hello again!", "foo") - log <- readLines(tmp) expect_match(log[1], "^foobar hello world!") expect_match(log[2], "^foo hello again!") @@ -98,16 +90,13 @@ test_that("debugging to a directory", { tmp <- tempfile() dir.create(tmp) on.exit(unlink(tmp, recursive = TRUE), add = TRUE) - on.exit(initialize_output_file(), add = TRUE) on.exit(try(close(debug_data$output_fd), silent = TRUE), add = TRUE) - withr::with_envvar(c(DEBUGME_OUTPUT_DIR = tmp), { initialize_output_file() }) debug("hello world!", "foobar") debug("hello again!", "foo") - log <- readLines(file.path(tmp, paste0("debugme-", Sys.getpid(), ".log"))) expect_match(log[1], "^foobar hello world!") expect_match(log[2], "^foo hello again!") diff --git a/tests/testthat/test-package.R b/tests/testthat/test-package.R index cacc5b4..f89a5a9 100644 --- a/tests/testthat/test-package.R +++ b/tests/testthat/test-package.R @@ -1,7 +1,5 @@ test_that(".onLoad", { - val <- NULL - local_mocked_bindings( initialize_colors = function(pkgs) val <<- pkgs ) @@ -13,18 +11,14 @@ test_that(".onLoad", { }) test_that("debugme", { - env <- new.env() env$f1 <- function() { "nothing here" } env$f2 <- function() { "!DEBUG foobar" } env$notme <- "!DEBUG nonono" env$.hidden <- function() { "!DEBUG foobar2" } - expect_silent(debugme(env)) - local_mocked_bindings(is_debugged2 = function(...) TRUE) debugme(env) - expect_silent(env$f1()) expect_output(env$f2(), "debugme foobar \\+[0-9]+ms") expect_identical(env$notme, "!DEBUG nonono") @@ -32,19 +26,15 @@ test_that("debugme", { }) test_that("instrument environments", { - env <- new.env() env$env <- new.env() env$env$fun <- function() { "!DEBUG coocoo" } - local_mocked_bindings(is_debugged2 = function(...) TRUE) expect_silent(debugme(env)) - expect_output(env$env$fun(), "coocoo") }) test_that("instrument R6 classes", { - env <- new.env() env$class <- R6::R6Class( "foobar", @@ -62,10 +52,8 @@ test_that("instrument R6 classes", { name = NULL ) ) - local_mocked_bindings(is_debugged2 = function(...) TRUE) expect_silent(debugme(env)) - expect_output(x <- env$class$new("mrx"), "debugme.*creating mrx") expect_output(x$hello(), "debugme.*hello mrx") }) From 0b04f8853e03b404661cdf0f8118f970e80ab403 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Thu, 15 Aug 2024 13:10:07 -0700 Subject: [PATCH 3/6] Slightly clearer commenting about is_debugged(). --- R/package.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/package.R b/R/package.R index 8ce3252..be63bbb 100644 --- a/R/package.R +++ b/R/package.R @@ -115,10 +115,11 @@ debugme <- function(env = topenv(parent.frame()), ) } +# Used in get_package_style() is_debugged <- function(pkg) { pkg %in% names(debug_data$palette) } -# To allow targetted mocking. +# To allow targetted mocking in debugme() is_debugged2 <- is_debugged From 3a3bdf83878ce7f57a49ed4d28887f3ec566ee21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Wed, 28 Aug 2024 14:09:11 +0200 Subject: [PATCH 4/6] Simplify testing, to aboid changing the package code --- R/colors.R | 6 +----- R/package.R | 6 +----- tests/testthat/test-colors.R | 16 ++++------------ 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/R/colors.R b/R/colors.R index c8a09a2..99c5b03 100644 --- a/R/colors.R +++ b/R/colors.R @@ -15,11 +15,7 @@ initialize_colors <- function(debug_pkgs) { names = debug_pkgs ) - assign_debug("palette", palette) -} - -assign_debug <- function(x, value) { - assign(x, value, envir = debug_data) + assign("palette", palette, envir = debug_data) } #' @importFrom crayon make_style diff --git a/R/package.R b/R/package.R index be63bbb..6169d36 100644 --- a/R/package.R +++ b/R/package.R @@ -100,7 +100,7 @@ debugme <- function(env = topenv(parent.frame()), refresh_pkg_info() - if (!is_debugged2(pkg)) return() + if (!is_debugged(pkg)) return() should_instrument <- function(x) { obj <- get(x, envir = env) @@ -115,13 +115,9 @@ debugme <- function(env = topenv(parent.frame()), ) } -# Used in get_package_style() is_debugged <- function(pkg) { pkg %in% names(debug_data$palette) } -# To allow targetted mocking in debugme() -is_debugged2 <- is_debugged - debug_data <- new.env() debug_data$timestamp <- NULL diff --git a/tests/testthat/test-colors.R b/tests/testthat/test-colors.R index 34dcd6e..6cefae3 100644 --- a/tests/testthat/test-colors.R +++ b/tests/testthat/test-colors.R @@ -1,19 +1,11 @@ test_that("color palette is fine", { - - val <- NULL - - local_mocked_bindings( - assign_debug = function(x, value) { - val <<- value - } - ) initialize_colors(c("foo", "bar")) - expect_equal(names(val), c("foo", "bar")) - expect_true(all(val %in% grDevices::colors())) + expect_equal(names(debug_data[["palette"]]), c("foo", "bar")) + expect_true(all(debug_data[["palette"]] %in% grDevices::colors())) initialize_colors(letters) - expect_equal(names(val), letters) - expect_true(all(val %in% c("silver", grDevices::colors()))) + expect_equal(names(debug_data[["palette"]]), letters) + expect_true(all(debug_data[["palette"]] %in% c("silver", grDevices::colors()))) }) ## Quite an artificial test case... From 03a2019d9ff8dc2e367652a5ec2a43f506995176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Wed, 28 Aug 2024 14:23:03 +0200 Subject: [PATCH 5/6] Avoid whitespace changes --- tests/testthat/test-colors.R | 1 + tests/testthat/test-debug.R | 26 +++++++++++++++++++------- tests/testthat/test-package.R | 11 +++++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/tests/testthat/test-colors.R b/tests/testthat/test-colors.R index 6cefae3..090f575 100644 --- a/tests/testthat/test-colors.R +++ b/tests/testthat/test-colors.R @@ -11,6 +11,7 @@ test_that("color palette is fine", { ## Quite an artificial test case... test_that("get a package style", { + local_mocked_bindings( is_debugged = function(...) TRUE, make_style = function(x) substitute(x) diff --git a/tests/testthat/test-debug.R b/tests/testthat/test-debug.R index 1da462a..0524daa 100644 --- a/tests/testthat/test-debug.R +++ b/tests/testthat/test-debug.R @@ -1,17 +1,20 @@ test_that("debug indent", { f1 <- function() { - debug("f1") - f2() + debug("f1") + f2() } + f2 <- function() { - debug("f2.1") - f3() - debug("f2.2") + debug("f2.1") + f3() + debug("f2.2") } f3 <- function() { - debug("f3") + debug("f3") } + out <- capture_output(eval({ debug("f0.1"); f1(); f2(); debug("f0.2")})) + expect_match(out, 'debugme f0.1', fixed = TRUE) expect_match(out, 'debugme +-f1', fixed = TRUE) expect_match(out, 'debugme +-f2.1', fixed = TRUE) @@ -20,11 +23,12 @@ test_that("debug indent", { expect_match(out, 'debugme +-f2.1', fixed = TRUE) expect_match(out, 'debugme -f2.2', fixed = TRUE) expect_match(out, 'debugme f0.2', fixed = TRUE) - + out <- withr::with_envvar( c(DEBUGME_SHOW_STACK = "no"), capture_output(eval({ debug("f0.1"); f1(); f2(); debug("f0.2")})) ) + expect_match(out, 'debugme f0.1', fixed = TRUE) expect_match(out, 'debugme f1', fixed = TRUE) expect_match(out, 'debugme f2.1', fixed = TRUE) @@ -36,6 +40,7 @@ test_that("debug indent", { }) test_that("debug levels", { + local_mocked_bindings( get_package_debug_level = function(...) 1 ) @@ -62,6 +67,7 @@ test_that("format_date", { }) test_that("get_timestamp_stamp", { + mytime <- structure(1477967634, class = c("POSIXct", "POSIXt"), tzone = "UTC") local_mocked_bindings(Sys.time = function() mytime, .package = "base") @@ -74,13 +80,16 @@ test_that("get_timestamp_stamp", { test_that("debugging to a file", { tmp <- tempfile() on.exit(unlink(tmp), add = TRUE) + on.exit(initialize_output_file(), add = TRUE) on.exit(try(close(debug_data$output_fd), silent = TRUE), add = TRUE) + withr::with_envvar(c(DEBUGME_OUTPUT_FILE = tmp), { initialize_output_file() }) debug("hello world!", "foobar") debug("hello again!", "foo") + log <- readLines(tmp) expect_match(log[1], "^foobar hello world!") expect_match(log[2], "^foo hello again!") @@ -90,13 +99,16 @@ test_that("debugging to a directory", { tmp <- tempfile() dir.create(tmp) on.exit(unlink(tmp, recursive = TRUE), add = TRUE) + on.exit(initialize_output_file(), add = TRUE) on.exit(try(close(debug_data$output_fd), silent = TRUE), add = TRUE) + withr::with_envvar(c(DEBUGME_OUTPUT_DIR = tmp), { initialize_output_file() }) debug("hello world!", "foobar") debug("hello again!", "foo") + log <- readLines(file.path(tmp, paste0("debugme-", Sys.getpid(), ".log"))) expect_match(log[1], "^foobar hello world!") expect_match(log[2], "^foo hello again!") diff --git a/tests/testthat/test-package.R b/tests/testthat/test-package.R index f89a5a9..064daa1 100644 --- a/tests/testthat/test-package.R +++ b/tests/testthat/test-package.R @@ -1,5 +1,7 @@ test_that(".onLoad", { + val <- NULL + local_mocked_bindings( initialize_colors = function(pkgs) val <<- pkgs ) @@ -11,14 +13,18 @@ test_that(".onLoad", { }) test_that("debugme", { + env <- new.env() env$f1 <- function() { "nothing here" } env$f2 <- function() { "!DEBUG foobar" } env$notme <- "!DEBUG nonono" env$.hidden <- function() { "!DEBUG foobar2" } + expect_silent(debugme(env)) + local_mocked_bindings(is_debugged2 = function(...) TRUE) debugme(env) + expect_silent(env$f1()) expect_output(env$f2(), "debugme foobar \\+[0-9]+ms") expect_identical(env$notme, "!DEBUG nonono") @@ -26,11 +32,14 @@ test_that("debugme", { }) test_that("instrument environments", { + env <- new.env() env$env <- new.env() env$env$fun <- function() { "!DEBUG coocoo" } + local_mocked_bindings(is_debugged2 = function(...) TRUE) expect_silent(debugme(env)) + expect_output(env$env$fun(), "coocoo") }) @@ -52,8 +61,10 @@ test_that("instrument R6 classes", { name = NULL ) ) + local_mocked_bindings(is_debugged2 = function(...) TRUE) expect_silent(debugme(env)) + expect_output(x <- env$class$new("mrx"), "debugme.*creating mrx") expect_output(x$hello(), "debugme.*hello mrx") }) From 6713d401d3036002fd9f4777e5a1f7daad152f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Wed, 28 Aug 2024 15:16:40 +0200 Subject: [PATCH 6/6] Simpligy tests --- tests/testthat/test-package.R | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/tests/testthat/test-package.R b/tests/testthat/test-package.R index 064daa1..4b3008b 100644 --- a/tests/testthat/test-package.R +++ b/tests/testthat/test-package.R @@ -1,15 +1,9 @@ test_that(".onLoad", { - - val <- NULL - - local_mocked_bindings( - initialize_colors = function(pkgs) val <<- pkgs - ) withr::with_envvar( c("DEBUGME" = c("foo,bar")), refresh_pkg_info() ) - expect_identical(val, c("foo", "bar")) + expect_identical(names(debug_data$palette), c("foo", "bar")) }) test_that("debugme", { @@ -20,13 +14,12 @@ test_that("debugme", { env$notme <- "!DEBUG nonono" env$.hidden <- function() { "!DEBUG foobar2" } - expect_silent(debugme(env)) - - local_mocked_bindings(is_debugged2 = function(...) TRUE) - debugme(env) + withr::local_envvar(DEBUGME = "debugme") + refresh_pkg_info() + debugme(env, pkg = "debugme") expect_silent(env$f1()) - expect_output(env$f2(), "debugme foobar \\+[0-9]+ms") + expect_output(env$f2(), "debugme foobar ") expect_identical(env$notme, "!DEBUG nonono") expect_output(env$.hidden(), "debugme foobar2 \\+[0-9]+ms") }) @@ -37,8 +30,9 @@ test_that("instrument environments", { env$env <- new.env() env$env$fun <- function() { "!DEBUG coocoo" } - local_mocked_bindings(is_debugged2 = function(...) TRUE) - expect_silent(debugme(env)) + withr::local_envvar(DEBUGME = "debugme") + refresh_pkg_info() + debugme(env, pkg = "debugme") expect_output(env$env$fun(), "coocoo") }) @@ -62,8 +56,9 @@ test_that("instrument R6 classes", { ) ) - local_mocked_bindings(is_debugged2 = function(...) TRUE) - expect_silent(debugme(env)) + withr::local_envvar(DEBUGME = "debugme") + refresh_pkg_info() + debugme(env, pkg = "debugme") expect_output(x <- env$class$new("mrx"), "debugme.*creating mrx") expect_output(x$hello(), "debugme.*hello mrx")