xoopR / distr6

R6 object-oriented interface for probability distributions.
https://xoopr.github.io/distr6/
Other
99 stars 23 forks source link

Add skip_if_not_installed() protections for Suggested packages #267

Closed MichaelChirico closed 2 years ago

MichaelChirico commented 2 years ago

Hi, I can't install GoFKernel (because it Depends on KernSmooth, which has licensing issues), so testing distr6 caused a lot of subtle issues -- the test output looks like:

══ Failed tests ════════════════════════════════════════════════════════════════
── Error (test-decorator-functionimputation.R:70:3): constructor ───────────────────────────
Error in `assert_pkgload(self$packages)`: The following packages could not be loaded, please install: {GoFKernel}
Backtrace:
    █
 1. ├─testthat::expect_error(...) test-decorator-functionimputation.R:70:2
 2. │ └─testthat:::expect_condition_matching(...)
 3. │   └─testthat:::quasi_capture(...)
 4. │     ├─testthat:::.capture(...)
 5. │     │ └─base::withCallingHandlers(...)
 6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 7. └─FunctionImputation$new()$decorate(MultivariateNormal$new())
 8.   └─distr6:::assert_pkgload(self$packages)
── Error (test-decorator-functionimputation.R:95:3): basic pdf checks ───────────────────────────
Error in `assert_pkgload(self$packages)`: The following packages could not be loaded, please install: {GoFKernel}
Backtrace:
     █
  1. ├─testthat::expect_message(...) test-decorator-functionimputation.R:95:2
  2. │ └─testthat:::expect_condition_matching(...)
  3. │   └─testthat:::quasi_capture(...)
  4. │     ├─testthat:::.capture(...)
  5. │     │ └─base::withCallingHandlers(...)
  6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
  7. └─distr6::decorate(cont_pdf, "FunctionImputation", n = 50000)
  8.   ├─base::suppressMessages(...)
  9.   │ └─base::withCallingHandlers(...)
 10.   └─base::lapply(...)
 11.     └─distr6:::FUN(X[[i]], ...)
 12.       └─get(a_decorator)$new()$decorate(distribution, ...)
 13.         └─distr6:::assert_pkgload(self$packages)
── Failure (test-decorator-functionimputation.R:117:3): continuous pdf2cdf ───────────────────────────
cont_pdf$cdf(1:3) (`actual`) not equal to pexp(1:3) (`expected`).

`actual` is NULL
`expected` is a double vector (0.632120558828558, 0.864664716763387, 0.950212931632136)
Backtrace:
    █
 1. ├─testthat::expect_message(expect_equal(cont_pdf$cdf(1:3), pexp(1:3))) test-decorator-functionimputation.R:117:2
 2. │ └─testthat:::expect_condition_matching(...)
 3. │   └─testthat:::quasi_capture(...)
 4. │     ├─testthat:::.capture(...)
 5. │     │ └─base::withCallingHandlers(...)
 6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 7. └─testthat::expect_equal(cont_pdf$cdf(1:3), pexp(1:3))
── Failure (test-decorator-functionimputation.R:117:3): continuous pdf2cdf ───────────────────────────
`expect_equal(cont_pdf$cdf(1:3), pexp(1:3))` did not throw the expected message.
Backtrace:
    █
 1. └─testthat::expect_message(expect_equal(cont_pdf$cdf(1:3), pexp(1:3))) test-decorator-functionimputation.R:117:2
 2.   └─testthat:::expect_condition_matching(...)
── Failure (test-decorator-functionimputation.R:122:3): discrete pdf2cdf ───────────────────────────
disc_pdf$cdf(0:10) (`actual`) not equal to pgeom(0:10, prob = 0.5) (`expected`).

