Skip to content

Commit

Permalink
Fail fast and informatively for nonexistent file (#261)
Browse files Browse the repository at this point in the history
* Initiate a snapshot test (non-informative failure)

* Fail early with a better message

* Explicitly check if `file` was provided

* Reveal the filepath that we can't find

* NEWS

Co-authored-by: Romain Francois <[email protected]>
  • Loading branch information
jennybc and romainfrancois authored Feb 7, 2022
1 parent 3341a58 commit 322e5ee
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# cpp11 (development version)

* `cpp_source()` errors on non-existent file (#261).

# cpp11 0.4.2

* Romain François is now the maintainer.
Expand Down
3 changes: 3 additions & 0 deletions R/source.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
#' @export
cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), dir = tempfile()) {
stop_unless_installed(c("brio", "callr", "cli", "decor", "desc", "glue", "tibble", "vctrs"))
if (!missing(file) && !file.exists(file)) {
stop("Can't find `file` at this path:\n", file, "\n", call. = FALSE)
}

dir.create(dir, showWarnings = FALSE, recursive = TRUE)
dir.create(file.path(dir, "R"), showWarnings = FALSE)
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# cpp_source fails informatively for nonexistent file

Code
cpp_source(i_do_not_exist)
Error <simpleError>
Can't find `file` at this path:
{NON_EXISTENT_FILEPATH}

10 changes: 10 additions & 0 deletions tests/testthat/test-source.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,13 @@ test_that("cpp_source(d) functions work after sourcing file more than once", {
cpp11::cpp_source(test_path("single.cpp"), clean = TRUE)
expect_equal(foo(), 1)
})

test_that("cpp_source fails informatively for nonexistent file", {
i_do_not_exist <- tempfile(pattern = "nope-", fileext = ".cpp")
expect_false(file.exists(i_do_not_exist))
expect_snapshot(
error = TRUE,
cpp_source(i_do_not_exist),
transform = ~ sub("^.+[.]cpp$", "{NON_EXISTENT_FILEPATH}", .x)
)
})

0 comments on commit 322e5ee

Please sign in to comment.