xonsh/tests
Andy Kipp f81d55d09b
Do not load ~/.xonshrc in not interactive mode (#5491)
### Motivation

In #5099 was introduced the logic where all RC files are loaded in
interactive and non-interactive modes. This logic is not working good
for home based `~/.xonshrc` file.

First of all `~/.xonshrc` is the buffer/accumulator of settings focused
on interactive mode. Most tools with integration with xonsh (e.g.
`conda`, `zoxide`, etc) offer to just do `echo "init_tool()" >>
~/.xonshrc` (`conda` has around 20 lines of init code) to start using
the tool and users are doing this without any doubts.

But because of after 5099 `~/.xonshrc` is executed in non-interactive
mode the adding code to it leads to unexpected and unintended side
effects:

* If you run a script with shebang (e.g. `#!/usr/bin/env xonsh` or
[xonsh-awesome-cli-app](https://github.com/anki-code/xonsh-awesome-cli-app))
or just from `xonsh script.xsh` the code will be unexpected and
unintended slower.

* If you're using xonsh-based tools (e.g. you install them using pip)
and run them in environment that has no packages that initiated in
`~/.xonshrc` you will see epic errors.

* Additional context: 
* Bash and Zsh do not load `~/.bashrc` and `~/.zshrc` in non-interactive
mode by the same reasons.
* We have welcome message `Create ~/.xonshrc file manually or use xonfig
to suppress the welcome message` and we don't want to make the process
of creating this file complex.

All of this leads to bad unexpected and unintended experience. This PR
is to fix this.

### Expectation

By doing this fix we assume that experienced user who wants to build
good repeatable run control files will use another ways to create config
files and this has [reflection in
docs](8860f2bd52/docs/xonshrc.rst).
In the nutshell if you want to create the RC files that affect every run
of code you should use one or many of these ways:

* Cross-desktop group (XDG) compliant `~/.config/xonsh/rc.xsh` control
file.
* The system-wide control file `/etc/xonsh/xonshrc` for Linux and OSX
and in `%ALLUSERSPROFILE%\xonsh\xonshrc` on Windows. It controls options
that are applied to all users of Xonsh on a given system.
* The home-based directory `~/.config/xonsh/rc.d/` and system
`/etc/xonsh/rc.d/` can contain .xsh files. They will be executed at
startup in order. This allows for drop-in configuration where your
configuration can be split across scripts and common and local
configurations more easily separated.

In your configs you need to check `$XONSH_INTERACTIVE` and
`$XONSH_LOGIN` explicitly.

### Before

`~/.xonshrc` is used in non-interactive mode.

```xsh
echo "echo RC" >> ~/.xonshrc
cd /tmp
echo "echo Script" > script.xsh
xonsh script.xsh
# RC
# Script
```
```xsh
cd /tmp
echo 'echo RC' >> ~/.xonshrc
echo '#!/usr/bin/env xonsh' > myscript
chmod +x myscript

./myscript
# RC
```

### After

`~/.xonshrc` is not used in non-interactive mode. Use `-i` if you need
it.

```xsh
echo "echo RC" >> ~/.xonshrc
cd /tmp
echo "echo Script" > script.xsh
xonsh script.xsh
# Script

xonsh -i script.xsh
# RC
# Script
```
```xsh
cd /tmp
echo 'echo RC' >> ~/.xonshrc
echo '#!/usr/bin/env xonsh' > myscript
chmod +x myscript

./myscript
```
Closes #5488 #4096 #5496

### Fun

I want to leave here some nice representation of how it works in
bash/sh/zsh from [twitter
post](https://twitter.com/paxx39/status/1742768007154479109):



![image](https://github.com/xonsh/xonsh/assets/1708680/cd7b3803-483f-4d5d-bf9d-baa61c794f68)


## 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-06-13 10:08:23 -04: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 chore: use stable 3.12 for CI jobs (#5307) 2024-05-16 15:43:57 +05:30
histories Fix tests 2020-09-23 21:15:37 +02:00
man1 improve completers (#4648) 2022-01-27 21:22:36 +05:30
procs Tutorial: Callable alias and capturing (#5472) 2024-06-10 09:42:00 -04: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 feat: add superhelp and additional context via new FuncAlias (#5366) 2024-05-13 09:11:58 -04: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 Fix argparser (#5421) 2024-05-16 16:31:05 +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 Perform proper case-insensitive matching in command_cache on Windows (#5477) 2024-06-10 12:29:13 -04:00
test_completer.py Use substring for env completion and better way to sort list (#5388) 2024-05-03 10:30:14 +02:00
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 Added `env.detype_all()` to get all available variables that is possible to detype. (#5431) 2024-05-22 07:53:11 +05:30
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 Revert partial_proxy (#5445) 2024-05-27 18:00:15 +02: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 Do not load ~/.xonshrc in not interactive mode (#5491) 2024-06-13 10:08:23 -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 Do not load ~/.xonshrc in not interactive mode (#5491) 2024-06-13 10:08:23 -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 feat: add superhelp and additional context via new FuncAlias (#5366) 2024-05-13 09:11:58 -04:00
test_tracer.py pre-commit hook for pyupgrade (#4583) 2021-12-06 14:42:26 -05:00
test_virtualenv_activator.py fix: update virtualenv activator (#5419) 2024-05-16 14:29:48 +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