forked from umeshrege/python-bugzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
90 lines (70 loc) · 2.19 KB
/
Copy path__init__.py
File metadata and controls
90 lines (70 loc) · 2.19 KB
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
from __future__ import print_function
import os
import shlex
import sys
# pylint: disable=import-error
if sys.version_info[0] >= 3:
from io import StringIO
else:
from StringIO import StringIO
# pylint: enable=import-error
from bugzilla import Bugzilla, RHBugzilla, _cli
class _CLICONFIG(object):
def __init__(self):
self.REDHAT_URL = None
CLICONFIG = _CLICONFIG()
os.environ["__BUGZILLA_UNITTEST"] = "1"
def make_bz(version, *args, **kwargs):
cls = Bugzilla
if kwargs.pop("rhbz", False):
cls = RHBugzilla
if "cookiefile" not in kwargs and "tokenfile" not in kwargs:
kwargs["use_creds"] = False
if "url" not in kwargs:
kwargs["url"] = None
bz = cls(*args, **kwargs)
bz._set_bz_version(version) # pylint: disable=protected-access
return bz
def clicomm(argvstr, bzinstance,
returnmain=False, stdin=None, expectfail=False):
"""
Run bin/bugzilla.main() directly with passed argv
"""
argv = shlex.split(argvstr)
oldstdout = sys.stdout
oldstderr = sys.stderr
oldstdin = sys.stdin
oldargv = sys.argv
try:
out_io = StringIO()
sys.stdout = out_io
sys.stderr = out_io
if stdin:
sys.stdin = stdin
sys.argv = argv
ret = 0
test_return = None
try:
print(" ".join(argv))
print()
test_return = _cli.main(unittest_bz_instance=bzinstance)
except SystemExit as sys_e:
ret = sys_e.code
outstr = out_io.getvalue()
if outstr.endswith("\n"):
outstr = outstr[:-1]
if ret != 0 and not expectfail:
raise RuntimeError("Command failed with %d\ncmd=%s\nout=%s" %
(ret, argvstr, outstr))
if ret == 0 and expectfail:
raise RuntimeError("Command succeeded but we expected success\n"
"ret=%d\ncmd=%s\nout=%s" %
(ret, argvstr, outstr))
if returnmain:
return test_return
return outstr
finally:
sys.stdout = oldstdout
sys.stderr = oldstderr
sys.stdin = oldstdin
sys.argv = oldargv