Skip to content

Commit

Permalink
appease codecov. modify docs. and refactor run_cargo
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahParry committed Nov 21, 2024
1 parent 9746349 commit f90145f
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 71 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
SystemRequirements: Rust 'cargo'; the crate 'libR-sys' must compile
without error
Config/rextendr/version: 0.3.1.9001
7 changes: 4 additions & 3 deletions R/clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ clean <- function(path = ".", echo = TRUE) {

if (!dir.exists(target_dir)) {
cli::cli_abort(
"Could not clean binaries.",
"Target directory not found at {.path target_dir}.",
c(
"Could not clean binaries.",
"Target directory not found at {.path target_dir}."
),
call = rlang::caller_call(),
class = "rextendr_error"
)
Expand All @@ -52,7 +54,6 @@ clean <- function(path = ".", echo = TRUE) {
run_cargo(
args,
wd = find_extendr_crate(path = path),
echo_cmd = echo,
echo = echo
)

Expand Down
7 changes: 1 addition & 6 deletions R/license_note.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#' recursive dependencies in Rust crate.
#'
#' @param path character scalar, the R package directory
#' @param echo logical scalar, whether to print cargo command and outputs to the
#' console (default is `TRUE`)
#' @param quiet logical scalar, whether to signal successful writing of
#' LICENSE.note (default is `FALSE`)
#' @param force logical scalar, whether to regenerate LICENSE.note if
Expand All @@ -21,11 +19,9 @@
#' }
write_license_note <- function(
path = ".",
echo = TRUE,
quiet = FALSE,
force = TRUE) {
check_string(path, class = "rextendr_error")
check_bool(echo, class = "rextendr_error")
check_bool(quiet, class = "rextendr_error")
check_bool(force, class = "rextendr_error")

Check warning on line 26 in R/license_note.R

View check run for this annotation

Codecov / codecov/patch

R/license_note.R#L24-L26

Added lines #L24 - L26 were not covered by tests

Expand All @@ -42,8 +38,7 @@ write_license_note <- function(
metadata <- run_cargo(
args,
wd = find_extendr_crate(path = path),
echo_cmd = echo,
echo = echo,
echo = FALSE,
parse_json = TRUE
)

Check warning on line 43 in R/license_note.R

View check run for this annotation

Codecov / codecov/patch

R/license_note.R#L38-L43

Added lines #L38 - L43 were not covered by tests

Expand Down
30 changes: 15 additions & 15 deletions R/read_cargo_metadata.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#' Retrieve metadata for packages and workspaces
#'
#' @param path character scalar, the R package directory
#' @param dependencies logical scalar, whether to include all recursive
#' dependencies in stdout (default is FALSE)
#' @param echo logical scalar, should cargo command and outputs be printed to
#' console (default is TRUE)
#' @param dependencies Default `FALSE`. A logical scalar, whether to include
#' all recursive dependencies in stdout.
#' @param echo Default `FALSE`. A logical scalar, should cargo command and
#' outputs be printed to the console.
#'
#' @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"
#' @returns
#' A `list` including the following elements:
#' - `packages`
#' - `workspace_members`
#' - `workspace_default_members`
#' - `resolve`
#' - `target_directory`
#' - `version`
#' - `workspace_root`
#' - `metadata`
#'
#' @export
#'
Expand All @@ -32,7 +33,7 @@
read_cargo_metadata <- function(
path = ".",
dependencies = FALSE,
echo = TRUE) {
echo = FALSE) {
check_string(path, class = "rextendr_error")
check_bool(dependencies, class = "rextendr_error")
check_bool(echo, class = "rextendr_error")
Expand All @@ -53,7 +54,6 @@ read_cargo_metadata <- function(
run_cargo(
args,
wd = find_extendr_crate(path = path),
echo_cmd = echo,
echo = echo,
parse_json = TRUE
)
Expand Down
54 changes: 32 additions & 22 deletions R/run_cargo.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,36 @@
#' @param args character vector, the Cargo subcommand and flags to be executed.
#' @param wd character scalar, location of the Rust crate, (default is
#' `find_extendr_crate()`).
#' @param error_on_status, logical scalar, whether to error if `status != 0`,
#' (default is `TRUE`).
#' @param echo_cmd logical scalar, whether to print Cargo subcommand and flags
#' to the console (default is `TRUE`).
#' @param echo logical scalar, whether to print standard output and error to the
#' console (default is `TRUE`).
#' @param error_on_status Default `TRUE`. A logical scalar, whether to error on a non-zero exist status.
#' @param echo_cmd Default `TRUE`. A logical scalar, whether to print Cargo subcommand and flags
#' to the console.
#' @param echo Default `TRUE`. Alogical scalar, whether to print standard output and error to the
#' console.
#' @param env character vector, environment variables of the child process.
#' @param parse_json logical scalar, whether to parse JSON-structured standard
#' output (default is `FALSE`).
#' @param error_call call scalar, from rlang docs: "the defused call with which
#' the function running in the frame was invoked." (default is
#' `rlang::caller_call()`)
#'
#' @return if `parse_json = TRUE`, result of parsing JSON-structured standard
#' output; otherwise, standard output is returned as a character scalar.
#' @param parse_json Default `FALSE`. A logical scalar, whether to parse JSON-structured standard
#' output using [`jsonlite::parse_json()`] with `simplifyDataFrame = TRUE`.
#' @param error_call Default [`rlang::caller_call()`]. The defused call with which
#' the function running in the frame was invoked.
#' @param ... additional arguments passed to [`processx::run()`].
#' @returns
#' A list with elements `status`, `stdout`, `stderr`, and `timeout`. See [`processx::run()`]. If `parse_json = TRUE`, result of parsing JSON-structured standard

Check warning on line 21 in R/run_cargo.R

View workflow job for this annotation

GitHub Actions / lint

file=R/run_cargo.R,line=21,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 160 characters.
#' output.
#'
#' @keywords internal
#' @noRd
run_cargo <- function(
args,

Check warning on line 27 in R/run_cargo.R

View workflow job for this annotation

GitHub Actions / lint

file=R/run_cargo.R,line=27,col=4,[indentation_linter] Indentation should be 2 spaces but is 4 spaces.
wd = find_extendr_crate(),
error_on_status = TRUE,
echo_cmd = TRUE,
echo = TRUE,
env = get_cargo_envvars(),
parse_json = FALSE,
error_call = rlang::caller_call()) {
error_call = rlang::caller_call(),

Check warning on line 33 in R/run_cargo.R

View workflow job for this annotation

GitHub Actions / lint

file=R/run_cargo.R,line=33,col=39,[trailing_whitespace_linter] Trailing whitespace is superfluous.
...
) {
check_character(args, call = error_call, class = "rextendr_error")
check_string(wd, call = error_call, class = "rextendr_error")
check_bool(error_on_status, call = error_call, class = "rextendr_error")
check_bool(echo_cmd, call = error_call, class = "rextendr_error")
check_bool(echo, call = error_call, class = "rextendr_error")
check_character(env, call = error_call, class = "rextendr_error")
check_bool(parse_json, call = error_call, class = "rextendr_error")
Expand All @@ -46,9 +45,10 @@ run_cargo <- function(
args = args,
error_on_status = error_on_status,
wd = wd,
echo_cmd = echo_cmd,
echo_cmd = echo,
echo = echo,
env = env
env = env,
...
)

stdout <- out[["stdout"]]
Expand All @@ -62,8 +62,18 @@ run_cargo <- function(
}

if (parse_json) {
stdout <- jsonlite::parse_json(stdout, simplifyDataFrame = TRUE)
res <- rlang::try_fetch(
jsonlite::parse_json(stdout, simplifyDataFrame = TRUE),
error = function(cnd) {
cli::cli_abort(
c("Failed to {.code stdout} as json:", " " = "{stdout}"),
parent = cnd,
class = "rextendr_error"
)

Check warning on line 72 in R/run_cargo.R

View check run for this annotation

Codecov / codecov/patch

R/run_cargo.R#L68-L72

Added lines #L68 - L72 were not covered by tests
}
)
return(res)
}

stdout
}
out
}

Check warning on line 79 in R/run_cargo.R

View workflow job for this annotation

GitHub Actions / lint

file=R/run_cargo.R,line=79,col=2,[trailing_blank_lines_linter] Missing terminal newline.
1 change: 0 additions & 1 deletion R/use_crate.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ use_crate <- function(
run_cargo(
args,
wd = find_extendr_crate(path = path),
echo_cmd = echo,
echo = echo
)

Expand Down
28 changes: 14 additions & 14 deletions man/read_cargo_metadata.Rd

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

3 changes: 1 addition & 2 deletions man/vendor_pkgs.Rd

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

5 changes: 1 addition & 4 deletions man/write_license_note.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test-clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ test_that("rextendr::clean() removes cargo target directory & binaries", {

expect_equal(length(dir("src", pattern = "testpkg\\..*")), 1)
expect_true(dir.exists(file.path("src", "rust", "target")))

# clean once
clean()

# we expect an error the second time
expect_error(clean())

expect_error(clean(1L))
expect_error(clean(echo = NULL))
expect_equal(length(dir("src", pattern = "testpkg\\..*")), 0)
expect_false(dir.exists(file.path("src", "rust", "target")))
})
9 changes: 8 additions & 1 deletion tests/testthat/test-license_note.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ test_that("LICENSE.note is generated properly", {
skip_if_cargo_unavailable(c("license", "--help"))

local_package("testPackage")

# try running write_license_note() when there is nothing present
dir.create(file.path("src", "rust"), recursive = TRUE)
expect_error(write_license_note())

# create license note for extendr package

Check warning on line 11 in tests/testthat/test-license_note.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-license_note.R,line=11,col=44,[trailing_whitespace_linter] Trailing whitespace is superfluous.
use_extendr()
write_license_note()

expect_snapshot(cat_file("LICENSE.note"))
expect_error(write_license_note(path = NULL))
expect_error(write_license_note(force = "yup"))
})
5 changes: 2 additions & 3 deletions tests/testthat/test-use_crate.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ test_that("use_crate() adds dependency to package or workspace", {
path = path
)

metadata <- read_cargo_metadata(path)
metadata <- read_cargo_metadata(path, echo = FALSE)

dependency <- metadata[["packages"]][["dependencies"]][[1]]
dependency <- dependency[dependency[["name"]] == "serde", ]

expect_equal(dependency[["name"]], "serde")

expect_equal(dependency[["features"]][[1]], "derive")

expect_equal(dependency[["req"]], "^1.0.1")

})

test_that("use_crate() errors when user passes git and version arguments", {
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,16 @@ test_that("`try_exec_cmd()` returns stdout when command is available", {
echo <- "This is an echo"
expect_equal(try_exec_cmd("echo", echo), echo)
})


test_that("`replace_na()` respects type", {
x <- 1:5
x[2] <- NA
expect_error(replace_na(x, "L"))
})

test_that("`replace_na()` replaces with the correct value", {
x <- 1:5
x[2] <- NA_integer_
expect_identical(replace_na(x, -99L), c(1L, -99L, 3L, 4L, 5L))
})

0 comments on commit f90145f

Please sign in to comment.