Skip to content

Commit 047b837

Browse files
committed
print error message when EasyBuildError was raised in main_with_hooks function
1 parent 6b09872 commit 047b837

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

easybuild/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ def main_with_hooks(args=None):
754754
main(args=args, prepared_cfg_data=(init_session_state, eb_go, cfg_settings))
755755
except EasyBuildError as err:
756756
run_hook(FAIL, hooks, args=[err])
757-
sys.exit(1)
757+
print_error(err.msg, exit_on_error=True, exit_code=1)
758758
except KeyboardInterrupt as err:
759759
run_hook(CANCEL, hooks, args=[err])
760760
print_error("Cancelled by user: %s" % err)

easybuild/tools/build_log.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,18 @@ def print_error(msg, *args, **kwargs):
335335
if args:
336336
msg = msg % args
337337

338+
# grab exit code, if specified;
339+
# also consider deprecated 'exitCode' option
340+
exitCode = kwargs.pop('exitCode', None)
341+
exit_code = kwargs.pop('exit_code', exitCode)
342+
if exitCode is not None:
343+
_init_easybuildlog.deprecated("'exitCode' option in print_error function is replaced with 'exit_code'", '6.0')
344+
345+
# use 1 as defaut exit code
346+
if exit_code is None:
347+
exit_code = 1
348+
338349
log = kwargs.pop('log', None)
339-
exitCode = kwargs.pop('exitCode', 1)
340350
opt_parser = kwargs.pop('opt_parser', None)
341351
exit_on_error = kwargs.pop('exit_on_error', True)
342352
silent = kwargs.pop('silent', False)
@@ -348,7 +358,7 @@ def print_error(msg, *args, **kwargs):
348358
if opt_parser:
349359
opt_parser.print_shorthelp()
350360
sys.stderr.write("ERROR: %s\n" % msg)
351-
sys.exit(exitCode)
361+
sys.exit(exit_code)
352362
elif log is not None:
353363
raise EasyBuildError(msg)
354364

test/framework/toy_build.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,6 +4104,29 @@ def pre_configure_hook(self, *args, **kwargs):
41044104
stderr = stderr.getvalue()
41054105
self.assertTrue(regex.search(stderr), f"Pattern '{regex.pattern}' should be found in {stderr}")
41064106

4107+
def test_eb_error(self):
4108+
"""
4109+
Test whether main function as run by 'eb' command print error messages to stderr.
4110+
"""
4111+
topdir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
4112+
toy_ec = os.path.join(topdir, 'test', 'framework', 'easyconfigs', 'test_ecs', 't', 'toy', 'toy-0.0.eb')
4113+
4114+
test_ec = os.path.join(self.test_prefix, 'test.eb')
4115+
test_ec_txt = read_file(toy_ec)
4116+
test_ec_txt += "\ndependencies = [('nosuchdep', '1.0')]"
4117+
write_file(test_ec, test_ec_txt)
4118+
4119+
with self.mocked_stdout_stderr() as (_, stderr):
4120+
cleanup()
4121+
try:
4122+
main_with_hooks(args=[test_ec, '--robot', '--force'])
4123+
except SystemExit:
4124+
pass
4125+
4126+
regex = re.compile("^ERROR: Missing dependencies", re.M)
4127+
stderr = stderr.getvalue()
4128+
self.assertTrue(regex.search(stderr), f"Pattern '{regex.pattern}' should be found in {stderr}")
4129+
41074130

41084131
def suite():
41094132
""" return all the tests in this file """

0 commit comments

Comments
 (0)