Skip to content

Commit

Permalink
Merge 34bc381 into 4c49974
Browse files Browse the repository at this point in the history
  • Loading branch information
robertzk committed Feb 26, 2016
2 parents 4c49974 + 34bc381 commit 72901ec
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions R/find.R
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions R/resource.R
Expand Up @@ -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
}

Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions inst/tests/test-director_find.R
Expand Up @@ -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.")
})
})

11 changes: 11 additions & 0 deletions inst/tests/test-director_resource.R
Expand Up @@ -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())
})
})

0 comments on commit 72901ec

Please sign in to comment.