Skip to content

Commit

Permalink
support exchanging globals with runs
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Mar 24, 2023
1 parent 4595675 commit 613fe31
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
27 changes: 15 additions & 12 deletions pytest_examples/eval_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,56 +71,59 @@ def update_examples(self) -> bool:
def run(
self,
example: CodeExample,
globals: dict[str, Any] | None = None,
*,
module_globals: dict[str, Any] | None = None,
rewrite_assertions: bool = True,
) -> dict[str, Any]:
"""
Run the example, print is not mocked and print statements are not checked.
:param example: The example to run.
:param globals: The globals to use when running the example.
:param module_globals: The globals to use when running the example.
:param rewrite_assertions: If True, rewrite assertions in the example using pytest's assertion rewriting.
"""
__tracebackhide__ = True
example.test_id = self._test_id
_, module_dict = self._run(example, None, globals, rewrite_assertions)
_, module_dict = self._run(example, None, module_globals, rewrite_assertions)
return module_dict

def run_print_check(
self,
example: CodeExample,
globals: dict[str, Any] | None = None,
*,
module_globals: dict[str, Any] | None = None,
rewrite_assertions: bool = True,
) -> dict[str, Any]:
"""
Run the example and check print statements.
:param example: The example to run.
:param globals: The globals to use when running the example.
:param module_globals: The globals to use when running the example.
:param rewrite_assertions: If True, rewrite assertions in the example using pytest's assertion rewriting.
"""
__tracebackhide__ = True
example.test_id = self._test_id
insert_print, module_dict = self._run(example, 'check', globals, rewrite_assertions)
insert_print, module_dict = self._run(example, 'check', module_globals, rewrite_assertions)
insert_print.check_print_statements(example)
return module_dict

def run_print_update(
self,
example: CodeExample,
globals: dict[str, Any] | None = None,
*,
module_globals: dict[str, Any] | None = None,
rewrite_assertions: bool = True,
) -> dict[str, Any]:
"""
Run the example and update print statements, requires `--update-examples`.
:param example: The example to run.
:param globals: The globals to use when running the example.
:param module_globals: The globals to use when running the example.
:param rewrite_assertions: If True, rewrite assertions in the example using pytest's assertion rewriting.
"""
__tracebackhide__ = True
self._check_update(example)
insert_print, module_dict = self._run(example, 'update', globals, rewrite_assertions)
insert_print, module_dict = self._run(example, 'update', module_globals, rewrite_assertions)

new_code = insert_print.updated_print_statements(example)
if new_code:
Expand All @@ -132,7 +135,7 @@ def _run(
self,
example: CodeExample,
insert_print_statements: Literal['check', 'update', None],
globals: dict[str, Any] | None,
module_globals: dict[str, Any] | None,
rewrite_assertions: bool,
) -> tuple[InsertPrintStatements, dict[str, Any]]:
__tracebackhide__ = True
Expand All @@ -159,8 +162,8 @@ def _run(
# does nothing if insert_print_statements is False
insert_print = InsertPrintStatements(python_file, self.config, enable_print_mock)

if globals:
module.__dict__.update(globals)
if module_globals:
module.__dict__.update(module_globals)
try:
with insert_print:
spec.loader.exec_module(module)
Expand Down
6 changes: 5 additions & 1 deletion pytest_examples/insert_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ class Arg:
def __init__(self, v: Any):
if isinstance(v, str):
self.string = v
elif isinstance(v, set):
# NOTE! this is not recursive
ordered = ', '.join(repr(x) for x in sorted(v))
self.string = f'{{{ordered}}}'
else:
self.code = str(v)
self.code = re.sub('0x[a-f0-9]{12}>', '0x0123456789ab>', str(v))

def __str__(self) -> str:
if self.string is not None:
Expand Down
2 changes: 1 addition & 1 deletion tests/cases_update/use_globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ examples_globals = {}
@pytest.mark.parametrize('example', find_examples('.'), ids=str)
def test_find_examples(example: CodeExample, eval_example: EvalExample):
if eval_example.update_examples:
m_dict = eval_example.run_print_update(example, globals=examples_globals)
m_dict = eval_example.run_print_update(example, module_globals=examples_globals)
examples_globals.update(m_dict)
```
19 changes: 19 additions & 0 deletions tests/test_insert_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,25 @@ def foobar():
""",
id='print-in-except',
),
pytest.param(
"""\
print({1, 3, 2, 4})
#> {1, 2, 3, 4}
""",
id='set-order',
),
pytest.param(
'''\
class Foo:
pass
print(Foo())
"""
<__main__.Foo object at 0x0123456789ab>
"""
''',
id='hex_id',
),
]


Expand Down

0 comments on commit 613fe31

Please sign in to comment.