From f5c7ae1cbf92d6bfd5b67382c2d0de0901fe07e5 Mon Sep 17 00:00:00 2001 From: Romain Francois Date: Thu, 2 Dec 2021 11:34:43 +0100 Subject: [PATCH] clean filename in cpp_source() closes #248 --- R/source.R | 1 + tests/testthat/test-source.R | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/R/source.R b/R/source.R index 0632228d..b795f076 100644 --- a/R/source.R +++ b/R/source.R @@ -153,6 +153,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 8f813d0e..f06305bc 100644 --- a/tests/testthat/test-source.R +++ b/tests/testthat/test-source.R @@ -217,3 +217,21 @@ 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() 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) +})