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

Suppress some compilation messages in non-interactive R sessions #462

Merged
merged 5 commits into from
Mar 10, 2021
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
8 changes: 6 additions & 2 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,19 @@ compile <- function(quiet = TRUE,
}

if (!force_recompile) {
message("Model executable is up to date!")
if (interactive()) {
message("Model executable is up to date!")
}
private$cpp_options_ <- cpp_options
private$precompile_cpp_options_ <- NULL
private$precompile_stanc_options_ <- NULL
private$precompile_include_paths_ <- NULL
self$exe_file(exe)
return(invisible(self))
} else {
message("Compiling Stan program...")
if (interactive()) {
message("Compiling Stan program...")
}
}

temp_stan_file <- tempfile(pattern = "model-", fileext = ".stan")
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/helper-custom-expectations.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ expect_gq_output <- function(object, num_chains = NULL) {
}
}
expect_output(object, output)
}
}

expect_interactive_message <- function(object, regexp = NULL) {
if (interactive()) {
expect_message(object = object, regexp = regexp)
} else {
expect_silent(object = object)
}
}
3 changes: 1 addition & 2 deletions tests/testthat/test-knitr.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ test_that("eng_cmdstan works", {
cache = TRUE,
cache.path = tempdir()
))
expect_message(eng_cmdstan(opts), "Compiling Stan program")

expect_interactive_message(eng_cmdstan(opts), "Compiling Stan program")
opts$eval <- FALSE
expect_silent(eng_cmdstan(opts))
})
Expand Down
21 changes: 12 additions & 9 deletions tests/testthat/test-model-compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ test_that("compile() method works", {
if (file.exists(exe)) {
file.remove(exe)
}
expect_message(mod$compile(quiet = TRUE), "Compiling Stan program...")
expect_message(mod$compile(quiet = TRUE), "Model executable is up to date!")
expect_interactive_message(mod$compile(quiet = TRUE), "Compiling Stan program...")
expect_interactive_message(mod$compile(quiet = TRUE), "Model executable is up to date!")
checkmate::expect_file_exists(mod$hpp_file())
checkmate::expect_file_exists(exe)
file.remove(exe)
Expand All @@ -45,7 +45,10 @@ test_that("compile() method works", {
test_that("compile() method forces recompilation force_recompile = TRUE", {
skip_on_cran()
mod$compile(quiet = TRUE)
expect_message(mod$compile(quiet = TRUE, force_recompile = TRUE), "Compiling Stan program...")
expect_interactive_message(
mod$compile(quiet = TRUE, force_recompile = TRUE),
"Compiling Stan program..."
)
})

test_that("compile() method forces recompilation if model modified", {
Expand All @@ -56,7 +59,7 @@ test_that("compile() method forces recompilation if model modified", {
mod$compile(quiet = TRUE)
}
Sys.setFileTime(mod$stan_file(), Sys.time() + 1) #touch file to trigger recompile
expect_message(mod$compile(quiet = TRUE), "Compiling Stan program...")
expect_interactive_message(mod$compile(quiet = TRUE), "Compiling Stan program...")
})

test_that("compile() method works with spaces in path", {
Expand All @@ -75,7 +78,7 @@ test_that("compile() method works with spaces in path", {
if (file.exists(exe)) {
file.remove(exe)
}
expect_message(mod_spaces$compile(), "Compiling Stan program...")
expect_interactive_message(mod_spaces$compile(), "Compiling Stan program...")
file.remove(stan_model_with_spaces)
file.remove(exe)
file.remove(dir_with_spaces)
Expand Down Expand Up @@ -111,7 +114,7 @@ test_that("compilation works with include_paths", {
)
)

expect_message(
expect_interactive_message(
mod_w_include <- cmdstan_model(stan_file = stan_program_w_include, quiet = TRUE,
include_paths = test_path("resources", "stan")),
"Compiling Stan program"
Expand Down Expand Up @@ -170,15 +173,15 @@ test_that("switching threads on and off works without rebuild", {
test_that("multiple cpp_options work", {
skip_on_cran()
stan_file <- testing_stan_file("bernoulli")
expect_message(
expect_interactive_message(
mod <- cmdstan_model(stan_file, cpp_options = list("DUMMY_TEST2"="1", "DUMMY_TEST2"="1", "DUMMY_TEST3"="1"), force_recompile = TRUE),
"Compiling Stan program..."
)
expect_message(
expect_interactive_message(
mod$compile(cpp_options = list("DUMMY_TEST2"="1", "DUMMY_TEST2"="1", "DUMMY_TEST3"="1"), force_recompile = TRUE),
"Compiling Stan program..."
)
expect_message(
expect_interactive_message(
mod$compile(cpp_options = list(), force_recompile = TRUE),
"Compiling Stan program..."
)
Expand Down