Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions R/source.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ the$count <- 0L
generate_cpp_name <- function(name, loaded_dlls = c("cpp11", names(getLoadedDLLs()))) {
ext <- tools::file_ext(name)
root <- tools::file_path_sans_ext(basename(name))
root <- gsub("[^[:alnum:]_]", "_", root)
count <- 2
new_name <- root
while(new_name %in% loaded_dlls) {
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-source.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ test_that("cpp_source(d) functions work after sourcing file more than once", {
expect_equal(foo(), 1)
})

test_that("cpp_source() cleans file name (#248)", {
code <- "
#include <cpp11.hpp>

[[cpp11::register]]
int some_function() {return 42; }
"
temp1 <- withr::local_tempfile(fileext = "contains-hyphen.cpp")
write(code, temp1)
expect_error(cpp11::cpp_source(temp1), NA)
expect_equal(some_function(), 42L)

temp2 <- withr::local_tempfile(fileext = "contains+plus.cpp")
write(code, temp2)
expect_error(cpp11::cpp_source(temp2), NA)
expect_equal(some_function(), 42L)
})

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))
Expand Down