Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpp_register(quiet=) set to TRUE on non-interactive session (incl testthat runs) #289

Merged
merged 7 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Config/Needs/cpp11/cpp_register:
vctrs
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
RoxygenNote: 7.2.1
SystemRequirements: C++11
Remotes:
r-lib/testthat@ad8b726d1734c59ed08ec7d11df4a3d76d8e69df
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

* `cpp_source()` errors on non-existent file (#261).

* `cpp_register()` is quiet by default when R is non interactive (#289).

* updated test to adapt to changes in R 4.2.1 (#290).

# cpp11 0.4.2
Expand Down
4 changes: 2 additions & 2 deletions R/register.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#'
#' # cleanup
#' unlink(dir, recursive = TRUE)
cpp_register <- function(path = ".", quiet = FALSE) {
cpp_register <- function(path = ".", quiet = !is_interactive()) {
stop_unless_installed(get_cpp_register_needs())

r_path <- file.path(path, "R", "cpp11.R")
Expand Down Expand Up @@ -138,7 +138,7 @@ cpp_register <- function(path = ".", quiet = FALSE) {

utils::globalVariables(c("name", "return_type", "line", "decoration", "context", ".", "functions", "res"))

get_registered_functions <- function(decorations, tag, quiet = FALSE) {
get_registered_functions <- function(decorations, tag, quiet = !is_interactive()) {
if (NROW(decorations) == 0) {
return(tibble::tibble(file = character(), line = integer(), decoration = character(), params = list(), context = list(), name = character(), return_type = character(), args = list()))
}
Expand Down
4 changes: 1 addition & 3 deletions R/source.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, qu
#provide original path for error messages
check_valid_attributes(all_decorations, file = orig_file_path)

cli_suppress(
funs <- get_registered_functions(all_decorations, "cpp11::register")
)
funs <- get_registered_functions(all_decorations, "cpp11::register", quiet = quiet)
cpp_functions_definitions <- generate_cpp_functions(funs, package = package)

cpp_path <- file.path(dirname(new_file_path), "cpp11.cpp")
Expand Down
9 changes: 0 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
cli_suppress <- function(expr) {
withCallingHandlers(
expr,
cli_message = function(c) {
invokeRestart("cli_message_handled")
}
)
}

glue_collapse_data <- function(data, ..., sep = ", ", last = "") {
res <- glue::glue_collapse(glue::glue_data(data, ...), sep = sep, last = last)
if (length(res) == 0) {
Expand Down
2 changes: 1 addition & 1 deletion man/cpp_register.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/_snaps/register.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# cpp_register: can be run with messages

Code
cpp_register(p, quiet = FALSE)
Message <cliMessage>
ℹ 1 functions decorated with [[cpp11::register]]
✔ generated file ']8;;file:///Users/romainfrancois/git/cpp11/tests/testthat/cpp11.Rcpp11.R]8;;'
✔ generated file ']8;;file:///Users/romainfrancois/git/cpp11/tests/testthat/cpp11.cppcpp11.cpp]8;;'

2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pkg_path <- function(pkg) {

get_funs <- function(path) {
all_decorations <- decor::cpp_decorations(path, is_attribute = TRUE)
get_registered_functions(all_decorations, "cpp11::register", quiet = FALSE)
get_registered_functions(all_decorations, "cpp11::register", quiet = TRUE)
}

get_package_name <- function(path) {
Expand Down
7 changes: 5 additions & 2 deletions tests/testthat/test-register.R
Original file line number Diff line number Diff line change
Expand Up @@ -614,12 +614,15 @@ extern \"C\" attribute_visible void R_init_testPkg(DllInfo* dll){
})


it("can be run with messages, by default", {
it("can be run with messages", {
pkg <- local_package()
p <- pkg_path(pkg)
dir.create(file.path(p, "src"))
file.copy(test_path("single.cpp"), file.path(p, "src", "single.cpp"))
expect_message(cpp_register(p), "1 functions decorated with [[cpp11::register]]", fixed = TRUE)

expect_snapshot(
cpp_register(p, quiet = FALSE)
)
romainfrancois marked this conversation as resolved.
Show resolved Hide resolved
})

it("includes pkg_types.h if included in src", {
Expand Down
8 changes: 0 additions & 8 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
describe("cli_suppress", {
it("suppresses cli outputs", {
f <- function() cli::cli_text("hi")
expect_message(f(), "hi")
expect_message(cli_suppress(f()), NA)
})
})

describe("glue_collapse_data", {
it("works with empty inputs", {
expect_equal(
Expand Down