Browse Source

test.include.unit_test: improve altcoin test skipping code

The MMGen Project 3 months ago
parent
commit
bf783fc074

+ 7 - 0
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 = []

+ 24 - 9
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', ())

+ 2 - 20
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']

+ 0 - 2
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

+ 1 - 2
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
 

+ 1 - 2
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 = [

+ 0 - 2
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