File: snapshot-file-snaps.R

package info (click to toggle)
r-cran-testthat 3.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,452 kB
  • sloc: cpp: 9,261; ansic: 37; sh: 14; makefile: 5
file content (102 lines) | stat: -rw-r--r-- 2,607 bytes parent folder | download | duplicates (2)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Manage a test files worth of snapshots - if the test file uses variants, this
# will correspond to multiple output files.
FileSnaps <- R6::R6Class("FileSnaps", public = list(
  snap_path = NULL,
  file = NULL,
  type = NULL,
  snaps = NULL,

  initialize = function(snap_path, file, type = c("old", "cur", "new")) {
    self$snap_path <- snap_path
    self$file <- file
    self$type <- arg_match(type)

    if (self$type == "old") {
      # Find variants
      variants <- c("_default", dirs(self$snap_path))
      paths <- set_names(self$path(variants), variants)
      paths <- paths[file.exists(paths)]

      self$snaps <- lapply(paths, read_snaps)
    } else {
      self$snaps <- list(`_default` = list())
    }
  },

  get = function(test, variant, i) {
    test_snaps <- self$snaps[[variant]][[test]]
    if (i > length(test_snaps)) {
      NULL
    } else {
      test_snaps[[i]]
    }
  },

  set = function(test, variant, i, data) {
    self$snaps[[variant]][[test]][[i]] <- data
  },

  append = function(test, variant, data) {
    if (!has_name(self$snaps, variant)) {
      # Needed for R < 3.6
      self$snaps[[variant]] <- list()
    }

    self$snaps[[variant]][[test]] <- c(self$snaps[[variant]][[test]], data)
    length(self$snaps[[variant]][[test]])
  },

  reset = function(test, old) {
    for (variant in names(self$snaps)) {
      cur_test <- self$snaps[[variant]][[test]]
      old_test <- old$snaps[[variant]][[test]]

      if (length(cur_test) == 0) {
        self$snaps[[variant]][[test]] <- old_test
      } else if (length(old_test) > length(cur_test)) {
        self$snaps[[variant]][[test]] <- c(cur_test, old_test[-seq_along(cur_test)])
      }
    }
    invisible()
  },

  write = function(variants = names(self$snaps)) {
    for (variant in variants) {
      default <- variant == "_default"
      if (!default) {
        dir.create(file.path(self$snap_path, variant), showWarnings = FALSE)
      }

      write_snaps(
        self$snaps[[variant]],
        self$path(variant),
        delete = default
      )
    }
    invisible()
  },

  delete = function(variant = "_default") {
    unlink(self$path(variant))
    invisible()
  },

  variants = function() {
    names(self$snaps)
  },

  filename = function() {
    paste0(self$file, if (self$type == "new") ".new", ".md")
  },

  path = function(variant = "_default") {
    ifelse(variant == "_default",
      file.path(self$snap_path, self$filename()),
      file.path(self$snap_path, variant, self$filename())
    )
  }
))

dirs <- function(path) {
  list.dirs(path, recursive = FALSE, full.names = FALSE)
}