xonsh/tests/test_ast.py

143 lines
3.2 KiB
Python
Raw Normal View History

2016-06-05 14:32:59 -04:00
"""Xonsh AST tests."""
2016-08-19 01:45:44 -04:00
import ast as pyast
import pytest
2022-01-31 21:26:34 +05:30
from xonsh import ast
from xonsh.ast import BinOp, Call, Name, Store, Tuple, isexpression, min_line
2022-03-24 00:46:50 +05:30
from xonsh.pytest.tools import nodes_equal
2016-06-05 14:32:59 -04:00
@pytest.fixture(autouse=True)
def xonsh_execer_autouse(xonsh_execer):
return xonsh_execer
2016-06-05 14:32:59 -04:00
2018-08-30 09:18:49 -05:00
2016-06-05 14:32:59 -04:00
def test_gather_names_name():
2018-08-30 09:18:49 -05:00
node = Name(id="y", ctx=Store())
exp = {"y"}
2016-06-05 14:32:59 -04:00
obs = ast.gather_names(node)
2016-06-22 17:12:33 -04:00
assert exp == obs
2016-06-05 14:32:59 -04:00
def test_gather_names_tuple():
2018-08-30 09:18:49 -05:00
node = Tuple(elts=[Name(id="y", ctx=Store()), Name(id="z", ctx=Store())])
exp = {"y", "z"}
2016-06-05 14:32:59 -04:00
obs = ast.gather_names(node)
2016-06-22 17:12:33 -04:00
assert exp == obs
2016-08-21 14:58:07 -04:00
def test_gather_load_store_names_tuple():
2018-08-30 09:18:49 -05:00
node = Tuple(elts=[Name(id="y", ctx=Store()), Name(id="z", ctx=Store())])
lexp = set()
2018-08-30 09:18:49 -05:00
sexp = {"y", "z"}
lobs, sobs = ast.gather_load_store_names(node)
assert lexp == lobs
assert sexp == sobs
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"line1",
[
"x = 1", # Both, ls and l remain undefined.
"ls = 1", # l remains undefined.
"l = 1", # ls remains undefined.
2018-08-30 09:18:49 -05:00
],
)
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
def test_multilline_num(xonsh_execer_parse, line1):
# Subprocess transformation happens on the second line,
# because not all variables are known.
2018-08-30 09:18:49 -05:00
code = line1 + "\nls -l\n"
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
tree = xonsh_execer_parse(code)
lsnode = tree.body[1]
2016-06-22 17:12:33 -04:00
assert 2 == min_line(lsnode)
2016-08-21 14:58:07 -04:00
assert isinstance(lsnode.value, Call)
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
def test_multilline_no_transform(xonsh_execer_parse):
# No subprocess transformations happen here, since all variables are known.
2018-08-30 09:18:49 -05:00
code = "ls = 1\nl = 1\nls -l\n"
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
tree = xonsh_execer_parse(code)
2016-08-21 14:58:07 -04:00
lsnode = tree.body[2]
assert 3 == min_line(lsnode)
assert isinstance(lsnode.value, BinOp)
2016-08-19 01:45:44 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"inp",
[
"""def f():
2016-08-19 01:45:44 -04:00
if True:
pass
""",
2018-08-30 09:18:49 -05:00
"""def f(x):
2016-08-19 01:45:44 -04:00
if x:
pass
""",
2018-08-30 09:18:49 -05:00
"""def f(*args):
2016-08-19 01:45:44 -04:00
if not args:
pass
""",
2018-08-30 09:18:49 -05:00
"""def f(*, y):
2016-08-19 01:45:44 -04:00
if y:
pass
""",
2018-08-30 09:18:49 -05:00
"""def f(**kwargs):
2016-08-19 01:45:44 -04:00
if not kwargs:
pass
""",
2018-08-30 09:18:49 -05:00
"""def f(k=42):
2016-08-19 01:45:44 -04:00
if not k:
pass
""",
2018-08-30 09:18:49 -05:00
"""def f(k=10, *, a, b=1, **kw):
2016-08-19 01:45:44 -04:00
if not kw and b:
pass
""",
2018-08-30 09:18:49 -05:00
"""import os
path = '/path/to/wakka'
paths = []
for root, dirs, files in os.walk(path):
paths.extend(os.path.join(root, d) for d in dirs)
paths.extend(os.path.join(root, f) for f in files)
""",
2018-08-30 09:18:49 -05:00
"""lambda x: x + 1
2018-04-03 21:57:30 -04:00
""",
"""def f(x):
return [i for i in x if i is not None and i < 10]
""",
2018-08-30 09:18:49 -05:00
],
)
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
def test_unmodified(inp, xonsh_execer_parse):
2016-08-19 01:45:44 -04:00
# Context sensitive parsing should not modify AST
exp = pyast.parse(inp)
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
obs = xonsh_execer_parse(inp)
2016-08-19 01:45:44 -04:00
assert nodes_equal(exp, obs)
2018-09-29 01:55:30 -07:00
2018-11-09 13:49:33 -06:00
@pytest.mark.parametrize(
"test_input",
["echo; echo && echo\n", "echo; echo && echo a\n", "true && false && true\n"],
)
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
def test_whitespace_subproc(test_input, xonsh_execer_parse):
assert xonsh_execer_parse(test_input)
2018-11-09 13:49:33 -06:00
@pytest.mark.parametrize(
"inp,exp",
[
("1+1", True),
("1+1;", True),
("1+1\n", True),
("1+1; 2+2", False),
("1+1; 2+2;", False),
("1+1; 2+2\n", False),
("1+1; 2+2;\n", False),
("x = 42", False),
],
)
def test_isexpression(xonsh_execer, inp, exp):
obs = isexpression(inp)
assert exp is obs