Repository: xonsh/xonsh

Issue:
Aliases containig logical subprocess operators not handled properly
Consider these three aliases:
```
┌─12-15:40 [confus@confusion /tmp/tmp.w8lR957tJh]                           😱  😱
└$ tail ~/.xonshrc -n8
# Working alias:
aliases['envpy2']='echo "layout python2" > .envrc; direnv allow .'

# Broken alias:
aliases['envpy3']='echo "layout python3" > .envrc && direnv allow .'

# Even more broken alias:
aliases['envnorrr']='echo "layout python2" && echo test'
```
When executing these, only the py2 one behaves as expected:
```
┌─12-15:40 [confus@confusion /tmp/tmp.w8lR957tJh]                           😱  😱
└$ envpy2                                                                                                                                                                        
direnv: loading .envrc
New python executable in /tmp/tmp.w8lR957tJh/.direnv/python-2.7.15+/bin/python2
Also creating executable in /tmp/tmp.w8lR957tJh/.direnv/python-2.7.15+/bin/python
Installing setuptools, pip, wheel...
done.
Running virtualenv with interpreter /usr/bin/python2
direnv: export +VIRTUAL_ENV -PS2 ~PATH

┌─12-15:40 [confus@confusion /tmp/tmp.w8lR957tJh]                           😱  😱
└$ envpy3                                                                                                                                                                        
layout python3 > .envrc anddirenv allow .

┌─12-15:40 [confus@confusion /tmp/tmp.w8lR957tJh]                           😱  😱
└$ envnorrr                                                                                                                                                                      
layout python2 andecho test
```

The others are broken because of  the logical operator between commands. Also stream redirection, which is the difference between `envpy3` and `envnorrr` (note the literal `and` instead of `&&` in alias definition) seems to make them behave differently.

<details>
  <summary>xonsh version and details</summary>

    
    $ xonfig                                                                              
    +------------------+----------------------+
    | xonsh            | 0.9.10               |
    | Git SHA          | 550d70d2             |
    | Commit Date      | May 22 01:42:07 2019 |
    | Python           | 3.6.8                |
    | PLY              | 3.11                 |
    | have readline    | True                 |
    | prompt toolkit   | 2.0.9                |
    | shell type       | prompt_toolkit2      |
    | pygments         | 2.4.0                |
    | on posix         | True                 |
    | on linux         | True                 |
    | distro           | ubuntu               |
    | on darwin        | False                |
    | on windows       | False                |
    | on cygwin        | False                |
    | on msys2         | False                |
    | is superuser     | False                |
    | default encoding | utf-8                |
    | xonsh encoding   | utf-8                |
    | encoding errors  | surrogateescape      |
    +------------------+----------------------+
    
    
    ┌─12-15:40 [confus@confusion /tmp/tmp.w8lR957tJh]                           😱  😱
    └$ alias | grep env
    envpy2=ExecAlias('echo "layout python2" > .envrc; direnv allow .\n', filename='<exec-alias:envpy2>')
    envpy3=ExecAlias('echo "layout python3" > .envrc; direnv allow .\n', filename='<exec-alias:envpy3>')
    envnorrr=['echo', 'layout python2', 'andecho', 'test']
    

</details>

Discussion:

Patch:
diff --git a/news/logical_op_aliases.rst b/news/logical_op_aliases.rst
new file mode 100644
index 00000000..0f0bcd40
--- /dev/null
+++ b/news/logical_op_aliases.rst
@@ -0,0 +1,28 @@
+**Added:**
+
+* <news item>
+
+**Changed:**
+
+* <news item>
+
+**Deprecated:**
+
+* <news item>
+
+**Removed:**
+
+* <news item>
+
+**Fixed:**
+
+* Logical operators in aliases are now executed as expected, e.g.
+  ``aliases['echocat'] = 'echo "hi" and echo "there"'`` will, when run, return
+
+  .. code-block::
+     hi
+     there
+
+**Security:**
+
+* <news item>
diff --git a/xonsh/aliases.py b/xonsh/aliases.py
index 3c5d9ed9..54d818f2 100644
--- a/xonsh/aliases.py
+++ b/xonsh/aliases.py
@@ -44,7 +44,7 @@ import xonsh.xoreutils.which as xxw
 
 @lazyobject
 def SUB_EXEC_ALIAS_RE():
-    return re.compile(r"@\(|\$\(|!\(|\$\[|!\[")
+    return re.compile(r"@\(|\$\(|!\(|\$\[|!\[|\&\&|\|\||and|or")
 
 
 class Aliases(cabc.MutableMapping):


Test patch:
diff --git a/tests/test_aliases.py b/tests/test_aliases.py
index b6020453..0bc2ab89 100644
--- a/tests/test_aliases.py
+++ b/tests/test_aliases.py
@@ -7,7 +7,7 @@ import os
 import pytest
 
 import xonsh.built_ins as built_ins
-from xonsh.aliases import Aliases
+from xonsh.aliases import Aliases, ExecAlias
 from xonsh.environ import Env
 
 from tools import skip_if_on_windows
@@ -113,3 +113,18 @@ def test_recursive_callable_partial_none(xonsh_execer, xonsh_builtins):
     args, obs = alias()
     assert args == "wakka"
     assert len(obs) == 0
+
+@pytest.mark.parametrize(
+    "alias",
+    [
+        "echo 'hi' and echo 'there'",
+        "echo 'hi' or echo 'there'",
+        "echo 'hi' && echo 'there'",
+        "echo 'hi' || echo 'there'",
+        "echo 'hi';  echo 'there'",
+    ],
+)
+def test_subprocess_logical_operators(xonsh_execer, xonsh_builtins, alias):
+    ales = make_aliases()
+    ales["echocat"] = alias
+    assert isinstance(ales["echocat"], ExecAlias)