1# DExTer : Debugging Experience Tester 2# ~~~~~~ ~ ~~ ~ ~~ 3# 4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5# See https://llvm.org/LICENSE.txt for license information. 6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7"""Unit test harness.""" 8 9from fnmatch import fnmatch 10import os 11import unittest 12 13from io import StringIO 14 15from dex.utils import is_native_windows, has_pywin32 16from dex.utils import PreserveAutoColors, PrettyOutput 17from dex.utils import Timer 18 19 20class DexTestLoader(unittest.TestLoader): 21 def _match_path(self, path, full_path, pattern): 22 """Don't try to import platform-specific modules for the wrong platform 23 during test discovery. 24 """ 25 d = os.path.basename(os.path.dirname(full_path)) 26 if is_native_windows(): 27 if d == 'posix': 28 return False 29 if d == 'windows': 30 return has_pywin32() 31 else: 32 if d == 'windows': 33 return False 34 elif d == 'dbgeng': 35 return False 36 return fnmatch(path, pattern) 37 38 39def unit_tests_ok(context): 40 unittest.TestCase.maxDiff = None # remove size limit from diff output. 41 42 with Timer('unit tests'): 43 suite = DexTestLoader().discover( 44 context.root_directory, pattern='*.py') 45 stream = StringIO() 46 result = unittest.TextTestRunner(verbosity=2, stream=stream).run(suite) 47 48 ok = result.wasSuccessful() 49 if not ok or context.options.unittest == 'show-all': 50 with PreserveAutoColors(context.o): 51 context.o.auto_reds.extend( 52 [r'FAIL(ED|\:)', r'\.\.\.\s(FAIL|ERROR)$']) 53 context.o.auto_greens.extend([r'^OK$', r'\.\.\.\sok$']) 54 context.o.auto_blues.extend([r'^Ran \d+ test']) 55 context.o.default('\n') 56 for line in stream.getvalue().splitlines(True): 57 context.o.auto(line, stream=PrettyOutput.stderr) 58 59 return ok 60 61 62class TestUnitTests(unittest.TestCase): 63 def test_sanity(self): 64 self.assertEqual(1, 1) 65