|
| 1 | +from dataclasses import dataclass |
1 | 2 | from typing import Any |
2 | 3 |
|
3 | 4 | import pytest |
|
8 | 9 | pytestmark = pytest.mark.anyio |
9 | 10 |
|
10 | 11 |
|
| 12 | +@dataclass |
| 13 | +class Foobar: |
| 14 | + a: int |
| 15 | + b: str |
| 16 | + c: bytes |
| 17 | + |
| 18 | + |
11 | 19 | @pytest.mark.parametrize( |
12 | | - 'deps,code,expected', |
| 20 | + 'deps,code,locals,expected', |
13 | 21 | [ |
14 | 22 | pytest.param( |
15 | 23 | [], |
16 | 24 | 'a = 1\na + 1', |
| 25 | + {}, |
17 | 26 | snapshot({'status': 'success', 'output': [], 'return_value': 2}), |
18 | 27 | id='return-value-success', |
19 | 28 | ), |
20 | 29 | pytest.param( |
21 | 30 | [], |
22 | 31 | 'print(123)', |
| 32 | + {}, |
23 | 33 | snapshot({'status': 'success', 'output': ['123'], 'return_value': None}), |
24 | 34 | id='print-success', |
25 | 35 | ), |
| 36 | + pytest.param( |
| 37 | + [], |
| 38 | + 'a', |
| 39 | + {'a': [1, 2, 3]}, |
| 40 | + snapshot({'status': 'success', 'output': [], 'return_value': [1, 2, 3]}), |
| 41 | + id='access-local-variables', |
| 42 | + ), |
| 43 | + pytest.param( |
| 44 | + [], |
| 45 | + 'a + b', |
| 46 | + {'a': 4, 'b': 5}, |
| 47 | + snapshot({'status': 'success', 'output': [], 'return_value': 9}), |
| 48 | + id='multiple-locals', |
| 49 | + ), |
| 50 | + pytest.param( |
| 51 | + [], |
| 52 | + 'print(f)', |
| 53 | + {'f': Foobar(1, '2', b'3')}, |
| 54 | + snapshot({'status': 'success', 'output': ["{'a': 1, 'b': '2', 'c': '3'}"], 'return_value': None}), |
| 55 | + id='print-complex-local', |
| 56 | + ), |
| 57 | + pytest.param( |
| 58 | + [], |
| 59 | + 'f', |
| 60 | + {'f': Foobar(1, '2', b'3')}, |
| 61 | + snapshot({'status': 'success', 'output': [], 'return_value': {'a': 1, 'b': '2', 'c': '3'}}), |
| 62 | + id='return-complex-local', |
| 63 | + ), |
26 | 64 | pytest.param( |
27 | 65 | [], |
28 | 66 | 'print(unknown)', |
| 67 | + {}, |
29 | 68 | snapshot( |
30 | 69 | { |
31 | 70 | 'status': 'run-error', |
|
44 | 83 | pytest.param( |
45 | 84 | ['numpy'], |
46 | 85 | 'import numpy\nnumpy.array([1, 2, 3])', |
| 86 | + {}, |
47 | 87 | snapshot({'status': 'success', 'output': [], 'return_value': [1, 2, 3]}), |
48 | 88 | id='return-numpy-success', |
49 | 89 | ), |
50 | 90 | ], |
51 | 91 | ) |
52 | | -async def test_sandbox(deps: list[str], code: str, expected: Any): |
| 92 | +async def test_sandbox(deps: list[str], code: str, locals: dict[str, Any], expected: Any): |
53 | 93 | async with code_sandbox(dependencies=deps) as sandbox: |
54 | | - result = await sandbox.eval(code) |
| 94 | + result = await sandbox.eval(code, locals) |
55 | 95 | assert result == expected |
56 | 96 |
|
57 | 97 |
|
|
0 commit comments