Skip to content

Commit

Permalink
aliases: Create ExecAlias for aliases that contain IO operators
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-shimon committed Oct 23, 2020
1 parent a68b9cc commit cf87c2f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
23 changes: 23 additions & 0 deletions news/exec-aliases-on-io-operators.rst
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Setting an alias with IO redirections (e.g ``ls | wc``) now works correctly.

**Security:**

* <news item>
17 changes: 17 additions & 0 deletions tests/test_aliases.py
Expand Up @@ -128,3 +128,20 @@ def test_subprocess_logical_operators(xonsh_execer, xonsh_builtins, alias):
ales = make_aliases()
ales["echocat"] = alias
assert isinstance(ales["echocat"], ExecAlias)


@pytest.mark.parametrize(
"alias",
[
"echo 'hi' | grep h",
"echo 'hi' > file",
"cat < file",
"COMMAND1 e>o < input.txt | COMMAND2 > output.txt e>> errors.txt",
"echo 'h|i' | grep h",
"echo 'h|i << x > 3' | grep x",
],
)
def test_subprocess_io_operators(xonsh_execer, xonsh_builtins, alias):
ales = make_aliases()
ales["echocat"] = alias
assert isinstance(ales["echocat"], ExecAlias)
8 changes: 4 additions & 4 deletions xonsh/aliases.py
Expand Up @@ -48,8 +48,8 @@


@lazyobject
def SUB_EXEC_ALIAS_RE():
return re.compile(r"@\(|\$\(|!\(|\$\[|!\[|\&\&|\|\||\s+and\s+|\s+or\s+")
def EXEC_ALIAS_RE():
return re.compile(r"@\(|\$\(|!\(|\$\[|!\[|\&\&|\|\||\s+and\s+|\s+or\s+|[>|<]")


class Aliases(cabc.MutableMapping):
Expand Down Expand Up @@ -128,8 +128,8 @@ def __getitem__(self, key):
def __setitem__(self, key, val):
if isinstance(val, str):
f = "<exec-alias:" + key + ">"
if SUB_EXEC_ALIAS_RE.search(val) is not None:
# We have a sub-command, e.g. $(cmd), to evaluate
if EXEC_ALIAS_RE.search(val) is not None:
# We have a sub-command (e.g. $(cmd)) or IO redirect (e.g. >>)
self._raw[key] = ExecAlias(val, filename=f)
elif isexpression(val):
# expansion substitution
Expand Down

0 comments on commit cf87c2f

Please sign in to comment.