more tests, news and docs

This commit is contained in:
Anthony Scopatz 2018-09-17 18:23:20 -04:00
parent 9c2a756b68
commit f0c3a4b1cd
6 changed files with 79 additions and 5 deletions

View file

@ -526,6 +526,28 @@ feed them to a subprocess as needed. For example:
for i in range(20):
$[touch @('file%02d' % i)]
The ``@()`` syntax may also be used inside of subprocess
arguments, not just as a stand-alone argument. For example:
.. code-block:: xonshcon
>>> x = 'hello'
>>> echo /path/to/@(x)
/path/to/hello
When used inside of a subprocess argument and ``<expr>`` evaluates to a
non-string iterable, ``@()`` will expand to the outer product of all
given values:
.. code-block:: sh
>>> echo /path/to/@(['hello', 'world'])
/path/to/hello /path/to/world
>>> echo @(['a', 'b']):@('x', 'y')
a:x a:y b:x b:y
Command Substitution with ``@$()``
==================================

33
news/atin.rst Normal file
View file

@ -0,0 +1,33 @@
**Added:**
* The python-mode ``@(expr)`` syntax may now be used inside of subprocess
arguments, not just as a stand-alone argument. For example:
.. code-block:: sh
$ x = 'hello'
$ echo /path/to/@(x)
/path/to/hello
This syntax will even properly expand to the outer product if the ``expr``
is a list (or other non-string iterable) of values:
.. code-block:: sh
$ echo /path/to/@(['hello', 'world'])
/path/to/hello /path/to/world
$ echo @(['a', 'b']):@('x', 'y')
a:x a:y b:x b:y
Previously this was not possible.
**Changed:** None
**Deprecated:** None
**Removed:** None
**Fixed:** None
**Security:** None

View file

@ -17,6 +17,7 @@ from xonsh.built_ins import (
superhelper,
ensure_list_of_strs,
list_of_strs_or_callables,
list_of_list_of_strs_outer_product,
regexsearch,
globsearch,
expand_path,
@ -142,6 +143,19 @@ def test_list_of_strs_or_callables(exp, inp):
assert exp == obs
@pytest.mark.parametrize(
"inp, exp",
[
(["x", ["y", "z"]], ["xy", "xz"]),
(["x", ["y", "z"], ["a"]], ["xya", "xza"]),
([["y", "z"], ["a", "b"]], ["ya", "yb", "za", "zb"]),
],
)
def test_list_of_list_of_strs_outer_product(xonsh_builtins, inp, exp):
obs = list_of_list_of_strs_outer_product(inp)
assert exp == obs
@pytest.mark.parametrize(
"s",
[

View file

@ -2063,6 +2063,7 @@ def test_nested_madness():
def test_atparens_intoken():
check_xonsh_ast({}, "![echo /x/@(y)/z]", False)
def test_ls_dot_nesting():
check_xonsh_ast({}, '$(ls @(None or "."))', False)

View file

@ -977,8 +977,8 @@ def list_of_list_of_strs_outer_product(x):
lolos = map(ensure_list_of_strs, x)
rtn = []
for los in itertools.product(*lolos):
s = ''.join(los)
if '*' in s:
s = "".join(los)
if "*" in s:
rtn.extend(builtins.__xonsh_glob__(s))
else:
rtn.append(builtins.__xonsh_expand_path__(s))
@ -1250,7 +1250,9 @@ def load_builtins(execer=None, ctx=None):
builtins.__xonsh_all_jobs__ = {}
builtins.__xonsh_ensure_list_of_strs__ = ensure_list_of_strs
builtins.__xonsh_list_of_strs_or_callables__ = list_of_strs_or_callables
builtins.__xonsh_list_of_list_of_strs_outer_product__ = list_of_list_of_strs_outer_product
builtins.__xonsh_list_of_list_of_strs_outer_product__ = (
list_of_list_of_strs_outer_product
)
builtins.__xonsh_completers__ = xonsh.completers.init.default_completers()
builtins.__xonsh_call_macro__ = call_macro
builtins.__xonsh_enter_macro__ = enter_macro

View file

@ -3123,7 +3123,9 @@ class BaseParser(object):
# has an expanding function call, such as @(x)
p0 = xonsh_call(
"__xonsh_list_of_list_of_strs_outer_product__",
args=[ensure_has_elts(p1)], lineno=p1[0].lineno, col=p1[0].col_offset
args=[ensure_has_elts(p1)],
lineno=p1[0].lineno,
col=p1[0].col_offset,
)
p0._cliarg_action = "extend"
elif hasglobstar(p1):
@ -3192,7 +3194,7 @@ class BaseParser(object):
"DOLLAR_LBRACKET",
"ATDOLLAR_LPAREN",
}
ts = "\n | ".join(sorted([t.lower() + '_tok' for t in toks]))
ts = "\n | ".join(sorted([t.lower() + "_tok" for t in toks]))
doc = "subproc_arg_part : " + ts + "\n"
self.p_subproc_arg_part.__func__.__doc__ = doc