Browse Source

test suite: improve initialization of `sys.path` and `repo_root`

The MMGen Project 1 year ago
parent
commit
6a21044127

+ 1 - 5
scripts/exec_wrapper.py

@@ -27,11 +27,7 @@ def exec_wrapper_init():
 
 	if exec_wrapper_os.path.dirname(exec_wrapper_sys.argv[1]) == 'test':
 		# support running of test scripts under wrapper
-		cwd = exec_wrapper_os.getcwd() # assume we’re in repo root
-		exec_wrapper_sys.path[0] = cwd
-		exec_wrapper_sys.path[1] = exec_wrapper_os.path.join(cwd,'test')
-		from test.overlay import get_overlay_tree_dir
-		exec_wrapper_sys.path.insert(0, get_overlay_tree_dir(cwd))
+		exec_wrapper_sys.path[0] = exec_wrapper_os.getcwd() # assume we’re in repo root
 	else:
 		exec_wrapper_sys.path.pop(0)
 

+ 4 - 2
test/cmdtest.py

@@ -88,8 +88,10 @@ if sys.argv[-1] == 'clean':
 	print(f'Removed {os.path.relpath(overlay_tree_dir)!r}')
 else:
 	# overlay must be set up before importing mmgen mods!
-	import include.test_init
-	repo_root = include.test_init.repo_root
+	try:
+		from include.test_init import repo_root
+	except ImportError:
+		from test.include.test_init import repo_root
 
 from mmgen.cfg import Config,gc
 from mmgen.color import red,yellow,green,blue,cyan,nocolor,init_color

+ 4 - 1
test/colortest.py

@@ -9,7 +9,10 @@ test/colortest.py: test color handling for the MMGen suite
 
 import os
 
-import include.test_init
+try:
+	from include import test_init
+except ImportError:
+	from test.include import test_init
 
 from mmgen.color import *
 from mmgen.util import msg,ymsg,gmsg

+ 4 - 1
test/gentest.py

@@ -22,7 +22,10 @@ test/gentest.py: Cryptocoin key/address generation tests for the MMGen suite
 
 import sys,os,time
 
-import include.test_init
+try:
+	from include import test_init
+except ImportError:
+	from test.include import test_init
 
 # Import these _after_ local path's been added to sys.path
 from mmgen.cfg import gc,Config

+ 6 - 1
test/hashfunc.py

@@ -21,7 +21,12 @@ test/hashfunc.py: Test internal implementations of SHA256, SHA512 and Keccak256
 """
 
 import sys
-import include.test_init
+
+try:
+	from include import test_init
+except ImportError:
+	from test.include import test_init
+
 from mmgen.util import die
 
 assert len(sys.argv) in (2,3),"Test takes 1 or 2 arguments: test name, plus optional rounds count"

+ 2 - 1
test/include/test_init.py

@@ -13,8 +13,9 @@ test.include.test_init: Initialization module for test scripts
 """
 
 import sys,os
+from pathlib import PurePath
 os.environ['MMGEN_TEST_SUITE'] = '1'
-repo_root = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),os.pardir)))
+repo_root = str(PurePath(*PurePath(sys.modules[__name__].__file__).parts[:-3]))
 os.chdir(repo_root)
 sys.path[0] = repo_root
 

+ 4 - 1
test/objattrtest.py

@@ -24,7 +24,10 @@ test/objattrtest.py: Test immutable attributes of MMGen data objects
 
 from collections import namedtuple
 
-import include.test_init
+try:
+	from include import test_init
+except ImportError:
+	from test.include import test_init
 
 from mmgen.cfg import Config
 from mmgen.util import msg,msg_r,gmsg,die

+ 4 - 1
test/objtest.py

@@ -22,7 +22,10 @@ test/objtest.py: Test MMGen data objects
 
 import os,re
 
-import include.test_init
+try:
+	from include import test_init
+except ImportError:
+	from test.include import test_init
 
 # for objtest, violate MMGen Project best practices and allow use of the dev tools
 # in production code:

+ 4 - 1
test/scrambletest.py

@@ -25,7 +25,10 @@ import sys,os,time
 from subprocess import run,PIPE
 from collections import namedtuple
 
-import include.test_init
+try:
+	from include import test_init
+except ImportError:
+	from test.include import test_init
 
 from mmgen.cfg import Config
 from mmgen.util import msg,msg_r,bmsg,die

+ 4 - 1
test/start-coin-daemons.py

@@ -4,4 +4,7 @@
 test/start-coin-daemons.py: Start daemons for the MMGen test suite
 """
 
-import include.coin_daemon_control
+try:
+	import include.coin_daemon_control
+except ImportError:
+	import test.include.coin_daemon_control

+ 4 - 1
test/stop-coin-daemons.py

@@ -4,4 +4,7 @@
 test/stop-coin-daemons.py: Stop daemons for the MMGen test suite
 """
 
-import include.coin_daemon_control
+try:
+	import include.coin_daemon_control
+except ImportError:
+	import test.include.coin_daemon_control

+ 5 - 2
test/tooltest.py

@@ -23,7 +23,10 @@ test/tooltest.py: Tests for the 'mmgen-tool' utility
 import sys,os,time
 from subprocess import run,PIPE
 
-import include.test_init
+try:
+	from include.test_init import repo_root
+except ImportError:
+	from test.include.test_init import repo_root
 
 from mmgen.cfg import Config
 from mmgen.color import red,yellow,green,blue,cyan
@@ -147,7 +150,7 @@ tn_ext = ('','.testnet')[proto.testnet]
 
 spawn_cmd = [
 	'scripts/exec_wrapper.py',
-	os.path.relpath(os.path.join(include.test_init.repo_root,'cmds','mmgen-tool')) ]
+	os.path.relpath(os.path.join(repo_root,'cmds','mmgen-tool')) ]
 
 if cfg.coverage:
 	d,f = init_coverage()

+ 4 - 1
test/tooltest2.py

@@ -27,7 +27,10 @@ import sys,os,time,importlib
 from subprocess import run,PIPE
 from decimal import Decimal
 
-import include.test_init
+try:
+	from include import test_init
+except ImportError:
+	from test.include import test_init
 
 from test.include.common import set_globals,end_msg,sample_text,init_coverage
 

+ 5 - 2
test/unit_tests.py

@@ -22,7 +22,10 @@ test/unit_tests.py: Unit tests for the MMGen suite
 
 import sys,os,time,importlib,platform
 
-import include.test_init
+try:
+	from include.test_init import repo_root
+except ImportError:
+	from test.include.test_init import repo_root
 
 # for the unit tests, violate MMGen Project best practices and allow use of the dev tools
 # in production code:
@@ -72,7 +75,7 @@ set_globals(cfg)
 
 file_pfx = 'ut_'
 
-tests_d = os.path.join(include.test_init.repo_root,'test','unit_tests_d')
+tests_d = os.path.join(repo_root,'test','unit_tests_d')
 
 all_tests = sorted(fn[len(file_pfx):-len('.py')] for fn in os.listdir(tests_d) if fn.startswith(file_pfx))