From 4f15e2d6eb323d41cf52a20dd07c7d62c4dea23f Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Mon, 13 Jul 2020 09:31:40 -0400 Subject: [PATCH] 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 --- virtinst/cli.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/virtinst/cli.py b/virtinst/cli.py index a2c869cb2..f9584c277 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -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 ###########################