From bf783fc07419e2b6e405e6ed08793154b0d116f7 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 9 Sep 2025 11:27:04 +0000 Subject: [PATCH] test.include.unit_test: improve altcoin test skipping code --- test/daemontest_d/__init__.py | 7 +++++++ test/include/unit_test.py | 33 ++++++++++++++++++++++++--------- test/modtest_d/__init__.py | 22 ++-------------------- test/modtest_d/cashaddr.py | 2 -- test/modtest_d/devtools.py | 3 +-- test/modtest_d/rune.py | 3 +-- test/modtest_d/xmrseed.py | 2 -- 7 files changed, 35 insertions(+), 37 deletions(-) create mode 100755 test/daemontest_d/__init__.py diff --git a/test/daemontest_d/__init__.py b/test/daemontest_d/__init__.py new file mode 100755 index 00000000..451f0b24 --- /dev/null +++ b/test/daemontest_d/__init__.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python3 + +""" +test.daemontest_d: shared data for daemon tests for the MMGen suite +""" + +altcoin_tests = [] diff --git a/test/include/unit_test.py b/test/include/unit_test.py index c60fae5a..e52c031c 100755 --- a/test/include/unit_test.py +++ b/test/include/unit_test.py @@ -30,7 +30,7 @@ if not os.getenv('MMGEN_DEVTOOLS'): from mmgen.devinit import init_dev init_dev() -from mmgen.cfg import Config, gc +from mmgen.cfg import Config, gc, gv from mmgen.color import gray, brown, orange, yellow, red from mmgen.util import msg, msg_r, gmsg, ymsg, Msg @@ -70,9 +70,6 @@ sys.argv.insert(1, '--skip-cfg-file') cfg = Config(opts_data=opts_data) -if cfg.no_altcoin_deps: - ymsg(f'{gc.prog_name}: skipping altcoin tests by user request') - type(cfg)._reset_ok += ('use_internal_keccak_module', 'debug_addrlist') set_globals(cfg) @@ -90,6 +87,10 @@ all_tests = sorted(fn.removesuffix('.py') for fn in os.listdir(test_dir) if not exclude = cfg.exclude.split(',') if cfg.exclude else [] +if cfg.no_altcoin_deps: + ymsg(f'{gc.prog_name}: skipping altcoin tests by user request') + altcoin_tests = importlib.import_module(f'test.{test_subdir}').altcoin_tests + for e in exclude: if e not in all_tests: die(1, f'{e!r}: invalid parameter for --exclude (no such test)') @@ -114,6 +115,19 @@ if cfg.list_subtests: Msg(fs.format('TEST', 'SUBTESTS') + '\n' + '\n'.join(gen())) sys.exit(0) +def silence(): + if not cfg.verbose: + global stdout_save, stderr_save + stdout_save = sys.stdout + stderr_save = sys.stderr + sys.stdout = sys.stderr = gv.stdout = gv.stderr = open(os.devnull, 'w') + +def end_silence(): + if not cfg.verbose: + global stdout_save, stderr_save + sys.stdout = gv.stdout = stdout_save + sys.stderr = gv.stderr = stderr_save + class UnitTestHelpers: def __init__(self, subtest_name): @@ -152,14 +166,13 @@ class UnitTestHelpers: tests_seen = [] def run_test(test, subtest=None): - mod = importlib.import_module(f'test.{test_subdir}.{test}') def run_subtest(t, subtest): subtest_disp = subtest.replace('_', '-') msg(brown(f'Running {test_type} subtest ') + orange(f'{test}.{subtest_disp}')) if getattr(t, 'silence_output', False): - t._silence() + silence() if hasattr(t, '_pre_subtest'): getattr(t, '_pre_subtest')(test, subtest, UnitTestHelpers(subtest)) @@ -181,14 +194,14 @@ def run_test(test, subtest=None): msg('OK\n' if cfg.verbose else 'OK') except: if getattr(t, 'silence_output', False): - t._end_silence() + end_silence() raise if hasattr(t, '_post_subtest'): getattr(t, '_post_subtest')(test, subtest, UnitTestHelpers(subtest)) if getattr(t, 'silence_output', False): - t._end_silence() + end_silence() if not ret: die(4, f'Unit subtest {subtest_disp!r} failed') @@ -197,10 +210,12 @@ def run_test(test, subtest=None): gmsg(f'Running {test_type} test {test}') tests_seen.append(test) - if cfg.no_altcoin_deps and getattr(mod, 'altcoin_dep', None): + if cfg.no_altcoin_deps and test in altcoin_tests: cfg._util.qmsg(gray(f'Skipping {test_type} test {test!r} [--no-altcoin-deps]')) return + mod = importlib.import_module(f'test.{test_subdir}.{test}') + if hasattr(mod, 'unit_tests'): # new class-based API t = getattr(mod, 'unit_tests')() altcoin_deps = getattr(t, 'altcoin_deps', ()) diff --git a/test/modtest_d/__init__.py b/test/modtest_d/__init__.py index cdeaac5d..bfc4c1d1 100755 --- a/test/modtest_d/__init__.py +++ b/test/modtest_d/__init__.py @@ -1,25 +1,7 @@ #!/usr/bin/env python3 """ -test.modtest_d.__init__: shared data for unit tests for the MMGen suite +test.modtest_d: shared data for unit tests for the MMGen suite """ -import sys, os - -from mmgen.cfg import gv -from ..include.common import cfg - -class unit_tests_base: - - silence_output = False - - def _silence(self): - if not cfg.verbose: - self.stdout_save = sys.stdout - self.stderr_save = sys.stderr - sys.stdout = sys.stderr = gv.stdout = gv.stderr = open(os.devnull, 'w') - - def _end_silence(self): - if not cfg.verbose: - sys.stdout = gv.stdout = self.stdout_save - sys.stderr = gv.stderr = self.stderr_save +altcoin_tests = ['cashaddr', 'rune', 'xmrseed'] diff --git a/test/modtest_d/cashaddr.py b/test/modtest_d/cashaddr.py index 51f0fccd..7e15ee18 100755 --- a/test/modtest_d/cashaddr.py +++ b/test/modtest_d/cashaddr.py @@ -4,8 +4,6 @@ test.modtest_d.cashaddr: unit test for the BCH cashaddr module """ -altcoin_dep = True - from collections import namedtuple from mmgen.proto.bch.cashaddr import cashaddr_parse_addr, cashaddr_decode_addr, cashaddr_encode_addr diff --git a/test/modtest_d/devtools.py b/test/modtest_d/devtools.py index 70689afc..ed5ade67 100755 --- a/test/modtest_d/devtools.py +++ b/test/modtest_d/devtools.py @@ -7,7 +7,6 @@ test.modtest_d.devtools: devtools unit tests for the MMGen suite import os, json from mmgen.util import msg from mmgen.devtools import print_diff, get_ndiff, print_stack_trace, pmsg_r, pmsg, Pmsg -from . import unit_tests_base textA = """ def main(): @@ -50,7 +49,7 @@ def print_hdr(hdr): c = '-' * ((78 - len(hdr))//2 + (len(hdr) % 2)))) # TODO: add data checks -class unit_tests(unit_tests_base): +class unit_tests: silence_output = True diff --git a/test/modtest_d/rune.py b/test/modtest_d/rune.py index 405d91dd..780be57e 100755 --- a/test/modtest_d/rune.py +++ b/test/modtest_d/rune.py @@ -7,7 +7,7 @@ from decimal import Decimal from mmgen.cfg import Config from mmgen.util import pp_fmt, ymsg from mmgen.proto.btc.common import hash160 -from mmgen.proto.cosmos.tx.protobuf import RawTx, SignDoc +from mmgen.proto.cosmos.tx.protobuf import RawTx from mmgen.proto.rune.tx.protobuf import ( RuneTx, build_tx, @@ -21,7 +21,6 @@ from ..include.common import vmsg, silence, end_silence test_cfg = Config({'coin': 'rune', 'test_suite': True}) -altcoin_dep = True _pv = namedtuple('parse_vector', ['fn', 'txid', 'parms', 'null_fee'], defaults=[None]) parse_vectors = [ diff --git a/test/modtest_d/xmrseed.py b/test/modtest_d/xmrseed.py index f7ef1e70..abbedc49 100755 --- a/test/modtest_d/xmrseed.py +++ b/test/modtest_d/xmrseed.py @@ -4,8 +4,6 @@ test.modtest_d.xmrseed: Monero mnemonic unit test for the MMGen suite """ -altcoin_dep = True - from mmgen.util import ymsg from mmgen.xmrseed import xmrseed from ..include.common import cfg, vmsg