|
| 1 | +"""Test that evaluating a markdown document twice doesn't break exec blocks. |
| 2 | +
|
| 3 | +This reproduces a CI failure where Granian's worker re-evaluates stateful pages |
| 4 | +in the same process after the initial compilation. The module-level cache in |
| 5 | +_exec_code causes an earlier exec-only block to pre-populate the transformer's |
| 6 | +env with names from *all* blocks on the second pass, so |
| 7 | +_exec_and_get_last_callable finds no "new" keys and raises RuntimeError. |
| 8 | +""" |
| 9 | + |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +sys.path.insert(0, str(Path(__file__).parent.parent)) |
| 16 | + |
| 17 | + |
| 18 | +MD_WITH_TWO_EXEC_BLOCKS = """\ |
| 19 | +```python exec |
| 20 | +import reflex as rx |
| 21 | +``` |
| 22 | +
|
| 23 | +# Demo page |
| 24 | +
|
| 25 | +```python demo exec |
| 26 | +class MyState(rx.State): |
| 27 | + count: int = 0 |
| 28 | +
|
| 29 | +def my_demo(): |
| 30 | + return rx.text(MyState.count) |
| 31 | +``` |
| 32 | +""" |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(autouse=True) |
| 36 | +def _clear_exec_caches(): |
| 37 | + """Reset the module-level caches so each test starts clean.""" |
| 38 | + from reflex_docs.docgen_pipeline import _executed_blocks, _file_modules |
| 39 | + |
| 40 | + old_blocks = _executed_blocks.copy() |
| 41 | + old_modules = _file_modules.copy() |
| 42 | + _executed_blocks.clear() |
| 43 | + _file_modules.clear() |
| 44 | + yield |
| 45 | + _executed_blocks.clear() |
| 46 | + _executed_blocks.update(old_blocks) |
| 47 | + _file_modules.clear() |
| 48 | + _file_modules.update(old_modules) |
| 49 | + |
| 50 | + |
| 51 | +def _render_once(text: str, virtual_filepath: str = "test_double_eval.md"): |
| 52 | + from reflex_docgen.markdown import parse_document |
| 53 | + |
| 54 | + from reflex_docs.docgen_pipeline import ReflexDocTransformer |
| 55 | + |
| 56 | + doc = parse_document(text) |
| 57 | + transformer = ReflexDocTransformer( |
| 58 | + virtual_filepath=virtual_filepath, filename=virtual_filepath |
| 59 | + ) |
| 60 | + return transformer.transform(doc) |
| 61 | + |
| 62 | + |
| 63 | +def test_double_eval_does_not_crash(): |
| 64 | + """Evaluating the same markdown twice must not raise 'Exec block defined nothing new'.""" |
| 65 | + # First pass — simulates the initial compilation. |
| 66 | + _render_once(MD_WITH_TWO_EXEC_BLOCKS) |
| 67 | + |
| 68 | + # Second pass — simulates the Granian worker re-evaluating stateful pages. |
| 69 | + # This is the call that fails before the fix. |
| 70 | + _render_once(MD_WITH_TWO_EXEC_BLOCKS) |
| 71 | + |
| 72 | + |
| 73 | +def test_double_eval_browser_javascript(): |
| 74 | + """The actual file that triggered the CI failure.""" |
| 75 | + filepath = ( |
| 76 | + Path(__file__).parent.parent.parent / "api-reference" / "browser_javascript.md" |
| 77 | + ) |
| 78 | + if not filepath.exists(): |
| 79 | + pytest.skip(f"{filepath} not found") |
| 80 | + |
| 81 | + from reflex_docs.docgen_pipeline import render_docgen_document |
| 82 | + |
| 83 | + vpath = "docs/api-reference/browser-javascript" |
| 84 | + render_docgen_document(vpath, filepath) |
| 85 | + render_docgen_document(vpath, filepath) |
| 86 | + |
| 87 | + |
| 88 | +# --------------------------------------------------------------------------- |
| 89 | +# Parametrized test: evaluate every markdown doc file twice |
| 90 | +# --------------------------------------------------------------------------- |
| 91 | + |
| 92 | +_app_root = Path(__file__).resolve().parent.parent # …/app/ |
| 93 | +_docs_dir = _app_root.parent # …/docs/ (parent of app/) |
| 94 | + |
| 95 | +_all_docs: dict[str, str] = {} # virtual_path → actual_path |
| 96 | +for _md_file in sorted(_docs_dir.rglob("*.md")): |
| 97 | + if _md_file.is_relative_to(_app_root): |
| 98 | + continue |
| 99 | + _virtual = "docs/" + str(_md_file.relative_to(_docs_dir)).replace("\\", "/") |
| 100 | + _all_docs[_virtual] = str(_md_file) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.fixture(params=list(_all_docs.keys())) |
| 104 | +def doc_file(request) -> tuple[str, str]: |
| 105 | + """Yield (virtual_path, actual_path) for each discovered markdown doc.""" |
| 106 | + virtual_path = request.param |
| 107 | + return virtual_path, _all_docs[virtual_path] |
| 108 | + |
| 109 | + |
| 110 | +def test_double_eval_all_docs(doc_file: tuple[str, str]): |
| 111 | + """Every markdown doc must survive two evaluations without error.""" |
| 112 | + from reflex_docs.docgen_pipeline import render_docgen_document |
| 113 | + |
| 114 | + virtual_path, actual_path = doc_file |
| 115 | + render_docgen_document(virtual_path, actual_path) |
| 116 | + render_docgen_document(virtual_path, actual_path) |
0 commit comments