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

library calls possible within a call #157

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions R/qenv-eval_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
tryCatch(
{
eval(single_call, envir = object@env)
if (!identical(parent.env(object@env), parent.env(.GlobalEnv))) {
# needed to make sure that @env is always a sibling of .GlobalEnv
# could be changed when any new package is added to search path (through library or require call)
parent.env(object@env) <- parent.env(.GlobalEnv)
}
NULL
},
error = function(e) {
Expand Down
14 changes: 3 additions & 11 deletions tests/testthat/test-qenv_eval_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,16 @@ testthat::test_that("@env in qenv is always a sibling of .GlobalEnv", {
testthat::expect_identical(parent.env(q3@env), parent.env(.GlobalEnv))
})

testthat::test_that("library have to be called separately before using function from package", {
q1 <- eval_code(new_qenv(), quote(library(checkmate)))
# library call adds package env in the search path just over .GlobalEnv
# it means that q3@env before the call was a parent of .GlobalEnv but not after the call

q2 <- eval_code(q1, quote(assert_number(1)))
testthat::expect_identical(parent.env(q2@env), parent.env(.GlobalEnv))

detach("package:checkmate", unload = TRUE)
testthat::expect_s3_class(
testthat::test_that("getting object from the package namespace works even if library in the same call", {
testthat::expect_s4_class(
eval_code(
new_qenv(),
as.expression(c(
quote(library(checkmate)),
quote(assert_number(1))
))
),
"qenv.error"
"qenv"
)
})

Expand Down