diff --git a/NEWS.md b/NEWS.md index 29380a50..9d30e2b8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/faux_prompt.R b/R/faux_prompt.R index 77553f7f..e1e6476b 100644 --- a/R/faux_prompt.R +++ b/R/faux_prompt.R @@ -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 } diff --git a/R/get.R b/R/get.R index 5cfe5914..140dec4a 100644 --- a/R/get.R +++ b/R/get.R @@ -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)) diff --git a/R/prompt.R b/R/prompt.R index d393d7f4..21fe5dbc 100644 --- a/R/prompt.R +++ b/R/prompt.R @@ -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")) { diff --git a/tests/testthat/testthat.get.R b/tests/testthat/testthat.get.R index 278fa236..43c716dc 100644 --- a/tests/testthat/testthat.get.R +++ b/tests/testthat/testthat.get.R @@ -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")) diff --git a/tests/testthat/testthat.misc.R b/tests/testthat/testthat.misc.R index 51491617..d6eb2108 100644 --- a/tests/testthat/testthat.misc.R +++ b/tests/testthat/testthat.misc.R @@ -285,7 +285,7 @@ test_that("relativize_path", { c( as.list( rep( - "..", + "..", length( unlist(strsplit(getwd(), .Platform$file.sep, fixed=TRUE)) ) - 1L @@ -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'))) +})