Skip to content

Commit 841b343

Browse files
authored
fix: update extract meta to work with new default feature in upstream (#483)
* update extract meta to work with new default feature in upstream * fix lint and add sccache to gha * reverst sccache changes
1 parent f9f0c8e commit 841b343

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ jobs:
4444
toolchain: ${{ matrix.config.rust-version }}
4545
targets: ${{ matrix.config.rust-target }}
4646

47+
# Add rust-cache for faster builds
48+
- name: Cache Rust dependencies
49+
uses: Swatinem/rust-cache@v2
50+
with:
51+
shared-key: ${{ matrix.config.os }}-${{ matrix.config.rust-version }}
52+
cache-on-failure: true
53+
4754
- uses: baptiste0928/cargo-install@v3
4855
if: matrix.config.r == 'release'
4956
with:

R/find_exports.R

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ find_exports <- function(clean_lns) {
55

66
# start and end may empty
77
if (rlang::is_empty(start) || rlang::is_empty(end)) {
8-
return(data.frame(name = character(0), type = character(0), lifetime = character(0)))
8+
return(data.frame(
9+
name = character(0),
10+
type = character(0),
11+
lifetime = character(0)
12+
))
913
}
1014

1115
map2(start, end, \(.x, .y) extract_meta(clean_lns[.x:.y])) |>
@@ -16,8 +20,9 @@ find_exports <- function(clean_lns) {
1620
}
1721

1822
# Finds lines which contain #[extendr] (allowing additional spaces)
23+
# Excludes #[extendr(default=...)] which is used for function arguments
1924
find_extendr_attrs_ids <- function(lns) {
20-
which(stringi::stri_detect_regex(lns, r"{#\s*\[\s*extendr(\s*\(.*\))?\s*\]}"))
25+
which(stringi::stri_detect_regex(lns, r"{#\s*\[\s*extendr(\s*\((?!.*default\s*=).*\))?\s*\]}"))
2126
}
2227

2328
# Gets function/module metadata from a subset of lines.
@@ -29,7 +34,15 @@ extract_meta <- function(lns) {
2934
"(?:(?<struct>struct)|(?<enum>enum)|(?<fn>fn)|(?<impl>impl)(?:\\s*<(?<lifetime>.+?)>)?)\\s+(?<name>(?:r#)?(?:_\\w+|[A-z]\\w*))" # nolint: line_length_linter
3035
) |>
3136
as.data.frame() |>
32-
rlang::set_names(c("match", "struct", "enum", "fn", "impl", "lifetime", "name")) |>
37+
rlang::set_names(c(
38+
"match",
39+
"struct",
40+
"enum",
41+
"fn",
42+
"impl",
43+
"lifetime",
44+
"name"
45+
)) |>
3346
dplyr::filter(!is.na(.data$match))
3447

3548
# If no matches have been found, then the attribute is misplaced
@@ -38,10 +51,10 @@ extract_meta <- function(lns) {
3851
# meaningful output or source line numbers.
3952
code_sample <- stringi::stri_sub(
4053
glue_collapse(lns, sep = "\n "),
41-
1, 80
54+
1,
55+
80
4256
)
4357

44-
4558
rlang::abort(
4659
cli::cli_fmt({
4760
cli::cli_text(

tests/data/test-default-arg.Rmd

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
```{r, include = FALSE}
3+
library(rextendr)
4+
```
5+
6+
7+
```{extendrsrc engine.opts = list(use_dev_extendr=TRUE)}
8+
#[extendr]
9+
fn check_default(#[extendr(default = "NULL")] x: Robj) -> bool {
10+
x.is_null()
11+
}
12+
```
13+
```{r}
14+
testthat::expect_true(check_default())
15+
```

tests/testthat/test-knitr-engine.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,15 @@ test_that("Snapshot test of knitr-engine", {
3434
knitr::knit(input, output)
3535
expect_snapshot(cat_file(output))
3636
})
37+
38+
39+
test_that("knitr-engine for #[extendr(default = )]", {
40+
skip_if_cargo_unavailable()
41+
skip_if_not_installed("knitr")
42+
skip_on_cran()
43+
44+
input <- file.path("../data/test-default-arg.Rmd")
45+
output <- withr::local_file("snapshot-extendr-default.md")
46+
knitr::knit(input, output)
47+
expect_true(check_default())
48+
})

0 commit comments

Comments
 (0)