`actual` is NULL
`expected` is a double vector (0.5, 0.75, 0.875, 0.9375, 0.96875, ...)
Backtrace:
    █
 1. ├─testthat::expect_message(...) test-decorator-functionimputation.R:122:2
 2. │ └─testthat:::expect_condition_matching(...)
 3. │   └─testthat:::quasi_capture(...)
 4. │     ├─testthat:::.capture(...)
 5. │     │ └─base::withCallingHandlers(...)
 6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 7. └─testthat::expect_equal(disc_pdf$cdf(0:10), pgeom(0:10, prob = 0.5))
── Failure (test-decorator-functionimputation.R:122:3): discrete pdf2cdf ───────────────────────────
`expect_equal(disc_pdf$cdf(0:10), pgeom(0:10, prob = 0.5))` did not throw the expected message.
Backtrace:
    █
 1. └─testthat::expect_message(...) test-decorator-functionimputation.R:122:2
 2.   └─testthat:::expect_condition_matching(...)
── Error (test-decorator-functionimputation.R:130:3): continuous pdf2quantile ───────────────────────────
Error in `round(object, dp)`: non-numeric argument to mathematical function
Backtrace:
     █
  1. ├─testthat::expect_message(...) test-decorator-functionimputation.R:130:2
  2. │ └─testthat:::expect_condition_matching(...)
  3. │   └─testthat:::quasi_capture(...)
  4. │     ├─testthat:::.capture(...)
  5. │     │ └─base::withCallingHandlers(...)
  6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
  7. └─distr6:::expect_rounded_equal(...)
  8.   └─testthat::expect_equal(...) helpers.R:2:2
  9.     └─testthat::quasi_label(enquo(object), label, arg = "object")
 10.       └─rlang::eval_bare(expr, quo_get_env(quo))
── Failure (test-decorator-functionimputation.R:137:3): discrete pdf2quantile ───────────────────────────
disc_pdf$quantile(c(0.2, 0.42, 0.6)) (`actual`) not equal to qgeom(c(0.2, 0.42, 0.6), prob = 0.5) (`expected`).

`actual` is NULL
`expected` is a double vector (0, 0, 1)
Backtrace:
    █
 1. ├─testthat::expect_message(...) test-decorator-functionimputation.R:137:2
 2. │ └─testthat:::expect_condition_matching(...)
 3. │   └─testthat:::quasi_capture(...)
 4. │     ├─testthat:::.capture(...)
 5. │     │ └─base::withCallingHandlers(...)
 6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 7. └─testthat::expect_equal(...)
── Failure (test-decorator-functionimputation.R:137:3): discrete pdf2quantile ───────────────────────────
`expect_equal(...)` did not throw the expected message.
Backtrace:
    █
 1. └─testthat::expect_message(...) test-decorator-functionimputation.R:137:2
 2.   └─testthat:::expect_condition_matching(...)
── Error (test-decorator-functionimputation.R:151:3): continuous pdf2rand ───────────────────────────
Error in `ks.test(r, t)`: not enough 'x' data
Backtrace:
     █
  1. ├─testthat::expect_warning(...) test-decorator-functionimputation.R:151:2
  2. │ └─testthat:::expect_condition_matching(...)
  3. │   └─testthat:::quasi_capture(...)
  4. │     ├─testthat:::.capture(...)
  5. │     │ └─base::withCallingHandlers(...)
  6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
  7. ├─testthat::expect_true(ks.test(r, t)$p.value > 0.05)
  8. │ └─testthat::quasi_label(enquo(object), label, arg = "object")
  9. │   └─rlang::eval_bare(expr, quo_get_env(quo))
 10. └─stats::ks.test(r, t)
── Error (test-decorator-functionimputation.R:161:3): discrete pdf2rand ───────────────────────────
Error in `ks.test(r, t)`: not enough 'x' data
Backtrace:
     █
  1. ├─testthat::expect_warning(...) test-decorator-functionimputation.R:161:2
  2. │ └─testthat:::expect_condition_matching(...)
  3. │   └─testthat:::quasi_capture(...)
  4. │     ├─testthat:::.capture(...)
  5. │     │ └─base::withCallingHandlers(...)
  6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
  7. ├─testthat::expect_true(ks.test(r, t)$p.value > 0.05)
  8. │ └─testthat::quasi_label(enquo(object), label, arg = "object")
  9. │   └─rlang::eval_bare(expr, quo_get_env(quo))
 10. └─stats::ks.test(r, t)
