diff --git a/R/source.R b/R/source.R index fc9124d5..6176c7f2 100644 --- a/R/source.R +++ b/R/source.R @@ -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) { diff --git a/tests/testthat/test-source.R b/tests/testthat/test-source.R index 2945d51d..c8c60b2b 100644 --- a/tests/testthat/test-source.R +++ b/tests/testthat/test-source.R @@ -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::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))