From f0944f840e5e2d63eef4cd46c8d6a29f2a402bb1 Mon Sep 17 00:00:00 2001 From: Foo Date: Sat, 5 Aug 2017 17:13:49 +0300 Subject: [PATCH] Make python tests also print to summary --- tests/modules/__init__.py | 83 +++++++++++++++++++++++++++++++- tests/modules/lib/__init__.py | 63 ------------------------ tests/test_in_vterm/test_tmux.py | 2 +- tests/test_in_vterm/test_vim.py | 2 +- 4 files changed, 83 insertions(+), 67 deletions(-) diff --git a/tests/modules/__init__.py b/tests/modules/__init__.py index 9a961acd..4a7ac971 100644 --- a/tests/modules/__init__.py +++ b/tests/modules/__init__.py @@ -4,8 +4,87 @@ from __future__ import (unicode_literals, division, absolute_import, print_funct import sys if sys.version_info < (2, 7): - from unittest2 import TestCase, main # NOQA + from unittest2 import TestCase as _TestCase # NOQA + from unittest2 import main as _main # NOQA from unittest2.case import SkipTest # NOQA else: - from unittest import TestCase, main # NOQA + from unittest import TestCase as _TestCase # NOQA + from unittest import main as _main # NOQA from unittest.case import SkipTest # NOQA + + +class PowerlineDummyTest(object): + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def fail(self, *args, **kwargs): + pass + + def exception(self, *args, **kwargs): + pass + + +class PowerlineTestSuite(object): + def __init__(self, name): + self.name = name + + def __enter__(self): + self.saved_current_suite = os.environ['POWERLINE_CURRENT_SUITE'] + os.environ['POWERLINE_CURRENT_SUITE'] = ( + self.saved_current_suite + '/' + self.name) + self.suite = self.saved_current_suite + '/' + self.name + return self + + def __exit__(self, exc_type, exc_value, traceback): + if exc_type is not None: + self.exception( + 'suite_noexcept', + 'Exception while running test suite: {0!r}'.format(exc_value), + ) + os.environ['POWERLINE_CURRENT_SUITE'] = self.saved_current_suite + + def record_test_failure(self, fail_char, test_name, message, allow_failure=False): + if allow_failure: + fail_char = 'A' + fail_char + full_msg = '{fail_char} {suite}|{test_name} :: {message}'.format( + fail_char=fail_char, + suite=self.suite, + test_name=test_name, + message=message, + ) + with open(os.environ['FAILURES_FILE'], 'a') as ffd: + ffd.write(full_msg + '\n') + return False + + def exception(self, test_name, message, allow_failure=False): + return self.record_test_failure('E', test_name, message, allow_failure) + + def fail(self, test_name, message, allow_failure=False): + return self.record_test_failure('F', test_name, message, allow_failure) + + def test(self, name, attempts_left=0): + if not attempts_left: + return PowerlineSingleTest(self, name) + else: + return PowerlineDummyTest() + + def subsuite(self, name): + return PowerlineTestSuite(name) + + +suite = None + + +def main(*args, **kwargs): + global suite + suite = PowerlineTestSuite(sys.argv[0]) + _main(*args, **kwargs) + + +class TestCase(_TestCase): + def fail(self, *args, **kwargs): + super(TestCase, self).fail(*args, **kwargs) + suite.fail(self.__class__.__name__, 'Failed test') diff --git a/tests/modules/lib/__init__.py b/tests/modules/lib/__init__.py index d7089acb..408b1773 100644 --- a/tests/modules/lib/__init__.py +++ b/tests/modules/lib/__init__.py @@ -182,66 +182,3 @@ class PowerlineSingleTest(object): def exception(self, message, allow_failure=False): return self.suite.exception(self.name, message, allow_failure) - - -class PowerlineDummyTest(object): - def __enter__(self): - return self - - def __exit__(self, *args): - pass - - def fail(self, *args, **kwargs): - pass - - def exception(self, *args, **kwargs): - pass - - -class PowerlineTestSuite(object): - def __init__(self, name): - self.name = name - - def __enter__(self): - self.saved_current_suite = os.environ['POWERLINE_CURRENT_SUITE'] - os.environ['POWERLINE_CURRENT_SUITE'] = ( - self.saved_current_suite + '/' + self.name) - self.suite = self.saved_current_suite + '/' + self.name - return self - - def __exit__(self, exc_type, exc_value, traceback): - if exc_type is not None: - self.exception( - 'suite_noexcept', - 'Exception while running test suite: {0!r}'.format(exc_value), - ) - os.environ['POWERLINE_CURRENT_SUITE'] = self.saved_current_suite - - def record_test_failure(self, fail_char, test_name, message, allow_failure=False): - if allow_failure: - fail_char = 'A' + fail_char - full_msg = '{fail_char} {suite}|{test_name} :: {message}'.format( - fail_char=fail_char, - suite=self.suite, - test_name=test_name, - message=message, - ) - print('Failed: ' + full_msg) - with open(os.environ['FAILURES_FILE'], 'a') as ffd: - ffd.write(full_msg + '\n') - return False - - def exception(self, test_name, message, allow_failure=False): - return self.record_test_failure('E', test_name, message, allow_failure) - - def fail(self, test_name, message, allow_failure=False): - return self.record_test_failure('F', test_name, message, allow_failure) - - def test(self, name, attempts_left=0): - if not attempts_left: - return PowerlineSingleTest(self, name) - else: - return PowerlineDummyTest() - - def subsuite(self, name): - return PowerlineTestSuite(name) diff --git a/tests/test_in_vterm/test_tmux.py b/tests/test_in_vterm/test_tmux.py index dfccc24e..c1e126bf 100755 --- a/tests/test_in_vterm/test_tmux.py +++ b/tests/test_in_vterm/test_tmux.py @@ -17,7 +17,7 @@ from powerline import get_fallback_logger from tests.modules.lib.terminal import (ExpectProcess, MutableDimensions, do_terminal_tests, get_env) -from tests.modules.lib import PowerlineTestSuite +from tests.modules import PowerlineTestSuite TEST_ROOT = os.path.abspath(os.environ['TEST_ROOT']) diff --git a/tests/test_in_vterm/test_vim.py b/tests/test_in_vterm/test_vim.py index 11da223b..0fbc3190 100755 --- a/tests/test_in_vterm/test_vim.py +++ b/tests/test_in_vterm/test_vim.py @@ -14,7 +14,7 @@ from powerline.lib.dict import updated from tests.modules.lib.terminal import (ExpectProcess, MutableDimensions, do_terminal_tests, get_env) -from tests.modules.lib import PowerlineTestSuite +from tests.modules import PowerlineTestSuite TEST_ROOT = os.path.abspath(os.environ['TEST_ROOT'])