Browse Source

test.py, exec_wrapper.py: improve error handling

The MMGen Project 1 year ago
parent
commit
f9ee86a98a
4 changed files with 20 additions and 11 deletions
  1. 1 0
      mmgen/exception.py
  2. 9 2
      scripts/exec_wrapper.py
  3. 1 1
      test/include/pexpect.py
  4. 9 8
      test/test.py

+ 1 - 0
mmgen/exception.py

@@ -51,6 +51,7 @@ class UserOptError(Exception):            mmcode = 1
 class NoLEDSupport(Exception):            mmcode = 1
 class MsgFileFailedSID(Exception):        mmcode = 1
 class TestSuiteException(Exception):      mmcode = 1
+class TestSuiteSpawnedScriptException(Exception): mmcode = 1
 
 # 2: yellow hl, message only
 class InvalidTokenAddress(Exception):     mmcode = 2

+ 9 - 2
scripts/exec_wrapper.py

@@ -40,11 +40,14 @@ def exec_wrapper_write_traceback(e,exit_val):
 
 	if os.getenv('EXEC_WRAPPER_TRACEBACK'):
 		import traceback
+		cwd = os.getcwd()
+		sys.path.insert(0,cwd)
+		from test.overlay import get_overlay_tree_dir
+		overlay_path_pfx = os.path.relpath(get_overlay_tree_dir(cwd)) + '/'
 
-		cwd = os.path.abspath('.')
 		def fixup_fn(fn_in):
 			from mmgen.util2 import removeprefix,removesuffix
-			fn = removeprefix(removeprefix(fn_in,cwd+'/'),'test/overlay/tree/')
+			fn = removeprefix(removeprefix(fn_in,cwd+'/'),overlay_path_pfx)
 			return removesuffix(fn,'_orig.py') + '.py' if fn.endswith('_orig.py') else fn
 			# Python 3.9:
 			# fn = fn_in.removeprefix(cwd+'/').removeprefix('test/overlay/tree/')
@@ -69,6 +72,10 @@ def exec_wrapper_write_traceback(e,exit_val):
 		from test.include.common import test_py_error_fn
 		with open(test_py_error_fn,'w') as fp:
 			fp.write('\n'.join(tb_lines + [exc_line]))
+
+		print(c.blue('{} script exited with error').format(
+			'Test' if os.path.dirname(sys.argv[0]) == 'test' else 'Spawned' ))
+
 	else:
 		sys.stdout.write( c.purple((f'NONZERO_EXIT[{exit_val}]: ' if exit_val else '') + exc_line) + '\n' )
 

+ 1 - 1
test/include/pexpect.py

@@ -92,7 +92,7 @@ class MMGenPexpect:
 		self.p.read()
 		ret = self.p.wait()
 		if ret != self.req_exit_val and not cfg.coverage:
-			die(1,red(f'test.py: spawned program exited with value {ret}'))
+			die( 'TestSuiteSpawnedScriptException', f'Spawned script exited with value {ret}' )
 		if cfg.profile:
 			return
 		if not self.skip_ok:

+ 9 - 8
test/test.py

@@ -1001,7 +1001,7 @@ elif cmd_args and cmd_args[0] in utils:
 if cfg.pause:
 	set_restore_term_at_exit()
 
-from mmgen.exception import TestSuiteException,TestSuiteFatalException
+from mmgen.exception import TestSuiteException,TestSuiteFatalException,TestSuiteSpawnedScriptException
 
 try:
 	tr = TestSuiteRunner(data_dir,trash_dir)
@@ -1018,13 +1018,14 @@ except TestSuiteException as e:
 	die(2,e.args[0])
 except TestSuiteFatalException as e:
 	die(4,e.args[0])
+except TestSuiteSpawnedScriptException as e:
+	# if spawned script is not running under exec_wrapper, output brief error msg:
+	if os.getenv('MMGEN_EXEC_WRAPPER'):
+		Msg(red(str(e)))
+		Msg(blue('test.py: spawned script exited with error'))
 except Exception:
-	if os.getenv('MMGEN_EXEC_WRAPPER'): # test.py itself is running under exec_wrapper
-		import traceback
-		print(''.join(traceback.format_exception(*sys.exc_info())))
-		msg(blue('Test script exited with error'))
-	else:
-		msg(blue('Spawned script exited with error'))
-	raise
+	# if test.py itself is running under exec_wrapper, re-raise so exec_wrapper can handle exception:
+	if os.getenv('MMGEN_EXEC_WRAPPER'):
+		raise
 except:
 	raise