diff --git a/DESCRIPTION b/DESCRIPTION index 63741e3..4319269 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -5,7 +5,7 @@ Description: Director is responsible for managing and loading resources in consecutive loads of resources (so that we can tell if a script was modified since we last ran it) and defining parsers that allow us to generalize from the pernicious simple linear execution that is common to R. -Version: 0.2.1 +Version: 0.2.3 Authors@R: c(person("Robert", "Krzyzanowski", email = "technoguyrob@gmail.com", role = c("aut", "cre"))) Depends: diff --git a/R/find.R b/R/find.R index 6c65c32..9ff9a5a 100644 --- a/R/find.R +++ b/R/find.R @@ -93,6 +93,7 @@ director_find <- function(search = '', method = 'wildcard', base = '', by_mtime # this separation is necessary to prevent things like looking for "2.1.2" # catching "model/2.1.1/2.1.1", which would be wrong. if (identical(method, 'exact')) { + all_files <- unname(c(all_files, idempotent_objects)) return(file.path(base, Find(function(x) x == search, all_files) %||% character(0))) } else if (!identical(search, '')) { pattern <- strip_r_extension(search) # Strip file extension diff --git a/R/resource.R b/R/resource.R index 1335fc6..7434650 100644 --- a/R/resource.R +++ b/R/resource.R @@ -58,12 +58,14 @@ resource <- function(name, provides = list(), body = TRUE, soft = FALSE, ..., tracking = TRUE, check.helpers = TRUE) { + defining_environment <- globalenv() + if (!is.environment(provides)) { provides <- - if (length(provides) == 0) new.env(parent = parent.frame()) + if (length(provides) == 0) new.env(parent = defining_environment) else { env <- as.environment(provides) - parent.env(env) <- parent.frame() + parent.env(env) <- defining_environment env } @@ -87,8 +89,8 @@ resource <- function(name, provides = list(), body = TRUE, soft = FALSE, ..., # If this resource does not exist, let the preprocessor handle it instead. return(directorResource(current = NULL, cached = NULL, modified = TRUE, resource_key = name, - source_args = list(local = new.env(parent = parent.frame())), director = .self, - defining_environment = parent.frame())) + source_args = list(local = new.env(parent = defining_environment)), director = .self, + defining_environment = defining_environment)) } filename <- .filename(name, FALSE, FALSE, !isTRUE(check.helpers)) # Convert resource to filename. @@ -125,7 +127,7 @@ resource <- function(name, provides = list(), body = TRUE, soft = FALSE, ..., for (file in helper_files) { helper <- resource(file.path(resource_key, file), body = FALSE, tracking = FALSE, check.helpers = FALSE, - defining_environment = parent.frame()) + defining_environment = defining_environment) if (tracking_is_on_and_resource_has_helpers) modified <- modified || helper$modified } @@ -135,7 +137,7 @@ resource <- function(name, provides = list(), body = TRUE, soft = FALSE, ..., output <- directorResource(current = current_details, cached = cached_details, modified = modified, resource_key = resource_key, source_args = source_args, director = .self, - defining_environment = parent.frame()) + defining_environment = defining_environment) if (.dependency_nesting_level > 0 && isTRUE(check.helpers)) .stack$push(list(level = .dependency_nesting_level, diff --git a/inst/tests/test-director_find.R b/inst/tests/test-director_find.R index 54889f9..5615773 100644 --- a/inst/tests/test-director_find.R +++ b/inst/tests/test-director_find.R @@ -70,3 +70,17 @@ test_that('it correctly uses a base to look for a wildcard match', { }) }) +test_that("it correctly does an exact match on idempotent resources", { + within_file_structure(list(uno = list(dos = list('dos.R'))), { d <- director(tempdir) + expect_identical('/uno/dos', d$find("uno/dos", method = "exact"), + info = "Exact matching shoudl find uno/dos as an idempotent resource.") + }) +}) + +test_that("it correctly does an exact match on idempotent resources with base", { + within_file_structure(list(uno = list(dos = list('dos.R'))), { d <- director(tempdir) + expect_identical('uno/dos', d$find("dos", base = "uno", method = "exact"), + info = "Exact matching shoudl find uno/dos as an idempotent resource.") + }) +}) + diff --git a/inst/tests/test-director_resource.R b/inst/tests/test-director_resource.R index 0939c2d..005b560 100644 --- a/inst/tests/test-director_resource.R +++ b/inst/tests/test-director_resource.R @@ -186,3 +186,14 @@ test_that('a sourced resource can pass args', { }) }) +test_that("it does not allow access to another resource file's locals", { + within_file_structure(list(one.R = "one <- 1; browser(); resource('two')$value()", two.R = "2"), { + d <- director(tempdir) + d$register_parser("one", function(source_args, source) { + source_args$local$resource <- d$resource + }) + browser() + expect_error(d$resource("one")$value()) + }) +}) +