-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathutils.py
More file actions
122 lines (95 loc) · 3.46 KB
/
utils.py
File metadata and controls
122 lines (95 loc) · 3.46 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
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
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
import difflib
import getpass
import inspect
import io
import os
import pprint
import shlex
import sys
from bugzilla import Bugzilla
import bugzilla._cli
import tests
def get_funcname():
# Return calling function name
return inspect.stack()[1][3]
def tests_path(filename):
testdir = os.path.dirname(__file__)
if testdir not in filename:
return os.path.join(testdir, filename)
return filename
def monkeypatch_getpass(monkeypatch):
monkeypatch.setattr(getpass, "getpass", input)
def sanitize_json(rawout):
# py2.7 leaves trailing whitespace after commas. strip it so
# tests pass on both python versions
return "\n".join([line.rstrip() for line in rawout.splitlines()])
def open_functional_bz(bzclass, url, kwargs):
bz = bzclass(url, **kwargs)
if kwargs.get("force_rest", False):
assert bz.is_rest() is True
if kwargs.get("force_xmlrpc", False):
assert bz.is_xmlrpc() is True
# Set a request timeout of 60 seconds
os.environ["PYTHONBUGZILLA_REQUESTS_TIMEOUT"] = "60"
return bz
def open_bz(url, bzclass=Bugzilla, **kwargs):
return open_functional_bz(bzclass=bzclass, url=url, kwargs=kwargs)
def diff_compare(inputdata, filename, expect_out=None):
"""Compare passed string output to contents of filename"""
def _process(data):
if isinstance(data, tuple) and len(data) == 1:
data = data[0]
if isinstance(data, (dict, tuple)):
out = pprint.pformat(data, width=81)
else:
out = str(data)
if not out.endswith("\n"):
out += "\n"
return out
actual_out = _process(inputdata)
if filename:
filename = tests_path(filename)
if not os.path.exists(filename) or tests.CLICONFIG.REGENERATE_OUTPUT:
open(filename, "w").write(actual_out)
expect_out = open(filename).read()
else:
expect_out = _process(expect_out)
diff = "".join(difflib.unified_diff(expect_out.splitlines(1),
actual_out.splitlines(1),
fromfile=filename or "Manual input",
tofile="Generated Output"))
if diff:
raise AssertionError("Conversion outputs did not match.\n%s" % diff)
def do_run_cli(capsys, monkeypatch,
argvstr, bzinstance,
expectfail=False, stdin=None):
"""
Run bin/bugzilla.main() directly with passed argv
"""
argv = shlex.split(argvstr)
monkeypatch.setattr(sys, "argv", argv)
if stdin:
monkeypatch.setattr(sys, "stdin", io.StringIO(stdin))
else:
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
ret = 0
try:
# pylint: disable=protected-access
if bzinstance is None:
bugzilla._cli.cli()
else:
bugzilla._cli.main(unittest_bz_instance=bzinstance)
except SystemExit as sys_e:
ret = sys_e.code
out, err = capsys.readouterr()
outstr = out + err
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))
return outstr