Skip to content

Commit 322e5ee

Browse files
Fail fast and informatively for nonexistent file (#261)
* 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 <romain@rstudio.com>
1 parent 3341a58 commit 322e5ee

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# cpp11 (development version)
22

3+
* `cpp_source()` errors on non-existent file (#261).
4+
35
# cpp11 0.4.2
46

57
* Romain François is now the maintainer.

R/source.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
#' @export
6868
cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), dir = tempfile()) {
6969
stop_unless_installed(c("brio", "callr", "cli", "decor", "desc", "glue", "tibble", "vctrs"))
70+
if (!missing(file) && !file.exists(file)) {
71+
stop("Can't find `file` at this path:\n", file, "\n", call. = FALSE)
72+
}
7073

7174
dir.create(dir, showWarnings = FALSE, recursive = TRUE)
7275
dir.create(file.path(dir, "R"), showWarnings = FALSE)

tests/testthat/_snaps/source.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# cpp_source fails informatively for nonexistent file
2+
3+
Code
4+
cpp_source(i_do_not_exist)
5+
Error <simpleError>
6+
Can't find `file` at this path:
7+
{NON_EXISTENT_FILEPATH}
8+

tests/testthat/test-source.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,13 @@ test_that("cpp_source(d) functions work after sourcing file more than once", {
217217
cpp11::cpp_source(test_path("single.cpp"), clean = TRUE)
218218
expect_equal(foo(), 1)
219219
})
220+
221+
test_that("cpp_source fails informatively for nonexistent file", {
222+
i_do_not_exist <- tempfile(pattern = "nope-", fileext = ".cpp")
223+
expect_false(file.exists(i_do_not_exist))
224+
expect_snapshot(
225+
error = TRUE,
226+
cpp_source(i_do_not_exist),
227+
transform = ~ sub("^.+[.]cpp$", "{NON_EXISTENT_FILEPATH}", .x)
228+
)
229+
})

0 commit comments

Comments
 (0)