From 8ecf00bd0e91bf5705446a5953eef20f4477bcdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Mon, 23 Sep 2019 03:58:33 +0200 Subject: [PATCH] tests: add helpful decorator to wait before test cleanup Allow to manual inspect test environment after test fails. This is similar to --do-not-clean option we had in R3.2. The decorator should be used only while debugging and should never be applied to the code committed into repository. --- qubes/tests/__init__.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/qubes/tests/__init__.py b/qubes/tests/__init__.py index d45943a7..334f85e2 100644 --- a/qubes/tests/__init__.py +++ b/qubes/tests/__init__.py @@ -220,6 +220,34 @@ def expectedFailureIfTemplate(templates): return wrapper return decorator + +def wait_on_fail(func): + """Test decorator for debugging. It pause test execution on failure and wait + for user input. It's useful to manually inspect system state just after test + fails, before executing any cleanup. + + Usage: decorate a test you are debugging. + DO IT ONLY TEMPORARILY, DO NOT COMMIT! + """ + + @functools.wraps(func) + def wrapper(self, *args, **kwargs): + try: + func(self, *args, **kwargs) + except: + print('FAIL\n') + traceback.print_exc() + print('Press return to continue:', end='') + sys.stdout.flush() + reader = asyncio.StreamReader(loop=self.loop) + transport, protocol = self.loop.run_until_complete( + self.loop.connect_read_pipe(lambda: asyncio.StreamReaderProtocol(reader), + sys.stdin)) + self.loop.run_until_complete(reader.readline()) + raise + return wrapper + + class _AssertNotRaisesContext(object): """A context manager used to implement TestCase.assertNotRaises methods.