cli: Fake fdopen to work around argcomplete bug with pytest

argcomplete will uncontionally try to take over fd=9 which causes
problems with pytest capturing

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2020-07-13 09:31:40 -04:00
parent 247170c066
commit 4f15e2d6eb

View File

@ -556,6 +556,7 @@ def autocomplete(parser):
return
import argcomplete
import unittest.mock
parsernames = [pclass.cli_flag_name() for pclass in
_get_completer_parsers()]
@ -572,13 +573,22 @@ def autocomplete(parser):
kwargs["output_stream"] = io.BytesIO()
kwargs["exit_method"] = sys.exit
try:
argcomplete.autocomplete(parser, **kwargs)
except SystemExit:
if in_testsuite():
output = kwargs["output_stream"].getvalue().decode("utf-8")
print(output)
raise
# This fdopen hackery is to avoid argcomplete debug_stream behavior
# from taking over an fd that pytest wants to use
fake_fdopen = os.fdopen
if in_testsuite():
def fake_fdopen_cb(*args, **kwargs):
return sys.stderr
fake_fdopen = fake_fdopen_cb
with unittest.mock.patch.object(os, "fdopen", fake_fdopen):
try:
argcomplete.autocomplete(parser, **kwargs)
except SystemExit:
if in_testsuite():
output = kwargs["output_stream"].getvalue().decode("utf-8")
print(output)
raise
###########################