Skip to content

Commit

Permalink
Merge branch 'main' into additive-rustflags
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahParry committed Nov 16, 2024
2 parents 95a2b61 + d58284b commit 387d4ff
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 39 deletions.
32 changes: 19 additions & 13 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
types:
- opened
- reopened
- synchronize
- ready_for_review

name: test-coverage
name: test-coverage.yaml

permissions: read-all

jobs:
test-coverage:
Expand All @@ -21,36 +17,46 @@ jobs:
REXTENDR_SKIP_DEV_TESTS: TRUE # TODO: Remove this when extendr/libR-sys issue is resolved

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
extra-packages: any::covr, any::xml2
needs: coverage

- name: Test coverage
run: |
covr::codecov(
cov <- covr::package_coverage(
quiet = FALSE,
clean = FALSE,
install_path = file.path(Sys.getenv("RUNNER_TEMP"), "package")
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
)
covr::to_cobertura(cov)
shell: Rscript {0}

- uses: codecov/codecov-action@v4
with:
# Fail if error if not on PR, or if on PR and token is given
fail_ci_if_error: ${{ github.event_name != 'pull_request' || secrets.CODECOV_TOKEN }}
file: ./cobertura.xml
plugin: noop
disable_search: true
token: ${{ secrets.CODECOV_TOKEN }}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(document)
export(eng_extendr)
export(eng_extendrsrc)
export(make_module_macro)
export(read_cargo_metadata)
export(register_extendr)
export(rust_eval)
export(rust_function)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Fixed an issue in `rust_source()` family of functions that prevented usage of `r#` escape sequences in Rust function names (#374)
* `use_cran_defaults()` now checks the `SystemRequirements` field in the `DESCRIPTION` file for cargo and rustc. It will display installation instructions if either is missing or provide the minimum required version if the installed version is outdated.
* Added `use_msrv()` to aid in specifying the minimum supported rust version (MSRV) for an R package
* Added `read_cargo_metadata()` to retrieve Cargo metadata for packages and
workspaces. (#389)

# rextend 0.3.1

Expand Down
46 changes: 46 additions & 0 deletions R/read_cargo_metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' Retrieve metadata for packages and workspaces
#'
#' @param path character scalar, the R package directory
#'
#' @details
#' For more details, see
#' \href{https://doc.rust-lang.org/cargo/commands/cargo-metadata.html}{Cargo docs}
#' for `cargo-metadata`. See especially "JSON Format" to get a sense of what you
#' can expect to find in the returned list.
#'
#' @return `list`, including the following elements:
#' - "packages"
#' - "workspace_members"
#' - "workspace_default_members"
#' - "resolve"
#' - "target_directory"
#' - "version"
#' - "workspace_root"
#' - "metadata"
#'
#' @export
#'
#' @examples
#' \dontrun{
#' read_cargo_metadata()
#' }
#'
read_cargo_metadata <- function(path = ".") {
check_string(path, class = "rextendr_error")

root <- rprojroot::find_package_root_file(path = path)

rust_folder <- normalizePath(
file.path(root, "src", "rust"),
winslash = "/",
mustWork = FALSE
)

out <- processx::run(
"cargo",
args = c("metadata", "--format-version=1", "--no-deps"),
wd = rust_folder
)

jsonlite::fromJSON(out[["stdout"]])
}
38 changes: 22 additions & 16 deletions R/use_crate.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,33 @@ use_crate <- function(
check_bool(optional)
check_string(path)

if (!is.null(version) && !is.null(git)) {
cli::cli_abort(
"Cannot specify a git URL ('{git}') with a version ('{version}').",
class = "rextendr_error"
)
}

if (!is.null(version)) {
crate <- paste0(crate, "@", version)
}

# combine main options
cargo_add_opts <- list(
"--features" = paste0(features, collapse = " "),
"--git" = git,
"--optional" = tolower(as.character(optional))
)
if (!is.null(features)) {
features <- c(
"--features",
paste(crate, features, sep = "/", collapse = ",")
)
}

# clear empty options
cargo_add_opts <- purrr::discard(cargo_add_opts, rlang::is_empty)
if (!is.null(git)) {
git <- c("--git", git)
}

# combine option names and values into single strings
adtl_args <- unname(purrr::imap_chr(
cargo_add_opts,
function(x, i) {
paste(i, paste0(x, collapse = " "))
}
))
if (optional) {
optional <- "--optional"
} else {
optional <- NULL
}

# get rust directory in project folder
root <- rprojroot::find_package_root_file(path = path)
Expand All @@ -86,7 +92,7 @@ use_crate <- function(
# run the commmand
processx::run(
"cargo",
c("add", crate, adtl_args),
c("add", crate, features, git, optional),
echo_cmd = TRUE,
wd = rust_folder
)
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ cargo_command_available <- function(args = "--help") {
try_exec_cmd <- function(cmd, args = character()) {
result <- tryCatch(
processx::run(cmd, args, error_on_status = FALSE),
error = \(...) list(status = -1)
error = function(...) list(status = -1)
)
if (result[["status"]] != 0) {
NA_character_
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ knitr::opts_chunk$set(
[![rextendr status badge](https://extendr.r-universe.dev/badges/rextendr)](https://extendr.r-universe.dev/rextendr)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![R build status](https://github.com/extendr/rextendr/workflows/R-CMD-check/badge.svg)](https://github.com/extendr/rextendr/actions)
[![codecov](https://codecov.io/gh/extendr/rextendr/branch/main/graph/badge.svg?token=5H6ID0LAO7)](https://app.codecov.io/gh/extendr/rextendr)
[![Codecov test coverage](https://codecov.io/gh/extendr/rextendr/graph/badge.svg)](https://app.codecov.io/gh/extendr/rextendr)
<!-- badges: end -->

## Installation
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ reference:
- to_toml
- make_module_macro
- rust_sitrep
- read_cargo_metadata
16 changes: 8 additions & 8 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
comment: false

comment:
layout: "header, reach, files"
require_changes: true
behavior: "new" # Only post on new commits
coverage:
status:
project:
default:
target: auto
threshold: 1%
informational: true
target: 70%
threshold: 5%
patch:
default:
target: auto
threshold: 1%
informational: true
target: 70%
threshold: 5%
39 changes: 39 additions & 0 deletions man/read_cargo_metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions tests/testthat/test-read_cargo_metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
test_that("read_cargo_metadata() returns crate or workspace metadata", {
skip_if_not_installed("usethis")

path <- local_package("testpkg")

# capture setup messages
withr::local_options(usethis.quiet = FALSE)

use_extendr(path, quiet = TRUE)

out <- read_cargo_metadata(path)

expect_type(out, "list")

expect_equal(
out[["packages"]][["name"]],
"testpkg"
)

expect_equal(
out[["packages"]][["version"]],
"0.1.0"
)

expect_equal(
out[["packages"]][["dependencies"]][[1]][["name"]],
"extendr-api"
)

expect_equal(
out[["workspace_root"]],
normalizePath(
file.path(path, "src", "rust"),
winslash = "\\",
mustWork = FALSE
)
)
})
Loading

0 comments on commit 387d4ff

Please sign in to comment.