Skip to content

Commit

Permalink
Merge pull request datalad#7641 from yarikoptic/bf-run-exitcode
Browse files Browse the repository at this point in the history
Exit with the original non-0 exit code if underlying functionality, in particular "datalad run", returned incomplete result record with a non-0 exit code
  • Loading branch information
yarikoptic authored Aug 31, 2024
2 parents b07ea09 + 1d02cd7 commit 19f5b45
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog.d/pr-7641.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### 🐛 Bug Fixes

- Exit with the original non-0 exit code if underlying functionality, in particular "datalad run", returned incomplete result record with a non-0 exit code. Fixes [#7504](https://github.com/datalad/datalad/issues/7504) via [PR #7641](https://github.com/datalad/datalad/pull/7641) (by [@yarikoptic](https://github.com/yarikoptic))
15 changes: 15 additions & 0 deletions datalad/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ def _run_with_exception_handler(cmdlineargs):
# in general we do not want to see the error again, but
# present in debug output
lgr.debug('could not perform all requested actions: %s', ce)
# fish for an exit code. If any of the "failed" results
# is caused by a command error, it will come with an `exit_code`
# property.
exit_codes = [
r['exit_code'] for r in exc.failed if 'exit_code' in r]
if exit_codes:
# we have exit code(s), take the first non-0 one
non0_codes = [e for e in exit_codes if e]
if len(non0_codes) != len(exit_codes):
lgr.debug(
"Among %d incomplete results with exit codes %d had exit code 0",
len(exit_codes),
len(exit_codes) - len(non0_codes))
if non0_codes:
exit_code = non0_codes[0]
elif isinstance(exc, CommandError):
exit_code = _communicate_commanderror(exc) or exit_code
elif isinstance(exc, KeyboardInterrupt):
Expand Down
12 changes: 12 additions & 0 deletions datalad/cli/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ def test_combined_short_option():
assert_in("too few arguments", stderr)


# https://github.com/datalad/datalad/issues/7504
@with_tempfile(mkdir=True)
def test_run_exit_code(tempdir=None):
# datalad run is just an internal command which can readily be used to trigger
# desired situation
create(dataset=tempdir, annex=False)
with chpwd(tempdir): # can't just use -C tempdir since we do "in process" run_main
stdout, stderr = run_main(['run', '--explicit', 'exit 3'], exit_code=3) #, expect_stderr=True)
print(stdout)
print(stderr)


# https://github.com/datalad/datalad/issues/6814
@with_tempfile(mkdir=True)
def test_conflicting_short_option(tempdir=None):
Expand Down

0 comments on commit 19f5b45

Please sign in to comment.