| 1 |
import io |
| 2 |
import inspect |
| 3 |
import asyncio |
| 4 |
|
| 5 |
import pytest |
| 6 |
from aioconsole import aexec, aeval |
| 7 |
from aioconsole.execute import compile_for_aexec |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
async def coro(x): |
| 14 |
await asyncio.sleep(0) |
| 15 |
return x |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
testdata = { |
| 21 |
"simple": ({}, "1+1", 2, {}), |
| 22 |
"affect": ({}, "a=1", None, {"a": 1}), |
| 23 |
"modify": ({"b": 1}, "b=2", None, {"b": 2}), |
| 24 |
"multiple": ({}, "c=3;d=4;5", 5, {"c": 3, "d": 4}), |
| 25 |
"async": ({"coro": coro}, "await coro(6)", 6, {"coro": coro}), |
| 26 |
"multiline string": ({}, '"""\n"""', "\n", {}), |
| 27 |
"splitted string": ({}, '"a\\\nb"', "ab", {}), |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
@pytest.mark.parametrize( |
| 35 |
"local, code, expected_result, expected_local", |
| 36 |
list(testdata.values()), |
| 37 |
ids=list(testdata.keys()), |
| 38 |
) |
| 39 |
@pytest.mark.asyncio |
| 40 |
async def test_aexec_exec_mode(local, code, expected_result, expected_local): |
| 41 |
stream = io.StringIO() |
| 42 |
await aexec(code, local=local, stream=stream) |
| 43 |
assert stream.getvalue() == "" |
| 44 |
assert local == expected_local |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.parametrize( |
| 48 |
"local, code, expected_result, expected_local", |
| 49 |
list(testdata.values()), |
| 50 |
ids=list(testdata.keys()), |
| 51 |
) |
| 52 |
@pytest.mark.asyncio |
| 53 |
async def test_aexec_single_mode(local, code, expected_result, expected_local): |
| 54 |
stream = io.StringIO() |
| 55 |
await aexec(compile_for_aexec(code, "test", "single"), local=local, stream=stream) |
| 56 |
if expected_result is None: |
| 57 |
assert stream.getvalue() == "" |
| 58 |
else: |
| 59 |
assert stream.getvalue().strip() == repr(expected_result) |
| 60 |
assert local.pop("_") == expected_result |
| 61 |
assert local == expected_local |
| 62 |
|
| 63 |
|
| 64 |
def test_invalid_compile_modes(): |
| 65 |
with pytest.raises(ValueError): |
| 66 |
compile_for_aexec("1\n", "test", "eval") |
| 67 |
with pytest.raises(ValueError): |
| 68 |
compile_for_aexec("1\n", "test", "unknown") |
| 69 |
|
| 70 |
|
| 71 |
@pytest.mark.asyncio |
| 72 |
async def test_incomplete_code(): |
| 73 |
with pytest.raises(SyntaxError): |
| 74 |
await aexec("(") |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.asyncio |
| 78 |
async def test_missing_newline(): |
| 79 |
|
| 80 |
dct = {} |
| 81 |
await aexec("def f():\n return 1", dct) |
| 82 |
assert dct["f"]() == 1 |
| 83 |
await aexec("def g(): return 2", dct) |
| 84 |
assert dct["g"]() == 2 |
| 85 |
|
| 86 |
|
| 87 |
with pytest.raises(SyntaxError): |
| 88 |
await aexec( |
| 89 |
compile_for_aexec( |
| 90 |
"def f():\n return 1", "test", "single", dont_imply_dedent=True |
| 91 |
) |
| 92 |
) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.asyncio |
| 99 |
async def test_return_handling(): |
| 100 |
with pytest.raises(SyntaxError) as context: |
| 101 |
await aexec("return None, {}") |
| 102 |
|
| 103 |
assert context.value.msg == "'return' outside function" |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.asyncio |
| 107 |
async def test_yield_handling(): |
| 108 |
with pytest.raises(SyntaxError) as context: |
| 109 |
await aexec("for i in range(5): yield i") |
| 110 |
|
| 111 |
assert context.value.msg == "'yield' outside function" |
| 112 |
|
| 113 |
|
| 114 |
@pytest.mark.asyncio |
| 115 |
async def test_yield_from_handling(): |
| 116 |
with pytest.raises(SyntaxError) as context: |
| 117 |
await aexec("yield from range(5)") |
| 118 |
|
| 119 |
assert context.value.msg == "'yield' outside function" |
| 120 |
|
| 121 |
|
| 122 |
@pytest.mark.asyncio |
| 123 |
async def test_correct(): |
| 124 |
await aexec("def x(): return") |
| 125 |
await aexec("async def x(): return") |
| 126 |
await aexec("def x(): yield") |
| 127 |
await aexec("async def x(): yield") |
| 128 |
|
| 129 |
|
| 130 |
def echo(x): |
| 131 |
"""Sync function for aeval parameterized test.""" |
| 132 |
return x |
| 133 |
|
| 134 |
|
| 135 |
async def aecho(x): |
| 136 |
"""Async function for aeval parameterized test.""" |
| 137 |
return echo(x) |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.asyncio |
| 142 |
@pytest.mark.parametrize( |
| 143 |
"expression, local", |
| 144 |
[ |
| 145 |
|
| 146 |
("1 + 2", None), |
| 147 |
("sum([i * i for i in range(10)])", None), |
| 148 |
|
| 149 |
("def foo(): return 42", None), |
| 150 |
("x = 1", None), |
| 151 |
("x = 1\nx + 1", None), |
| 152 |
("for i in range(10): pass", None), |
| 153 |
("if True: pass", None), |
| 154 |
("while True: break", None), |
| 155 |
("try: pass\nexcept: pass", None), |
| 156 |
|
| 157 |
("undefined_variable", None), |
| 158 |
("undefined_function()", None), |
| 159 |
|
| 160 |
("1/0", None), |
| 161 |
("open('nonexistent_file.txt')", None), |
| 162 |
|
| 163 |
("(lambda x: x * 2)(5)", None), |
| 164 |
|
| 165 |
("len('test')", None), |
| 166 |
("min([3, 1, 4, 1, 5, 9])", None), |
| 167 |
("max([x * x for x in range(10)])", None), |
| 168 |
|
| 169 |
("True and False", None), |
| 170 |
("not True", None), |
| 171 |
("5 if True else 10", None), |
| 172 |
|
| 173 |
("'hello' + ' ' + 'world'", None), |
| 174 |
("f'hello {42}'", None), |
| 175 |
|
| 176 |
("[x for x in range(5)]", None), |
| 177 |
("[x * x for x in range(10) if x % 2 == 0]", None), |
| 178 |
|
| 179 |
("return 42", None), |
| 180 |
("yield 5", None), |
| 181 |
|
| 182 |
("await aecho(5)", {"aecho": aecho, "echo": echo}), |
| 183 |
|
| 184 |
("...", []), |
| 185 |
("...", "string_instead_of_dict"), |
| 186 |
("...", 42), |
| 187 |
("...", set()), |
| 188 |
("...", ...), |
| 189 |
("...", 1.5), |
| 190 |
("...", object()), |
| 191 |
("...", asyncio), |
| 192 |
("...", lambda: ...), |
| 193 |
("...", {"__result__": 99}), |
| 194 |
|
| 195 |
("", None), |
| 196 |
(None, None), |
| 197 |
(0, None), |
| 198 |
({}, None), |
| 199 |
(object(), None), |
| 200 |
(asyncio, None), |
| 201 |
(..., None), |
| 202 |
(lambda: ..., None), |
| 203 |
|
| 204 |
("x", {"x": 1, "__aeval_result__": 99}), |
| 205 |
], |
| 206 |
) |
| 207 |
async def test_aeval(expression, local): |
| 208 |
|
| 209 |
async def capture(func, *args, **kwargs): |
| 210 |
try: |
| 211 |
if inspect.iscoroutinefunction(func): |
| 212 |
result = await func(*args, **kwargs) |
| 213 |
else: |
| 214 |
result = func(*args, **kwargs) |
| 215 |
except Exception as exc: |
| 216 |
return (type(exc), None) |
| 217 |
else: |
| 218 |
return (None, result) |
| 219 |
|
| 220 |
|
| 221 |
sync_expression = ( |
| 222 |
expression.replace("await a", "") if isinstance(expression, str) else expression |
| 223 |
) |
| 224 |
|
| 225 |
|
| 226 |
sync_capture = await capture(eval, sync_expression, local) |
| 227 |
async_capture = await capture(aeval, expression, local) |
| 228 |
assert sync_capture == async_capture |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
@pytest.mark.asyncio |
| 233 |
async def test_aeval_async_func_without_await(): |
| 234 |
expression = "asyncio.sleep(0)" |
| 235 |
local = {"asyncio": asyncio} |
| 236 |
result = await aeval(expression, local) |
| 237 |
assert asyncio.iscoroutine(result) |
| 238 |
await result |
| 239 |
|
| 240 |
|
| 241 |
@pytest.mark.asyncio |
| 242 |
async def test_aeval_valid_await_syntax(): |
| 243 |
expression = "await aecho(10)" |
| 244 |
local = {"aecho": aecho} |
| 245 |
result = await aeval(expression, local) |
| 246 |
assert result == 10 |
| 247 |
|
| 248 |
|
| 249 |
@pytest.mark.asyncio |
| 250 |
async def test_aeval_coro_in_local(): |
| 251 |
expression = "await coro" |
| 252 |
local = {"coro": aecho(10)} |
| 253 |
result = await aeval(expression, local) |
| 254 |
assert result == 10 |