Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove mockery #59

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
^codecov\.yml$
^LICENSE\.md$
^screencast.gif$
^debugme\.Rproj$
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Imports:
grDevices
Suggests:
covr,
mockery,
R6,
testthat (>= 3.0.0),
withr
Expand Down
6 changes: 5 additions & 1 deletion R/colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion R/debug.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ get_timestamp_diff <- function() {
}

get_timestamp_stamp <- function() {
paste0(format_date(Sys.time()), " ")
paste0(format_date(systime()), " ")
}

systime <- function() {
jonthegeek marked this conversation as resolved.
Show resolved Hide resolved
Sys.time()
}

format_date <- function(date) {
Expand Down
5 changes: 4 additions & 1 deletion R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -118,6 +118,9 @@ debugme <- function(env = topenv(parent.frame()),
is_debugged <- function(pkg) {
pkg %in% names(debug_data$palette)
}
# To allow targetted mocking.
gaborcsardi marked this conversation as resolved.
Show resolved Hide resolved
is_debugged2 <- is_debugged


debug_data <- new.env()
debug_data$timestamp <- NULL
Expand Down
18 changes: 18 additions & 0 deletions debugme.Rproj
Original file line number Diff line number Diff line change
@@ -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
14 changes: 9 additions & 5 deletions tests/testthat/test-colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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]))
})
43 changes: 22 additions & 21 deletions tests/testthat/test-debug.R
Original file line number Diff line number Diff line change
@@ -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")
}

jonthegeek marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 "
Expand All @@ -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!")
Expand All @@ -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!")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-levels.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/test-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())
Expand All @@ -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")
Expand All @@ -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")
Expand Down
Loading