Skip to content

Commit 2142584

Browse files
committed
tests: Add explicit tests for our main() error wrappers
1 parent f11f210 commit 2142584

3 files changed

Lines changed: 32 additions & 12 deletions

File tree

bugzilla/_cli.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,27 +1134,22 @@ def _main(unittest_bz_instance):
11341134

11351135
def main(unittest_bz_instance=None):
11361136
try:
1137-
return _main(unittest_bz_instance)
1138-
except KeyboardInterrupt:
1139-
log.debug("", exc_info=True)
1140-
print("\nExited at user request.")
1141-
sys.exit(1)
1137+
try:
1138+
return _main(unittest_bz_instance)
1139+
except (Exception, KeyboardInterrupt):
1140+
log.debug("", exc_info=True)
1141+
raise
11421142
except (Fault, bugzilla.BugzillaError) as e:
1143-
log.debug("", exc_info=True)
11441143
print("\nServer error: %s" % str(e))
11451144
sys.exit(3)
11461145
except ProtocolError as e:
1147-
log.debug("", exc_info=True)
11481146
print("\nInvalid server response: %d %s" % (e.errcode, e.errmsg))
1149-
# Detect redirect
11501147
redir = (e.headers and 'location' in e.headers)
11511148
if redir:
11521149
print("\nServer was attempting a redirect. Try: "
11531150
" bugzilla --bugzilla %s ..." % redir)
11541151
sys.exit(4)
11551152
except requests.exceptions.SSLError as e:
1156-
log.debug("", exc_info=True)
1157-
11581153
# Give SSL recommendations
11591154
print("SSL error: %s" % e)
11601155
print("\nIf you trust the remote server, you can work "
@@ -1164,6 +1159,13 @@ def main(unittest_bz_instance=None):
11641159
except (socket.error,
11651160
requests.exceptions.HTTPError,
11661161
requests.exceptions.ConnectionError) as e:
1167-
log.debug("", exc_info=True)
11681162
print("\nConnection lost/failed: %s" % str(e))
11691163
sys.exit(2)
1164+
1165+
def cli():
1166+
try:
1167+
main()
1168+
except KeyboardInterrupt:
1169+
log.debug("", exc_info=True)
1170+
print("\nExited at user request.")
1171+
sys.exit(1)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def _parse_requirements(fname):
228228
'Programming Language :: Python :: 3.6',
229229
],
230230
packages = ['bugzilla'],
231-
entry_points={'console_scripts': ['bugzilla = bugzilla._cli:main']},
231+
entry_points={'console_scripts': ['bugzilla = bugzilla._cli:cli']},
232232
data_files=[('share/man/man1', ['bugzilla.1'])],
233233

234234
install_requires=_parse_requirements("requirements.txt"),

tests/ro_functional.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,21 @@ def testActiveComps(self):
352352
out = self.clicomm("info --component_owners 'Virtualization Tools' "
353353
"--active-components")
354354
self.assertTrue("virtinst" not in out)
355+
356+
def testFaults(self):
357+
# Test special error wrappers in bugzilla/_cli.py
358+
bzinstance = Bugzilla(self.url, use_creds=False)
359+
out = tests.clicomm("bugzilla query --field=IDONTEXIST=FOO",
360+
bzinstance, expectfail=True)
361+
self.assertTrue("Server error:" in out)
362+
363+
out = tests.clicomm("bugzilla "
364+
"--bugzilla https://example.com/xmlrpc.cgi "
365+
"query --field=IDONTEXIST=FOO", None, expectfail=True)
366+
self.assertTrue("Connection lost/failed" in out)
367+
368+
out = tests.clicomm("bugzilla "
369+
"--bugzilla https://expired.badssl.com/ "
370+
"query --bug_id 1234", None, expectfail=True)
371+
self.assertTrue(("trust the remote server" in out) and
372+
("--nosslverify" in out))

0 commit comments

Comments
 (0)