Skip to content

Commit

Permalink
Fix #225: Infer unitizer location when not at pkg top level
Browse files Browse the repository at this point in the history
  • Loading branch information
brodieG committed Nov 23, 2017
2 parents aa1a6e6 + cc392dc commit 3c0de8b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -3,6 +3,8 @@
## v1.4.5

* Colors in test browser working again.
* [#225](https://github.com/brodieG/unitizer/issues/225) Inferring file
locations when not in pkg top level.
* [#237](https://github.com/brodieG/unitizer/issues/237) Option to turn off
diffs.
* [#239](https://github.com/brodieG/unitizer/issues/239) Document issues with
Expand Down
11 changes: 7 additions & 4 deletions R/faux_prompt.R
Expand Up @@ -10,16 +10,19 @@ faux_prompt <- function(
) {
res <- character()
repeat {
res <- paste0(res, read_line(prompt), if(length(res)) '\n' else "")
res.parse <- tryCatch(
parsed <- parse(text=res),
res.parse <- tryCatch({
res <- paste0(res, read_line(prompt), if(length(res)) '\n' else "")
parsed <- parse(text=res)
},
error=function(e) {
if(!isTRUE(grepl(" unexpected end of input\n", conditionMessage(e)))) {
stop(simpleError(conditionMessage(e), res))
e$call <- if(length(res)) res else NULL
stop(e)
}
} )
prompt <- `continue`
if(is.expression(res.parse)) {
return(res.parse)
} }
NULL
}
11 changes: 11 additions & 0 deletions R/get.R
Expand Up @@ -309,6 +309,17 @@ infer_unitizer_location.character <- function(
if(!(identical(type, "d") && at.package.dir) && test.fun(store.id))
return(store.id)

# If we're not at the package directory, check to see if we are in a package
# directory and change directory to that, but only do that if we're not
# already matching an actual directory

if(
!at.package.dir && !is.null(file.store.id) &&
length(pkg.dir.tmp <- get_package_dir(dir.store.id))
) {
at.package.dir <- TRUE
dir.store.id <- pkg.dir.tmp
}
if(at.package.dir) {
test.base <- file.path(dir.store.id, "tests", "unitizer")
if(!file_test("-d", test.base))
Expand Down
7 changes: 6 additions & 1 deletion R/prompt.R
Expand Up @@ -113,7 +113,12 @@ unitizer_prompt <- function(
prompt.txt <- sprintf("%s> ", "unitizer")

old.opt <- options(warn=1)
val <- tryCatch(faux_prompt(prompt.txt), simpleError=function(e) e)
on.exit(options(old.opt))
val <- tryCatch(
faux_prompt(prompt.txt),
simpleError=function(e) e
)
on.exit(NULL)
options(old.opt)

if(inherits(val, "simpleError")) {
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/testthat.get.R
Expand Up @@ -385,6 +385,11 @@ local({
infer(file.path(base.dir2, "z"), type="u"),
"tests/unitizer/zzz\\.unitizer$"
)
# Random file without setting working dir first

f.test2 <- infer('tests2')
expect_match(f.test2, "unitizer/tests2.R$")

# Interactive mode

unitizer:::read_line_set_vals(c("26", "Q"))
Expand Down
13 changes: 12 additions & 1 deletion tests/testthat/testthat.misc.R
Expand Up @@ -285,7 +285,7 @@ test_that("relativize_path", {
c(
as.list(
rep(
"..",
"..",
length(
unlist(strsplit(getwd(), .Platform$file.sep, fixed=TRUE))
) - 1L
Expand Down Expand Up @@ -381,3 +381,14 @@ test_that("diff conditionList", {
) )
expect_is(diffobj::diffObj(cond1, cond1), "Diff")
})

test_that("Condition object structure", {
# We're assuming a particular structure for the condition object in
# `faux_prompt` and `unitizer_prompt` so we put in a test here to make sure it
# doesn't change

cond <- simpleError('hello')
expect_true(is.list(cond))
expect_true(identical(names(cond), c('message', 'call')))
expect_true(identical(class(cond), c('simpleError', 'error', 'condition')))
})

0 comments on commit 3c0de8b

Please sign in to comment.