Debian Codegraph @

tests/testthat/test-python.R

raw
1
2 renv_test_scope_python <- function(scope = parent.frame()) {
3 renv_scope_envvars(
4 PATH = Sys.getenv("PATH"),
5 RENV_PYTHON = NULL,
6 RETICULATE_PYTHON = NULL,
7 RETICULATE_PYTHON_ENV = NULL,
8 scope = scope
9 )
10 }
11
12 test_that("we can activate Python with a project", {
13
14 skip_on_os("windows")
15 skip_on_cran()
16 skip_if_no_python()
17 renv_test_scope_python()
18
19 renv_tests_scope("breakfast")
20 use_python(python = sys_python(), type = "system")
21
22 lockfile <- renv_lockfile_read("renv.lock")
23 expect_true(!is.null(lockfile$Python))
24
25 })
26
27 test_that("we can activate Python with a virtualenv in a project", {
28
29 skip_slow()
30 skip_on_os("windows")
31 skip_if_no_virtualenv()
32 renv_test_scope_python()
33
34 renv_tests_scope("breakfast")
35 use_python(python = sys_python(), type = "virtualenv")
36
37 lockfile <- renv_lockfile_read("renv.lock")
38 expect_equal(lockfile$Python$Type, "virtualenv")
39
40 })
41
42 test_that("renv uses local virtual environment for names beginning with '.'", {
43
44 skip_on_os("windows")
45 skip_on_cran()
46 skip_if_no_virtualenv()
47 renv_test_scope_python()
48
49 renv_tests_scope("breakfast")
50 use_python(python = sys_python(), name = ".venv")
51
52 expect_true(renv_file_exists(".venv"))
53
54 lockfile <- renv_lockfile_read("renv.lock")
55 expect_equal(lockfile$Python$Type, "virtualenv")
56 expect_equal(lockfile$Python$Name, ".venv")
57
58 })
59
60 test_that("renv can bind to virtualenvs in WORKON_HOME", {
61
62 skip_on_os("windows")
63 skip_on_cran()
64 skip_if_no_virtualenv()
65 renv_test_scope_python()
66
67 # work with temporary virtualenv home
68 renv_scope_envvars(WORKON_HOME = tempdir())
69 home <- renv_python_virtualenv_home()
70 expect_true(renv_path_same(home, tempdir()))
71
72 # construct environment name, path
73 name <- "renv-test-environment"
74 path <- file.path(home, name)
75
76 # clean up when we're done
77 defer(unlink(path, recursive = TRUE))
78
79 # create a test project
80 renv_tests_scope("breakfast")
81 use_python(python = sys_python(), name = "renv-test-environment")
82
83 expect_true(renv_file_exists(path))
84
85 lockfile <- renv_lockfile_read("renv.lock")
86 expect_equal(lockfile$Python$Type, "virtualenv")
87 expect_equal(lockfile$Python$Name, "renv-test-environment")
88
89 })
90
91 test_that("installed Python packages are snapshotted / restored [virtualenv]", {
92
93 skip_slow()
94 skip_on_os("windows")
95 skip_if_no_virtualenv()
96 renv_test_scope_python()
97
98 renv_tests_scope("breakfast")
99
100 python <- sys_python()
101 # initialize python
102 python <- use_python(
103 python,
104 name = renv_scope_tempfile("python-virtualenv-"),
105 type = "virtualenv"
106 )
107
108 # install python-dotenv
109 expect_false(renv_python_module_available(python, "dotenv"))
110 pip_install("python-dotenv", python = python)
111 expect_true(renv_python_module_available(python, "dotenv"))
112
113 # snapshot changes
114 snapshot()
115
116 # check requirements.txt for install
117 expect_true(file.exists("requirements.txt"))
118 reqs <- renv_properties_read("requirements.txt", delimiter = "==")
119 expect_true("python-dotenv" %in% names(reqs))
120
121 # uninstall python-dotenv
122 expect_true(renv_python_module_available(python, "dotenv"))
123 pip_uninstall("python-dotenv", python = python)
124 expect_false(renv_python_module_available(python, "dotenv"))
125
126 # try to restore
127 restore()
128
129 # check that we can load python-dotenv now
130 expect_true(renv_python_module_available(python, "dotenv"))
131
132 })
133
134
135 test_that("installed Python packages are snapshotted / restored [conda]", {
136
137 skip_if_local()
138 skip_on_os("windows")
139 skip_on_cran()
140 skip_if_no_miniconda("3.6")
141 renv_test_scope_python()
142
143 renv_tests_scope("breakfast")
144
145 # initialize python
146 quietly(use_python(type = "conda"))
147 python <- Sys.getenv("RETICULATE_PYTHON")
148
149 # install numpy
150 cmd <- paste(shQuote(python), "-m pip install --quiet numpy")
151 system(cmd, ignore.stdout = TRUE, ignore.stderr = TRUE)
152
153 # snapshot changes
154 snapshot()
155
156 # check requirements.txt for install
157 expect_true(file.exists("environment.yml"))
158
159 # uninstall numpy
160 cmd <- paste(shQuote(python), "-m pip uninstall --quiet --yes numpy")
161 system(cmd, ignore.stdout = TRUE, ignore.stderr = TRUE)
162
163 # can no longer load numpy
164 cmd <- paste(shQuote(python), "-c 'import numpy'")
165 status <- system(cmd, ignore.stdout = TRUE, ignore.stderr = TRUE)
166 expect_false(status == 0L)
167
168 # try to restore
169 restore()
170
171 # check that we can load numpy now
172 cmd <- paste(shQuote(python), "-c 'import numpy'")
173 status <- system(cmd, ignore.stdout = TRUE, ignore.stderr = TRUE)
174 expect_true(status == 0L)
175
176 })
177
178 test_that("python environment name is preserved after snapshot", {
179
180 skip_if_local()
181 skip_on_os("windows")
182 skip_on_cran()
183 skip_if_no_miniconda("3.6")
184 renv_test_scope_python()
185
186 # create and use local python environment
187 renv_tests_scope()
188 init()
189
190 system2(python, c("-m", "venv", "virtualenv"))
191 use_python(name = "./virtualenv")
192
193 # check that the lockfile has been updated
194 lockfile <- renv_lockfile_read("renv.lock")
195 expect_equal(lockfile$Python$Name, "./virtualenv")
196
197 # try to snapshot
198 snapshot()
199
200 # check that the virtual environment name was preserved
201 lockfile <- renv_lockfile_read("renv.lock")
202 expect_equal(lockfile$Python$Name, "./virtualenv")
203
204 })
205
206 test_that("renv_python_discover() respects PATH ordering", {
207
208 skip_on_cran()
209 skip_on_windows()
210 renv_tests_scope()
211 renv_test_scope_python()
212
213 # create a bunch of python directories
214 wd <- renv_path_normalize(getwd())
215 pythons <- file.path(wd, c("1", "2", "3"), "python")
216 for (python in pythons) {
217 ensure_parent_directory(python)
218 file.create(python)
219 Sys.chmod(python, "0755")
220 }
221
222 # update the path
223 path <- paste(dirname(pythons), collapse = .Platform$path.sep)
224 discovered <- local({
225 renv_scope_envvars(PATH = path)
226 renv_python_discover()
227 })
228
229 # check that we looked at them in the right order
230 expect_equal(tail(discovered, n = 3L), pythons)
231
232 })
233
234 test_that("renv uses symlinks for system python", {
235
236 skip_on_cran()
237 skip_on_windows()
238 renv_tests_scope()
239 renv_test_scope_python()
240 skip_if(!file.exists("/usr/bin/python3"))
241
242 use_python("/usr/bin/python3", type = "system")
243 expect_false(Sys.which("python3") == "/usr/bin/python3")
244
245 })