forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli.py
283 lines (230 loc) · 9.53 KB
/
test_cli.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
273
274
275
276
277
278
279
280
281
282
283
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
import json
import unittest
import uuid
import os
import stat
from mock import patch
import pytest
from conda.base.constants import on_win
from conda.base.context import context
from conda.common.io import captured
from conda.gateways.disk.delete import rm_rf
from conda.testing.helpers import capture_json_with_argv, run_inprocess_conda_command
from conda.testing.integration import Commands, run_command, make_temp_env, make_temp_prefix
@pytest.mark.usefixtures("tmpdir")
class TestJson(unittest.TestCase):
@pytest.fixture(autouse=True)
def empty_env_tmpdir(self, tmpdir):
# TODO :: Figure out if the pkgcache and a way to look for alternatives until one is found and add
# a warning about it.
"""
# Slightly fancier, "works on my computer", using the last 3 dirs is probably a pytest-ism?
self.tmpdir = os.path.join('opt', 'conda.tmp', *(str(tmpdir).split(os.sep)[-3:]))
try:
try:
rm_rf(self.tmpdir)
except:
pass
os.makedirs(self.tmpdir)
except:
self.tmpdir = str(tmpdir)
"""
self.tmpdir = str(tmpdir)
return self.tmpdir
def assertJsonSuccess(self, res):
self.assertIsInstance(res, dict)
self.assertIn("success", res)
def assertJsonError(self, res):
self.assertIsInstance(res, dict)
self.assertIn("error", res)
def tearDown(self):
rm_rf("tempfile.rc")
def test_config(self):
res = capture_json_with_argv("conda config --get --json")
self.assertJsonSuccess(res)
res = capture_json_with_argv("conda config --get channels --json")
self.assertJsonSuccess(res)
if context.root_writable:
res = capture_json_with_argv("conda config --get channels --system --json")
self.assertJsonSuccess(res)
res = capture_json_with_argv("conda config --get channels --file tempfile.rc --json")
self.assertJsonSuccess(res)
res = capture_json_with_argv(
"conda config --get channels --file tempfile.rc --file tempfile.rc --json"
)
self.assertJsonSuccess(res)
res = capture_json_with_argv("conda config --get use_pip --json")
self.assertJsonSuccess(res)
@pytest.mark.integration
@patch("conda.core.envs_manager.get_user_environments_txt_file", return_value=os.devnull)
def test_info(self, _mocked_guetf):
res = capture_json_with_argv("conda info --json")
keys = (
"channels",
"conda_version",
"default_prefix",
"envs",
"envs_dirs",
"pkgs_dirs",
"platform",
"python_version",
"rc_path",
"root_prefix",
"root_writable",
)
self.assertIsInstance(res, dict)
for key in keys:
assert key in res
res = capture_json_with_argv(
"conda info conda --json", disallow_stderr=False, ignore_stderr=True
)
self.assertIsInstance(res, dict)
self.assertIn("conda", res)
self.assertIsInstance(res["conda"], list)
assert _mocked_guetf.call_count > 0
@pytest.mark.usefixtures("empty_env_tmpdir")
@patch("conda.base.context.mockable_context_envs_dirs")
def test_list(self, mockable_context_envs_dirs):
mockable_context_envs_dirs.return_value = (self.tmpdir,)
res = capture_json_with_argv("conda list --json")
self.assertIsInstance(res, list)
res = capture_json_with_argv("conda list -r --json")
self.assertTrue(isinstance(res, list) or (isinstance(res, dict) and "error" in res))
res = capture_json_with_argv("conda list ipython --json")
self.assertIsInstance(res, list)
stdout, stderr, rc = run_inprocess_conda_command("conda list --name nonexistent --json")
assert json.loads(stdout.strip())["exception_name"] == "EnvironmentLocationNotFound"
assert stderr == ""
assert rc > 0
stdout, stderr, rc = run_inprocess_conda_command(
"conda list --name nonexistent --revisions --json"
)
assert json.loads(stdout.strip())["exception_name"] == "EnvironmentLocationNotFound"
assert stderr == ""
assert rc > 0
assert mockable_context_envs_dirs.call_count > 0
@pytest.mark.usefixtures("empty_env_tmpdir")
@patch("conda.base.context.mockable_context_envs_dirs")
def test_compare(self, mockable_context_envs_dirs):
mockable_context_envs_dirs.return_value = (self.tmpdir,)
stdout, stderr, rc = run_inprocess_conda_command(
"conda compare --name nonexistent tempfile.rc --json"
)
assert json.loads(stdout.strip())["exception_name"] == "EnvironmentLocationNotFound"
assert stderr == ""
assert rc > 0
assert mockable_context_envs_dirs.call_count > 0
@pytest.mark.integration
def test_search_0(self):
with captured():
res = capture_json_with_argv("conda search --json")
self.assertIsInstance(res, dict)
self.assertIsInstance(res["conda"], list)
self.assertIsInstance(res["conda"][0], dict)
keys = ("build", "channel", "fn", "version")
for key in keys:
self.assertIn(key, res["conda"][0])
stdout, stderr, rc = run_inprocess_conda_command("conda search * --json")
assert stderr == ""
assert rc is None
@pytest.mark.integration
def test_search_1(self):
self.assertIsInstance(capture_json_with_argv("conda search ipython --json"), dict)
@pytest.mark.integration
def test_search_2(self):
with make_temp_env() as prefix:
stdout, stderr, _ = run_command(
Commands.SEARCH, prefix, "nose", use_exception_handler=True
)
result = stdout.replace("Loading channels: ...working... done", "")
assert "nose 1.3.7 py37_2 pkgs/main" in result
@pytest.mark.integration
def test_search_3(self):
with make_temp_env() as prefix:
stdout, stderr, _ = run_command(
Commands.SEARCH,
prefix,
"*/linux-64::nose==1.3.7[build=py37_2]",
"--info",
use_exception_handler=True,
)
result = stdout.replace("Loading channels: ...working... done", "")
assert "file name : nose-1.3.7-py37_2" in result
assert "name : nose" in result
assert (
"url : https://repo.anaconda.com/pkgs/main/linux-64/nose-1.3.7-py37_2"
in result
)
# assert "md5 : ff390a1e44d77e54914ca1a2c9e75445" in result
@pytest.mark.integration
def test_search_4(self):
self.assertIsInstance(
capture_json_with_argv("conda search --json --use-index-cache"), dict
)
@pytest.mark.integration
def test_search_5(self):
self.assertIsInstance(
capture_json_with_argv("conda search --platform win-32 --json"), dict
)
def test_run_returns_int():
prefix = make_temp_prefix(name="test")
with make_temp_env(prefix=prefix):
stdout, stderr, result = run_inprocess_conda_command(
"conda run -p {} echo hi".format(prefix)
)
assert isinstance(result, int)
def test_run_returns_zero_errorlevel():
prefix = make_temp_prefix(name="test")
with make_temp_env(prefix=prefix):
stdout, stderr, result = run_inprocess_conda_command(
"conda run -p {} exit 0".format(prefix)
)
assert result == 0
def test_run_returns_nonzero_errorlevel():
prefix = make_temp_prefix(name="test")
with make_temp_env(prefix=prefix) as prefix:
stdout, stderr, result = run_inprocess_conda_command(
'conda run -p "{}" exit 5'.format(prefix)
)
assert result == 5
def test_run_uncaptured(capfd):
prefix = make_temp_prefix(name="test")
with make_temp_env(prefix=prefix):
random_text = uuid.uuid4().hex
stdout, stderr, result = run_inprocess_conda_command(
"conda run -p {} --no-capture-output echo {}".format(prefix, random_text)
)
assert result == 0
# Output is not captured
assert stdout == ""
# Check that the expected output is somewhere between the conda logs
captured = capfd.readouterr()
assert random_text in captured.out
@pytest.mark.skipif(on_win, reason="cannot make readonly env on win")
def test_run_readonly_env(request):
prefix = make_temp_prefix(name="test")
with make_temp_env(prefix=prefix) as prefix:
# Remove write permissions
current = stat.S_IMODE(os.lstat(prefix).st_mode)
os.chmod(prefix, current & ~stat.S_IWRITE)
# reset permissions in case something goes wrong
def reset_permissions():
if os.path.exists(prefix):
os.chmod(prefix, current)
request.addfinalizer(reset_permissions)
# Confirm we do not have write access.
raise_ok = False
try:
open(os.path.join(prefix, "test.txt"), "w+")
except PermissionError:
raise_ok = True
assert raise_ok
stdout, stderr, result = run_inprocess_conda_command(
"conda run -p {} exit 0".format(prefix)
)
# Reset permissions in case all goes according to plan
reset_permissions()
assert result == 0