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")