test.py, exec_wrapper.py: improve error handling

This commit is contained in:
The MMGen Project 2023-09-29 12:24:22 +00:00
commit f9ee86a98a
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 20 additions and 11 deletions

View file

@ -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

View file

@ -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' )

View file

@ -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:

View file

@ -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