Debian Codegraph @

tests/testthat/helper-snapshot.R

raw
1
2 expect_snapshot <- function(...,
3 cran = FALSE,
4 error = FALSE,
5 transform = strip_dirs,
6 variant = NULL,
7 cnd_class = FALSE)
8 {
9 if (renv_platform_windows() && getRversion() < "4.2") {
10 testthat::skip("Snapshot tests unsupported on older Windows")
11 }
12
13 renv_scope_options(
14 renv.caution.verbose = TRUE,
15 renv.verbose = TRUE
16 )
17
18 testthat::expect_snapshot(
19 ...,
20 cran = cran,
21 error = error,
22 transform = transform,
23 variant = variant,
24 cnd_class = cnd_class
25 )
26 }
27
28 strip_dirs <- function(x) {
29
30 # TODO: we might want to map multiple strings to the same
31 # placeholder, so this should probably be flipped?
32 #
33 # note also that order matters for snapshot tests; the least-specific
34 # paths should go at the end of this list
35 prefix <- if (renv_platform_windows()) "^file:///" else "^file://"
36
37 filters <- list(
38 "<R>" = file.path(R.home("bin"), "R"),
39 "<cache>" = renv_paths_cache(),
40 "<platform-prefix>" = renv_platform_prefix(),
41 "<r-version>" = getRversion(),
42 "<test-repo>" = getOption("repos")[[1L]],
43 "<test-repo-path>" = gsub(prefix, "", getOption("repos")[[1L]]),
44 "<root>" = renv_path_normalize(renv_paths_root()),
45 "<wd>" = renv_path_aliased(getwd()),
46 "<wd>" = renv_path_normalize(getwd()),
47 "<tempdir>" = renv_path_normalize(tempdir()),
48 "<wd-name>" = basename(getwd())
49 )
50
51 # normalize backslashes to forward slashes so snapshots are
52 # platform-independent; do this before applying filters so that
53 # filter paths (which use forward slashes) match consistently
54 x <- chartr("\\", "/", x)
55
56 # apply filters
57 enumerate(filters, function(target, source) {
58 source <- chartr("\\", "/", source)
59 x <<- gsub(source, target, x, fixed = TRUE)
60 })
61
62 # other pattern-based filters here
63 x <- gsub("renv-library-\\w+", "<renv-library>", x)
64 x <- gsub(renv_path_aliased(getwd()), "<wd>", x, fixed = TRUE)
65 x <- gsub(renv_path_aliased(tempdir()), "<tempdir>", x, fixed = TRUE)
66
67 # Standardise the dashes produced by header()
68 x <- gsub("-{3,}\\s*$", "---", x, perl = TRUE)
69
70 # Collapse width-dependent padding in progress lines
71 x <- gsub("(\\.\\.\\.) +(OK|FAILED)", "\\1 \\2", x)
72
73 # Standardise version
74 x <- gsub(renv_metadata_version_friendly(), "<version>", x, fixed = TRUE)
75
76 x
77
78 }
79
80 strip_versions <- function(x) {
81 gsub("\\[[0-9.-]*\\]", "[<version>]", x)
82 }