── Error (test-decorator-functionimputation.R:181:3): basic cdf checks ───────────────────────────
Error in `assert_pkgload(self$packages)`: The following packages could not be loaded, please install: {GoFKernel}
Backtrace:
     █
  1. ├─testthat::expect_message(...) test-decorator-functionimputation.R:181:2
  2. │ └─testthat:::expect_condition_matching(...)
  3. │   └─testthat:::quasi_capture(...)
  4. │     ├─testthat:::.capture(...)
  5. │     │ └─base::withCallingHandlers(...)
  6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
  7. └─distr6::decorate(cont_cdf, "FunctionImputation", n = 10000)
  8.   ├─base::suppressMessages(...)
  9.   │ └─base::withCallingHandlers(...)
 10.   └─base::lapply(...)
 11.     └─distr6:::FUN(X[[i]], ...)
 12.       └─get(a_decorator)$new()$decorate(distribution, ...)
 13.         └─distr6:::assert_pkgload(self$packages)
── Failure (test-decorator-functionimputation.R:201:3): continuous cdf2pdf ───────────────────────────
cont_cdf$pdf(1) (`actual`) not equal to dexp(1) (`expected`).

`actual` is NULL
`expected` is a double vector (0.367879441171442)
Backtrace:
    █
 1. ├─testthat::expect_message(expect_equal(cont_cdf$pdf(1), dexp(1))) test-decorator-functionimputation.R:201:2
 2. │ └─testthat:::expect_condition_matching(...)
 3. │   └─testthat:::quasi_capture(...)
 4. │     ├─testthat:::.capture(...)
 5. │     │ └─base::withCallingHandlers(...)
 6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 7. └─testthat::expect_equal(cont_cdf$pdf(1), dexp(1))
── Failure (test-decorator-functionimputation.R:201:3): continuous cdf2pdf ───────────────────────────
`expect_equal(cont_cdf$pdf(1), dexp(1))` did not throw the expected message.
Backtrace:
    █
 1. └─testthat::expect_message(expect_equal(cont_cdf$pdf(1), dexp(1))) test-decorator-functionimputation.R:201:2
 2.   └─testthat:::expect_condition_matching(...)
── Failure (test-decorator-functionimputation.R:202:3): continuous cdf2pdf ───────────────────────────
cont_cdf$pdf(1:3) (`actual`) not equal to dexp(1:3) (`expected`).

`actual` is NULL
`expected` is a double vector (0.367879441171442, 0.135335283236613, 0.0497870683678639)
Backtrace:
    █
 1. ├─testthat::expect_message(expect_equal(cont_cdf$pdf(1:3), dexp(1:3))) test-decorator-functionimputation.R:202:2
 2. │ └─testthat:::expect_condition_matching(...)
 3. │   └─testthat:::quasi_capture(...)
 4. │     ├─testthat:::.capture(...)
 5. │     │ └─base::withCallingHandlers(...)
 6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 7. └─testthat::expect_equal(cont_cdf$pdf(1:3), dexp(1:3))
── Failure (test-decorator-functionimputation.R:202:3): continuous cdf2pdf ───────────────────────────
`expect_equal(cont_cdf$pdf(1:3), dexp(1:3))` did not throw the expected message.
Backtrace:
    █
 1. └─testthat::expect_message(expect_equal(cont_cdf$pdf(1:3), dexp(1:3))) test-decorator-functionimputation.R:202:2
 2.   └─testthat:::expect_condition_matching(...)
