Skip to content

Commit

Permalink
always uninject before clear_traces
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Jun 23, 2023
1 parent 74a9f4b commit 30cf7c6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
18 changes: 9 additions & 9 deletions R/trace-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ trace_package <- function (package = NULL,
if (is.null (trace_fns)) {
trace_fns <- ls (p, all.names = TRUE)
}

clear_traces ()

pkg_env <- as.environment (p)
for (fnm in trace_fns) {
f <- get (fnm, envir = pkg_env)
Expand All @@ -67,8 +70,6 @@ trace_package <- function (package = NULL,
}
}

clear_traces ()

traces_ex <- NULL

if ("examples" %in% types) {
Expand Down Expand Up @@ -115,19 +116,18 @@ trace_package <- function (package = NULL,
traces$trace_name <- traces$trace_source <- NULL
}

# Envvar to enable traces to remain so that package can be used by
# 'autotest', through loading traces after calling 'trace_package()'
if (!Sys.getenv ("TYPETRACER_LEAVE_TRACES") == "true") {
clear_traces ()
}

for (f in trace_fns) {
f <- get (f, envir = pkg_env)
if (is.function (f)) {
uninject_tracer (f)
}
}
clear_fn_bodies_dir ()

# Envvar to enable traces to remain so that package can be used by
# 'autotest', through loading traces after calling 'trace_package()'
if (!Sys.getenv ("TYPETRACER_LEAVE_TRACES") == "true") {
clear_traces ()
}

tryCatch (
unloadNamespace (package),
Expand Down
13 changes: 8 additions & 5 deletions tests/testthat/test-trace-fns.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test_that ("injected tracer body", {
expect_true (length (body1) > length (body0))

expect_equal (body1 [[2]], body (typetracer_header))
expect_true (uninject_tracer (f))
})

test_that ("No traces", {
Expand All @@ -43,9 +44,9 @@ test_that ("trace call", {
x * x + y * y
}

clear_traces ()
inject_tracer (f)

clear_traces ()
val <- f (x = 1:2, y = 3:4 + 0.)
flist <- list.files (tempdir (),
pattern = "^typetrace\\_",
Expand All @@ -54,6 +55,7 @@ test_that ("trace call", {
expect_true (length (flist) > 0L)

x <- load_traces (files = TRUE)
expect_true (uninject_tracer (f))

expect_s3_class (x, "tbl_df")
expect_equal (nrow (x), 2L) # x and y
Expand Down Expand Up @@ -89,6 +91,7 @@ test_that ("untrace call", {
expect_false (identical (e0, e1))

expect_identical (e0, e2)
expect_false (uninject_tracer (f))
})

test_that ("trace lists", {
Expand All @@ -99,17 +102,17 @@ test_that ("trace lists", {
x * x + y * y + a$x
}

inject_tracer (f, trace_lists = FALSE)
clear_traces ()
inject_tracer (f, trace_lists = FALSE)
val <- f (x = 1:2, y = 3:4 + 0., a = list (x = 4))
x0 <- load_traces ()
uninject_tracer (f)
expect_true (uninject_tracer (f))

inject_tracer (f, trace_lists = TRUE)
clear_traces ()
inject_tracer (f, trace_lists = TRUE)
val <- f (x = 1:2, y = 3:4 + 0., a = list (x = 4))
x1 <- load_traces ()
uninject_tracer (f)
expect_true (uninject_tracer (f))

expect_true (nrow (x1) > nrow (x0))
expect_false (any (grepl ("\\$", x0$par_name)))
Expand Down

0 comments on commit 30cf7c6

Please sign in to comment.