Skip to content

Commit

Permalink
clean filename in cpp_source()
Browse files Browse the repository at this point in the history
closes r-lib#248
  • Loading branch information
romainfrancois committed Dec 2, 2021
1 parent ca8bd28 commit f5c7ae1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions R/source.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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 @@ -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.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)
})

0 comments on commit f5c7ae1

Please sign in to comment.