── Failure (test-decorator-functionimputation.R:203:3): continuous cdf2pdf ───────────────────────────
cont_cdf$pdf(1:3, log = TRUE) (`actual`) not equal to dexp(1:3, log = TRUE) (`expected`).

`actual` is NULL
`expected` is a double vector (-1, -2, -3)
Backtrace:
    █
 1. ├─testthat::expect_message(...) test-decorator-functionimputation.R:203:2
 2. │ └─testthat:::expect_condition_matching(...)
 3. │   └─testthat:::quasi_capture(...)
 4. │     ├─testthat:::.capture(...)
 5. │     │ └─base::withCallingHandlers(...)
 6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 7. └─testthat::expect_equal(...)
── Failure (test-decorator-functionimputation.R:203:3): continuous cdf2pdf ───────────────────────────
`expect_equal(cont_cdf$pdf(1:3, log = TRUE), dexp(1:3, log = TRUE))` did not throw the expected message.
Backtrace:
    █
 1. └─testthat::expect_message(...) test-decorator-functionimputation.R:203:2
 2.   └─testthat:::expect_condition_matching(...)
── Failure (test-decorator-functionimputation.R:207:3): discrete cdf2pdf ───────────────────────────
disc_cdf$pdf(1) (`actual`) not equal to dgeom(1, prob = 0.5) (`expected`).

`actual` is NULL
`expected` is a double vector (0.25)
── Error (test-decorator-functionimputation.R:216:3): continuous cdf2quantile ───────────────────────────
Error in `round(object, dp)`: non-numeric argument to mathematical function
Backtrace:
     █
  1. ├─testthat::expect_message(...) test-decorator-functionimputation.R:216:2
  2. │ └─testthat:::expect_condition_matching(...)
  3. │   └─testthat:::quasi_capture(...)
  4. │     ├─testthat:::.capture(...)
  5. │     │ └─base::withCallingHandlers(...)
  6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
  7. └─distr6:::expect_rounded_equal(...)
  8.   └─testthat::expect_equal(...) helpers.R:2:2
  9.     └─testthat::quasi_label(enquo(object), label, arg = "object")
 10.       └─rlang::eval_bare(expr, quo_get_env(quo))
── Failure (test-decorator-functionimputation.R:223:3): discrete cdf2quantile ───────────────────────────
disc_cdf$quantile(c(0.2, 0.42, 0.6)) (`actual`) not equal to qgeom(c(0.2, 0.42, 0.6), prob = 0.5) (`expected`).

`actual` is NULL
`expected` is a double vector (0, 0, 1)
Backtrace:
    █
 1. ├─testthat::expect_message(...) test-decorator-functionimputation.R:223:2
 2. │ └─testthat:::expect_condition_matching(...)
 3. │   └─testthat:::quasi_capture(...)
 4. │     ├─testthat:::.capture(...)
 5. │     │ └─base::withCallingHandlers(...)
 6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 7. └─testthat::expect_equal(...)
── Failure (test-decorator-functionimputation.R:223:3): discrete cdf2quantile ───────────────────────────
`expect_equal(...)` did not throw the expected message.
Backtrace:
    █
 1. └─testthat::expect_message(...) test-decorator-functionimputation.R:223:2
 2.   └─testthat:::expect_condition_matching(...)
── Error (test-decorator-functionimputation.R:237:3): continuous cdf2rand ───────────────────────────
Error in `ks.test(r, t)`: not enough 'x' data
Backtrace:
    █
 1. ├─testthat::expect_true(ks.test(r, t)$p.value > 0.05) test-decorator-functionimputation.R:237:2
 2. │ └─testthat::quasi_label(enquo(object), label, arg = "object")
 3. │   └─rlang::eval_bare(expr, quo_get_env(quo))
 4. └─stats::ks.test(r, t)
