|
| 1 | +# copied from https://github.com/tidymodels/tune/blob/main/tests/testthat/helper-tune-package.R |
| 2 | +catalog_lines <- function(lines) { |
| 3 | + lines[grepl("^>", lines)] |
| 4 | +} |
| 5 | + |
| 6 | +# Make a new binding to prevent infinite recursion when the original is mocked. |
| 7 | +initialize_catalog_ <- tune:::initialize_catalog |
| 8 | + |
| 9 | +# Sets a new exit handler on `initialize_catalog()` that stores the summary |
| 10 | +# of issues before it's cleared along with the progress bar. Together with |
| 11 | +# the above, we can test the full catalog output. |
| 12 | +redefer_initialize_catalog <- function(test_env) { |
| 13 | + local({ |
| 14 | + function(control, env = rlang::caller_env()) { |
| 15 | + initialize_catalog_(control, env) |
| 16 | + |
| 17 | + withr::defer( |
| 18 | + assign( |
| 19 | + "catalog_summary_test", |
| 20 | + tune:::tune_env$progress_env$catalog_summary, |
| 21 | + test_env |
| 22 | + ), |
| 23 | + envir = env, |
| 24 | + priority = "first" |
| 25 | + ) |
| 26 | + |
| 27 | + NULL |
| 28 | + } |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +test_that("interactive logger works (finetune integration, error)", { |
| 33 | + skip_if(tune:::allow_parallelism(FALSE), "Will not catalog: parallelism is enabled") |
| 34 | + local_mocked_bindings( |
| 35 | + is_testing = function() {FALSE}, |
| 36 | + initialize_catalog = redefer_initialize_catalog(rlang::current_env()), |
| 37 | + .package = "tune" |
| 38 | + ) |
| 39 | + library(finetune) |
| 40 | + |
| 41 | + raise_error <- function(x) {stop("AHHhH")} |
| 42 | + raise_warning <- function(x) {warning("ope! yikes.")} |
| 43 | + |
| 44 | + set.seed(1) |
| 45 | + expect_snapshot( |
| 46 | + {res_anova <- |
| 47 | + tune_race_anova( |
| 48 | + parsnip::nearest_neighbor("regression", "kknn", neighbors = tune()), |
| 49 | + Sale_Price ~ ., |
| 50 | + rsample::vfold_cv(modeldata::ames[, c(72, 40:45)], 5), |
| 51 | + control = control_race(extract = function(x) {raise_warning(); raise_error()}) |
| 52 | + )}, |
| 53 | + transform = catalog_lines |
| 54 | + ) |
| 55 | + |
| 56 | + # `catalog_summary_test` written to this env via `redefer_initialize_catalog()` |
| 57 | + expect_snapshot(catalog_summary_test) |
| 58 | + |
| 59 | + set.seed(1) |
| 60 | + expect_snapshot( |
| 61 | + {res_sa <- |
| 62 | + tune_sim_anneal( |
| 63 | + parsnip::nearest_neighbor("regression", "kknn", neighbors = tune()), |
| 64 | + Sale_Price ~ ., |
| 65 | + rsample::vfold_cv(modeldata::ames[, c(72, 40:45)], 5), |
| 66 | + initial = res_anova, |
| 67 | + iter = 15, |
| 68 | + control = control_sim_anneal(verbose_iter = FALSE, |
| 69 | + extract = function(x) {raise_warning(); raise_error()}) |
| 70 | + )}, |
| 71 | + transform = catalog_lines |
| 72 | + ) |
| 73 | + |
| 74 | + # `catalog_summary_test` written to this env via `redefer_initialize_catalog()` |
| 75 | + expect_snapshot(catalog_summary_test) |
| 76 | +}) |
0 commit comments