File: test-reload.R

package info (click to toggle)
r-cran-devtools 2.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,328 kB
  • sloc: sh: 15; makefile: 5
file content (30 lines) | stat: -rw-r--r-- 788 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
test_that("reload works", {
  withr::local_temp_libpaths()

  pkg <- as.package(test_path("testTest"))
  pkg_name <- pkg$package

  install(pkg, quiet = TRUE)
  on.exit(unload(pkg$package), add = TRUE)

  expect_false(is_loaded(pkg))

  # Do nothing if the package is not loaded
  expect_error(reload(pkg, quiet = TRUE), NA)
  expect_false(is_loaded(pkg))

  # Reload if loaded
  requireNamespace(pkg_name, quietly = TRUE)
  expect_true(is_loaded(pkg))
  reload(pkg, quiet = TRUE)
  expect_true(is_loaded(pkg))

  # Re-attach if attached
  unload(pkg$package, quiet = TRUE)
  library(pkg_name, character.only = TRUE, quietly = TRUE)
  expect_true(is_loaded(pkg))
  expect_true(is_attached(pkg))
  reload(pkg, quiet = TRUE)
  expect_true(is_loaded(pkg))
  expect_true(is_attached(pkg))
})