forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
115 lines (97 loc) · 2.83 KB
/
helpers.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
"""
Helpers for the tests
"""
import subprocess
import sys
import os
import json
try:
from unittest import mock
except ImportError:
try:
import mock
except ImportError:
mock = None
from contextlib import contextmanager
import conda.cli as cli
from conda.compat import StringIO
def raises(exception, func, string=None):
try:
a = func()
except exception as e:
if string:
assert string in e.args[0]
print(e)
return True
raise Exception("did not raise, gave %s" % a)
def run_in(command, shell='bash'):
p = subprocess.Popen([shell, '-c', command], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
return (stdout.decode('utf-8').replace('\r\n', '\n'),
stderr.decode('utf-8').replace('\r\n', '\n'))
python = sys.executable
conda = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'bin', 'conda')
def run_conda_command(*args):
env = os.environ.copy()
# Make sure bin/conda imports *this* conda.
env['PYTHONPATH'] = os.path.dirname(os.path.dirname(__file__))
env['CONDARC'] = ' '
p= subprocess.Popen((python, conda,) + args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=env)
stdout, stderr = p.communicate()
return (stdout.decode('utf-8').replace('\r\n', '\n'),
stderr.decode('utf-8').replace('\r\n', '\n'))
class CapturedText(object):
pass
@contextmanager
def captured(disallow_stderr=True):
"""
Context manager to capture the printed output of the code in the with block
Bind the context manager to a variable using `as` and the result will be
in the stdout property.
>>> from tests.helpers import capture
>>> with captured() as c:
... print('hello world!')
...
>>> c.stdout
'hello world!\n'
"""
import sys
stdout = sys.stdout
stderr = sys.stderr
sys.stdout = outfile = StringIO()
sys.stderr = errfile = StringIO()
c = CapturedText()
yield c
c.stdout = outfile.getvalue()
c.stderr = errfile.getvalue()
sys.stdout = stdout
sys.stderr = stderr
if disallow_stderr and c.stderr:
raise Exception("Got stderr output: %s" % c.stderr)
def capture_with_argv(*argv):
sys.argv = argv
stdout, stderr = StringIO(), StringIO()
oldstdout, oldstderr = sys.stdout, sys.stderr
sys.stdout = stdout
sys.stderr = stderr
try:
cli.main()
except SystemExit:
pass
sys.stdout = oldstdout
sys.stderr = oldstderr
stdout.seek(0)
stderr.seek(0)
return stdout.read(), stderr.read()
def capture_json_with_argv(*argv):
stdout, stderr = capture_with_argv(*argv)
if stderr:
# TODO should be exception
return stderr
try:
return json.loads(stdout)
except ValueError:
print(stdout, stderr)
raise