diff options
| author | Christoph Berg <christoph.berg@credativ.de> | 2022-11-08 11:40:39 +0100 |
|---|---|---|
| committer | Christoph Berg <christoph.berg@credativ.de> | 2022-11-08 11:40:39 +0100 |
| commit | 6f19923a918f7fb63a444b0c99950fdbe9d2f74c (patch) | |
| tree | 326c13aee4c097b57d784572fa0d322146c832a2 /tests/__init__.py | |
| parent | 14581c70e561f9f350a4d5ad86924a5d466028d4 (diff) | |
New upstream version 20221106+py3upstream/20221106+py3
Diffstat (limited to 'tests/__init__.py')
| -rw-r--r-- | tests/__init__.py | 107 |
1 files changed, 101 insertions, 6 deletions
diff --git a/tests/__init__.py b/tests/__init__.py index d04e516..7fc393e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,3 +1,4 @@ +import functools import glob import logging import os @@ -7,6 +8,7 @@ import sys import tempfile import unittest +import pytest import six from chirp import directory @@ -15,6 +17,24 @@ from tests import run_tests LOG = logging.getLogger('testadapter') +PY3_XFAIL = [] + + +def if_enabled(fn): + name = fn.__name__.replace('test_', '') + if 'CHIRP_TESTS' in os.environ: + tests_enabled = os.environ['CHIRP_TESTS'].split(',') + enabled = name in tests_enabled + else: + enabled = True + + def wrapper(*a, **k): + if enabled: + return fn(*a, **k) + else: + raise unittest.SkipTest('%s not enabled' % name) + + return wrapper class TestAdapterMeta(type): @@ -27,6 +47,7 @@ class TestAdapter(unittest.TestCase): SOURCE_IMAGE = None RADIO_INST = None testwrapper = None + XFAIL = False def shortDescription(self): test = self.id().split('.')[-1].replace('test_', '').replace('_', ' ') @@ -48,6 +69,17 @@ class TestAdapter(unittest.TestCase): def tearDownClass(cls): os.remove(cls.testimage) + def setUp(self): + self._out = run_tests.TestOutputANSI() + rid = "%s_%s_" % (self.RADIO_CLASS.VENDOR, self.RADIO_CLASS.MODEL) + rid = rid.replace("/", "_") + basefn, ext = os.path.splitext(self.testimage) + self.testimage = tempfile.mktemp('.%s' % ext, rid) + shutil.copy(self.SOURCE_IMAGE, self.testimage) + + def tearDown(self): + os.remove(self.testimage) + def _runtest(self, test): tw = run_tests.TestWrapper(self.RADIO_CLASS, self.testimage, @@ -65,27 +97,37 @@ class TestAdapter(unittest.TestCase): finally: testcase.cleanup() + @if_enabled def test_copy_all(self): self._runtest(run_tests.TestCaseCopyAll) + @if_enabled def test_brute_force(self): self._runtest(run_tests.TestCaseBruteForce) + @if_enabled def test_edges(self): self._runtest(run_tests.TestCaseEdges) + @if_enabled def test_settings(self): self._runtest(run_tests.TestCaseSettings) + @if_enabled def test_banks(self): self._runtest(run_tests.TestCaseBanks) + @if_enabled def test_detect(self): self._runtest(run_tests.TestCaseDetect) + @if_enabled def test_clone(self): self._runtest(run_tests.TestCaseClone) + def test_py3_expected(self): + assert self.SOURCE_IMAGE not in PY3_XFAIL + def _get_sub_devices(rclass, testimage): try: @@ -93,22 +135,43 @@ def _get_sub_devices(rclass, testimage): except Exception as e: tw = run_tests.TestWrapper(rclass, testimage) - rf = tw.do("get_features") + try: + rf = tw.do("get_features") + except Exception as e: + print('Failed to get features for %s: %s' % (rclass, e)) + # FIXME: If the driver fails to run get_features with no memobj + # we should not arrest the test load. This appears to happen for + # the Puxing777 for some reason, and not all the time. Figure that + # out, but until then, assume crash means "no sub devices". + return [rclass] if rf.has_sub_devices: return tw.do("get_sub_devices") else: return [rclass] +class TestAdapterXFAIL(TestAdapter): + @pytest.mark.xfail(reason=('Driver not expected to run in python3. ' + 'Remove from py3_remaining.txt'), strict=True) + def test_py3_expected(self): + pass + + class RadioSkipper(unittest.TestCase): def test_is_supported_by_environment(self): raise unittest.SkipTest('Running in py3 and driver is not supported') -def load_tests(loader, tests, pattern, suite=None): +def _load_tests(loader, tests, pattern, suite=None): if not suite: suite = unittest.TestSuite() + if 'CHIRP_TESTIMG' in os.environ: + images = os.environ['CHIRP_TESTIMG'].split() + else: + images = glob.glob("tests/images/*.img") + tests = [os.path.splitext(os.path.basename(img))[0] for img in images] + base = os.path.dirname(os.path.abspath(__file__)) base = os.path.join(base, 'images') images = glob.glob(os.path.join(base, "*")) @@ -118,13 +181,29 @@ def load_tests(loader, tests, pattern, suite=None): # This default is meaningless for us pattern = None + py3_remaining = [x.strip() for x in open(os.path.join( + base, '..', 'py3_remaining.txt')).readlines() + if not x.startswith('#')] + for image, test in tests.items(): try: rclass = directory.get_radio(test) + if os.path.basename(image) in py3_remaining: + PY3_XFAIL.append(image) + except Exception: - if six.PY3 and 'CHIRP_DEBUG' in os.environ: + if not six.PY3: + raise + + if 'CHIRP_DEBUG' in os.environ: LOG.error('Failed to load %s' % test) + + if os.path.basename(image) in py3_remaining: + # We expect this to fail to import in py3, so do not + # abort the test run. continue + print('%s not in py3 exclusion list %s...' % ( + os.path.basename(image), ','.join(py3_remaining))) raise for device in _get_sub_devices(rclass, image): class_name = 'TestCase_%s' % ( @@ -135,10 +214,16 @@ def load_tests(loader, tests, pattern, suite=None): else: dst = device device = device.__class__ + + if image in PY3_XFAIL: + cls = TestAdapterXFAIL + else: + cls = TestAdapter + tc = TestAdapterMeta( - class_name, (TestAdapter,), dict(RADIO_CLASS=device, - SOURCE_IMAGE=image, - RADIO_INST=dst)) + class_name, (cls,), dict(RADIO_CLASS=device, + SOURCE_IMAGE=image, + RADIO_INST=dst)) tests = loader.loadTestsFromTestCase(tc) if pattern: @@ -149,3 +234,13 @@ def load_tests(loader, tests, pattern, suite=None): suite.addTests(tests) return suite + + +def load_tests(loader, tests, pattern, suite=None): + try: + return _load_tests(loader, tests, pattern, suite=suite) + except Exception as e: + import traceback + print('Failed to load: %s' % e) + print(traceback.format_exc()) + raise |