── Error (test-decorator-functionimputation.R:247:3): discrete cdf2rand ───────────────────────────
Error in `ks.test(r, t)`: not enough 'x' data
Backtrace:
     █
  1. ├─testthat::expect_warning(...) test-decorator-functionimputation.R:247:2
  2. │ └─testthat:::expect_condition_matching(...)
  3. │   └─testthat:::quasi_capture(...)
  4. │     ├─testthat:::.capture(...)
  5. │     │ └─base::withCallingHandlers(...)
  6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
  7. ├─testthat::expect_true(ks.test(r, t)$p.value > 0.05)
  8. │ └─testthat::quasi_label(enquo(object), label, arg = "object")
  9. │   └─rlang::eval_bare(expr, quo_get_env(quo))
 10. └─stats::ks.test(r, t)
── Error (test-decorator-functionimputation.R:266:3): continuous quantile2rand ───────────────────────────
Error in `assert_pkgload(self$packages)`: The following packages could not be loaded, please install: {GoFKernel}
Backtrace:
     █
  1. ├─testthat::expect_message(decorate(cont_cdf, "FunctionImputation")) test-decorator-functionimputation.R:266:2
  2. │ └─testthat:::expect_condition_matching(...)
  3. │   └─testthat:::quasi_capture(...)
  4. │     ├─testthat:::.capture(...)
  5. │     │ └─base::withCallingHandlers(...)
  6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
  7. └─distr6::decorate(cont_cdf, "FunctionImputation")
  8.   ├─base::suppressMessages(...)
  9.   │ └─base::withCallingHandlers(...)
 10.   └─base::lapply(...)
 11.     └─distr6:::FUN(X[[i]], ...)
 12.       └─get(a_decorator)$new()$decorate(distribution, ...)
 13.         └─distr6:::assert_pkgload(self$packages)
── Error (test-sdistribution-LogLogistic.R:4:3): autotest ───────────────────────────
Error in `assert_pkgload(self$packages)`: The following packages could not be loaded, please install: {actuar}
Backtrace:
    █
 1. └─distr6:::autotest_sdistribution(...) test-sdistribution-LogLogistic.R:4:2
 2.   ├─base::do.call(sdist$new, pars) helpers.R:84:2
 3.   └─(function (...) ...
 4.     └─distr6:::initialize(...)
 5.       └─super$initialize(...)
 6.         └─distr6:::assert_pkgload(self$packages)

[ FAIL 26 | WARN 0 | SKIP 0 | PASS 4371 ]
✖ | 1       0 | testthat [89.5s]                                                
────────────────────────────────────────────────────────────────────────────────
Error (testthat.R:4:1): (code run outside of `test_that()`)
Error: Test failures
Backtrace:
 1. testthat::test_check("distr6") testthat.R:4:0
 2. testthat::test_dir(...)
 3. testthat:::test_files(...)
 4. testthat:::test_files(...)
 5. testthat:::test_files_check(...)
────────────────────────────────────────────────────────────────────────────────

══ Results ═════════════════════════════════════════════════════════════════════
Duration: 89.6 s

As you can see, it's quite noisy / very hard to pick up on the relationship of GoFKernel to many of the failures (besides just those that reference GoFKernel explicitly).

I also can't install the latest version of actuar (since it has an R version dependency I don't meet yet), but that's only one test so it's easy to skip/remove it manually.

A fix would be to add skip_if_not_installed("GoFKernel") (ditto for other Suggests) where appropriate.

At present the GoFKernel tests are heavily interweaved with other tests that don't require it -- maybe it's better to move the GoFKernel functionality to a separate test file?

RaphaelS1 commented 2 years ago

A fix would be to add skip_if_not_installed("GoFKernel") (ditto for other Suggests) where appropriate.

Will do, thanks for spotting

maybe it's better to move the GoFKernel functionality to a separate test file?

Would make it harder to find tests this way

MichaelChirico commented 2 years ago

maybe library(GoFKernel) at the top of the file would suffice. that way it's clear to readers that the package is deeply embedded in that files tests

RaphaelS1 commented 2 years ago

Closed in #268