-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest_environment.py
272 lines (225 loc) · 8.86 KB
/
test_environment.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import re
import sys
import os
import tempfile
import subprocess
from unittest import TestCase
import rsconnect.environment
from rsconnect.exception import RSConnectException
from rsconnect.environment import Environment, which_python
from rsconnect.subprocesses.inspect_environment import get_python_version, get_default_locale, filter_pip_freeze_output
from .utils import get_dir
import pytest
version_re = re.compile(r"\d+\.\d+(\.\d+)?")
TESTDATA = os.path.join(os.path.dirname(__file__), "testdata")
class TestEnvironment(TestCase):
@staticmethod
def python_version():
return ".".join(map(str, sys.version_info[:3]))
def test_get_python_version(self):
self.assertEqual(
get_python_version(),
self.python_version(),
)
def test_get_default_locale(self):
self.assertEqual(get_default_locale(lambda: ("en_US", "UTF-8")), "en_US.UTF-8")
self.assertEqual(get_default_locale(lambda: (None, "UTF-8")), ".UTF-8")
self.assertEqual(get_default_locale(lambda: ("en_US", None)), "en_US.")
self.assertEqual(get_default_locale(lambda: (None, None)), "")
def test_file(self):
result = Environment.create_python_environment(get_dir("pip1"))
self.assertTrue(version_re.match(result.pip))
self.assertIsInstance(result.locale, str)
self.assertIn(".", result.locale)
expected = Environment.from_dict(
dict(
contents="numpy\npandas\nmatplotlib\n",
filename="requirements.txt",
locale=result.locale,
package_manager="pip",
pip=result.pip,
python=self.python_version(),
source="file",
),
python_interpreter=sys.executable,
)
self.assertEqual(expected, result)
def test_pip_freeze(self):
result = Environment.create_python_environment(get_dir("pip2"))
# these are the dependencies declared in our pyproject.toml
self.assertIn("six", result.contents)
self.assertIn("click", result.contents.lower())
self.assertTrue(version_re.match(result.pip))
self.assertIsInstance(result.locale, str)
self.assertIn(".", result.locale)
expected = Environment.from_dict(
dict(
contents=result.contents,
filename="requirements.txt",
locale=result.locale,
package_manager="pip",
pip=result.pip,
python=self.python_version(),
source="pip_freeze",
),
python_interpreter=sys.executable,
)
self.assertEqual(expected, result)
def test_filter_pip_freeze_output(self):
raw_stdout = "numpy\npandas\n[notice] A new release of pip is available: 23.1.2 -> 23.3\n\
[notice] To update, run: pip install --upgrade pip"
filtered = filter_pip_freeze_output(raw_stdout)
expected = "numpy\npandas"
self.assertEqual(filtered, expected)
raw_stdout = "numpy\npandas"
filtered = filter_pip_freeze_output(raw_stdout)
expected = "numpy\npandas"
self.assertEqual(filtered, expected)
raw_stdout = "numpy\npandas\nnot at beginning [notice]\n\
[notice] To update, run: pip install --upgrade pip"
filtered = filter_pip_freeze_output(raw_stdout)
expected = "numpy\npandas\nnot at beginning [notice]"
self.assertEqual(filtered, expected)
class WhichPythonTestCase(TestCase):
def test_default(self):
self.assertEqual(which_python(), sys.executable)
def test_none(self):
self.assertEqual(which_python(None), sys.executable)
def test_sys(self):
self.assertEqual(which_python(sys.executable), sys.executable)
def test_does_not_exist(self):
with tempfile.NamedTemporaryFile() as tmpfile:
name = tmpfile.name
with self.assertRaises(RSConnectException):
which_python(name)
def test_is_directory(self):
with tempfile.TemporaryDirectory() as tmpdir:
with self.assertRaises(RSConnectException):
which_python(tmpdir)
@pytest.mark.skipif(sys.platform.startswith("win"), reason="os.X_OK always returns True")
def test_is_not_executable(self):
with tempfile.NamedTemporaryFile() as tmpfile:
with self.assertRaises(RSConnectException):
which_python(tmpfile.name)
class TestPythonVersionRequirements:
def test_pyproject_toml(self):
env = Environment.create_python_environment(os.path.join(TESTDATA, "python-project", "using_pyproject"))
assert env.python_interpreter == sys.executable
assert env.python_version_requirement == ">=3.8"
def test_python_version(self):
env = Environment.create_python_environment(os.path.join(TESTDATA, "python-project", "using_pyversion"))
assert env.python_interpreter == sys.executable
assert env.python_version_requirement == ">=3.8, <3.12"
def test_all_of_them(self):
env = Environment.create_python_environment(os.path.join(TESTDATA, "python-project", "allofthem"))
assert env.python_interpreter == sys.executable
assert env.python_version_requirement == ">=3.8, <3.12"
def test_missing(self):
env = Environment.create_python_environment(os.path.join(TESTDATA, "python-project", "empty"))
assert env.python_interpreter == sys.executable
assert env.python_version_requirement is None
def test_inspect_environment():
environment = Environment._inspect_environment(sys.executable, get_dir("pip1"))
assert environment is not None
assert environment.python != ""
def test_inspect_environment_catches_type_error():
with pytest.raises(RSConnectException) as exec_info:
Environment._inspect_environment(sys.executable, None) # type: ignore
assert isinstance(exec_info.value, RSConnectException)
assert isinstance(exec_info.value.__cause__, TypeError)
@pytest.mark.parametrize(
(
"file_name",
"python",
"force_generate",
"expected_python",
"expected_environment",
),
[
pytest.param(
"path/to/file.py",
sys.executable,
False,
sys.executable,
Environment.from_dict(
dict(
contents=None,
filename="requirements.txt",
locale="en_US.UTF-8",
package_manager="pip",
pip=None,
python=None,
source="pip_freeze",
error=None,
),
python_interpreter=sys.executable,
),
id="basic",
),
pytest.param(
"another/file.py",
os.path.basename(sys.executable),
False,
sys.executable,
Environment.from_dict(
dict(
contents=None,
filename="requirements.txt",
locale="en_US.UTF-8",
package_manager="pip",
pip=None,
python=None,
source="pip_freeze",
error=None,
),
python_interpreter=sys.executable,
),
id="which_python",
),
pytest.param(
"will/the/files/never/stop.py",
"argh.py",
False,
"unused",
Environment.from_dict(
dict(
contents=None,
filename=None,
locale=None,
package_manager=None,
pip=None,
python=None,
source=None,
error="Could not even do things",
)
),
id="exploding",
),
],
)
def test_get_python_env_info(
monkeypatch,
file_name,
python,
force_generate,
expected_python,
expected_environment,
):
def fake_which_python(python, env=os.environ):
return expected_python
def fake_inspect_environment(
python,
directory,
force_generate=False,
check_output=subprocess.check_output,
):
return expected_environment
monkeypatch.setattr(Environment, "_inspect_environment", fake_inspect_environment)
monkeypatch.setattr(rsconnect.environment, "which_python", fake_which_python)
if expected_environment.error is not None:
with pytest.raises(RSConnectException):
_ = Environment._get_python_env_info(file_name, python, force_generate=force_generate)
else:
environment = Environment._get_python_env_info(file_name, python, force_generate=force_generate)
assert environment.python_interpreter == expected_python
assert environment == expected_environment