xonsh/tests
Andy Kipp 21da30e753
Fixed populating the return code for interrupted process. (#5380)
### Motivation

There is annoying behavior when you run command in the loop and can't
interrupt e.g. [this
report](https://github.com/xonsh/xonsh/discussions/5371) and a bit
#5342. After diving into this I see the issue around return code.

### The essence

Basically ``p = subprocess.Popen()`` populates ``p.returncode`` after
``p.wait()``, ``p.poll()`` or ``p.communicate()``
([doc](https://docs.python.org/3/library/os.html#os.waitpid)). But if
you're using `os.waitpid()` BEFORE these functions you're capturing
return code from a signal subsystem and ``p.returncode`` will be ``0``
like success but it's not success. So after ``os.waitid`` call you need
to set return code manually ``p.returncode = -os.WTERMSIG(status)`` like
in Popen. Example:
```xsh
python  # python interactive

import os, signal, subprocess as sp

p = sp.Popen(['sleep', '100'])
p.pid
# 123
p.wait()
# Ctrl+C or `kill -SIGINT 123` from another terminal
p.returncode
# -2

# BUT:

p = sp.Popen(['sleep', '100'])
p.pid
# 123

pid, status = os.waitpid(p.pid, os.WUNTRACED)
# status=2

# From another terminal:
kill -SIGINT 123

p.wait()
# 0
p.returncode
# 0
```

```xsh
from xonsh.tools import describe_waitpid_status
describe_waitpid_status(2)
# WIFEXITED - False - Return True if the process returning status exited via the exit() system call.
# WEXITSTATUS - 0 - Return the process return code from status.
# WIFSIGNALED - True - Return True if the process returning status was terminated by a signal.
# WTERMSIG - 2 - Return the signal that terminated the process that provided the status value.
# WIFSTOPPED - False - Return True if the process returning status was stopped.
# WSTOPSIG - 0 - Return the signal that stopped the process that provided the status value.
```

See also: [Helpful things for
knight](https://github.com/xonsh/xonsh/pull/5361#issuecomment-2078826181).

### Before

```xsh
$RAISE_SUBPROC_ERROR = True

sleep 100
# Ctrl+C
_.rtn
# 0  # It's wrong and RAISE_SUBPROC_ERROR ignored.

for i in range(5):
    print(i)
    sleep 5
# 0
# Ctrl+C  # Can't interrupt
# 1
# 2 
```

### After
```xsh
sleep 100
# Ctrl+C
_.rtn
# -2  # Like in default Popen

$RAISE_SUBPROC_ERROR = True
for i in range(5):
    print(i)
    sleep 5
# 0
# Ctrl+C
# subprocess.CalledProcessError
```

### Notes

* We need to refactor `xonsh.jobs`. It's pretty uncomfortable work with
module.
* The logic is blurry between Specs, Pipelines and Jobs. We need to
bring more clear functions.
* The `captured` variable looks like "just the way of capturing (stdout,
object)" but in fact it affects all logic very much. We need to create
table where we can see the logic difference for every type of capturing.
E.g. in `captured=stdout` mode we use `xonsh.jobs` to monitor the
command but in `captured=object` we avoid this and some logic from
`xonsh.jobs` applied in `stdout` mode but missed in `object` mode. We
need clear map.

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-02 22:10:53 +02:00
..
aliases refactor(aliases): source foreign shell funcs without interactive mode (#5344) 2024-04-18 19:51:39 +05:30
bin style: isort imports 2022-01-31 11:16:51 -05:00
completers fix test_bash_completer for the date command (#5258) 2023-12-31 16:08:28 +01:00
histories Fix tests 2020-09-23 21:15:37 +02:00
man1 improve completers (#4648) 2022-01-27 21:22:36 +05:30
procs Fixed populating the return code for interrupted process. (#5380) 2024-05-02 22:10:53 +02:00
prompt Py 312 pre-liminary support (#5156) 2023-07-04 22:18:37 +05:30
scripts xonsh script.xsh should not fail over 2017-01-11 21:59:42 +08:00
test_lib chore: update tests 2022-03-24 19:49:30 +05:30
xoreutils Py 312 pre-liminary support (#5156) 2023-07-04 22:18:37 +05:30
xpack fix: do not append empty/comment-only input to history (#4822) 2022-05-26 17:50:08 +05:30
.somedotfile dotfile matching 2018-09-12 18:12:02 -04:00
bashrc.sh added tests for foreign shells 2015-10-10 19:52:53 -04:00
batch.bat Add test for sourcing bat file on windows 2016-04-18 00:41:11 +02:00
conftest.py Fix vox env dir (#4736) 2022-03-27 17:12:52 +05:30
run_pwd.xsh more fixes 2016-09-09 00:58:32 -04:00
sample.xsh fixed up dashing 2018-11-08 13:16:44 -05:00
test_aliases.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_ansi_colors.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_ast.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_base_shell.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_builtins.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_cli_utils.py history transfer (#4604) 2021-12-20 08:36:29 +05:30
test_color_tools.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_commands_cache.py commands_cache: ignore mtime check on non-darwin os (#4988) 2022-12-05 21:04:20 +05:30
test_completer.py Py 312 pre-liminary support (#5156) 2023-07-04 22:18:37 +05:30
test_completion_context.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_contexts.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_dirstack.py fix(dirstack): use XSH.env for $HOME check, not os.path (#5211) 2023-09-25 14:53:15 +06:00
test_dirstack_unc.py refactor: update line length to default setting 2024-04-16 11:23:35 -04:00
test_environ.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_events.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_execer.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_foreign_shells.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_history_dummy.py Add XONSH_HISTORY_IGNORE_REGEX support (#4953) 2022-10-05 00:16:42 +05:30
test_history_json.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_history_sqlite.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_imphooks.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_integrations.py feat: add subproc output format, autostrip singleline output (#5377) 2024-05-02 11:48:25 -04:00
test_jobs.py Fix job control for callable aliases (#4901) 2022-08-03 11:37:26 +05:30
test_jsonutils.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_lazyasd.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_lazyjson.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_lexer.py Fix parsing of redirect tokens (#5322) 2024-04-05 13:21:54 +05:30
test_main.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_man.py style: sort imports 2022-03-24 19:49:30 +05:30
test_news.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_parser.py feat: allow square brackets in command arguments (#5326) 2024-04-09 22:33:43 +05:30
test_pipelines.py feat: add subproc output format, autostrip singleline output (#5377) 2024-05-02 11:48:25 -04:00
test_platform.py style: isort imports 2022-01-31 11:16:51 -05:00
test_pretty.py fix: item order of pretty-printed dictionaries (#4786) 2022-04-30 08:25:52 +05:30
test_ptk_completer.py cmd cache refactor - optimize cache usage (#4954) 2022-11-14 23:52:10 +05:30
test_ptk_highlight.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_ptk_history.py refactor: remove xonsh.ptk2 module (#4570) 2021-11-30 08:22:31 -05:00
test_ptk_multiline.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_ptk_shell.py [pre-commit.ci] pre-commit autoupdate (#5148) 2023-05-30 17:10:53 +06:00
test_pyghooks.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_readline_shell.py Add test for readline shell (#4447) 2021-09-02 22:15:20 +03:00
test_shell.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_tools.py update 2024-04-21 17:14:31 +05:30
test_tracer.py pre-commit hook for pyupgrade (#4583) 2021-12-06 14:42:26 -05:00
test_virtualenv_activator.py test: xfail virtualenv plugin 2024-04-20 23:44:26 +05:30
test_wizard.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_xonfig.py refactor: update line length to default setting 2024-04-16 11:23:35 -04:00
test_xonsh.xsh Remove globals (#4280) 2021-05-20 13:14:26 +03:00
test_xontribs.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00