File: testthat-helpers.R

package info (click to toggle)
r-cran-reticulate 1.41.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,088 kB
  • sloc: cpp: 5,154; python: 620; sh: 13; makefile: 2
file content (146 lines) | stat: -rw-r--r-- 2,898 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

test_that <- function(desc, code) {

  # don't run tests on CRAN
  enabled <- Sys.getenv("RETICULATE_TESTS_ENABLED", unset = "FALSE")
  if (enabled %in% "FALSE")
    testthat::skip_on_cran()

  # skip if we don't have python
  skip_if_no_python()

  # delegate to testthat
  call <- sys.call()
  call[[1L]] <- quote(testthat::test_that)
  eval(call, envir = parent.frame())

}

context <- function(label) {

  # one-time initialization
  if (!py_available(initialize = FALSE)) {

    config <- tryCatch(py_config(), error = identity)
    if (inherits(config, "error"))
      options(reticulate.python.disabled = TRUE)

    writeLines("\n\n# Python config ----")
    print(config)
    writeLines("")

  }

  if (py_available(initialize = FALSE)) {

    # import some modules used by the tests
    modules <- list(
      test     = import("rpytools.test"),
      inspect  = import("inspect"),
      sys      = import("sys"),
      builtins = import_builtins(convert = FALSE)
    )

    list2env(modules, envir = parent.frame())

  }

  # delegate to testthat
  call <- sys.call()
  call[[1L]] <- quote(testthat::context)
  eval(call, envir = parent.frame())

}

skip <- function(message) {
  testthat::skip(message)
}

skip_on_cran <- function() {
  testthat::skip_on_cran()
}

skip_if_no_python <- function() {

  if (identical(getOption("reticulate.python.disabled"), TRUE))
    skip("Python bindings not available for testing")

  if (!py_available(initialize = TRUE))
    skip("Python bindings not available for testing")

}

skip_if_no_numpy <- function() {

  skip_on_cran()
  skip_if_no_python()

  if (!py_numpy_available())
    skip("NumPy not available for testing")

}

skip_if_no_docutils <- function() {

  skip_on_cran()
  skip_if_no_python()

  if (!py_module_available("docutils"))
    skip("docutils not available for testing.")

}

skip_if_no_pandas <- function() {

  skip_on_cran()
  skip_if_no_python()

  if (!py_module_available("pandas"))
    skip("pandas not available for testing")

}

skip_if_no_scipy <- function() {

  skip_on_cran()
  skip_if_no_python()

  if (!py_module_available("scipy"))
    skip("scipy not available for testing")

  scipy <- import("scipy")
  if (clean_version(scipy$`__version__`) < "1.0")
    skip("scipy version is less than v1.0")

}

skip_if_no_test_environments <- function() {

  skip_on_cran()
  skip_if_no_python()

  skip <- is.na(Sys.getenv("RETICULATE_TEST_ENVIRONMENTS", unset = NA))
  if (skip)
    skip("python environments not available for testing")

}

skip_if_no_conda <- function() {

  skip_on_cran()
  skip_if_no_python()

  if (is.null(tryCatch(reticulate::conda_binary(), error = function(e) NULL)))
    skip("conda not available for testing")

}

skip_if_no_matplotlib <- function() {

  skip_on_cran()
  skip_if_no_python()

  if (!py_module_available("matplotlib"))
    skip("matplotlib not available for testing")

}