Skip to content

Commit f30e6d4

Browse files
authored
Remove rST-specific code in snippets_test.py (#7085)
A regex in this file (on line 143) triggered a code scan security warning:(https://github.com/quantumlib/Cirq/security/code-scanning/97): ```python def find_rst_code_snippets(content: str) -> List[Tuple[str, int]]: snippets = find_code_snippets( r'\n.. code-block:: python\n(?:\s+:.*?\n)*\n(.*?)(?:\n\S|\Z)', content ``` > This part of the regular expression may cause exponential > backtracking on strings starting with '\naa code-block:: python\n :' > and containing many repetitions of '\n :'. This was in code for testing reStructuredText files. Since we no longer have any `.rst` files in the code base, it seemed more sensible to remove all the rST-specific code in here. Not only does that resolve the code scan warning; it also reduces the maintenance burden going forward.
1 parent a2bf6e8 commit f30e6d4

File tree

1 file changed

+2
-129
lines changed

1 file changed

+2
-129
lines changed

dev_tools/snippets_test.py

Lines changed: 2 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414

1515
"""Tests for executable snippets in documentation.
1616
17-
This tests runs code snippets that are executable in `.md` and `.rst`
18-
documentation. It covers all such files under the docs directory, as well as
19-
the top level README file.
17+
This tests code snippets that are executable in `.md` documentation. It covers
18+
all such files under the docs directory, as well as the top-level README file.
2019
2120
In addition to checking that the code executes:
2221
@@ -45,12 +44,6 @@
4544
substitution
4645
--->
4746
48-
and for `.rst` the substitution is of the form
49-
50-
.. test-substitution::
51-
pattern
52-
substitution
53-
5447
where pattern is the regex matching pattern (passed to re.compile) and
5548
substitution is the replacement string.
5649
"""
@@ -79,8 +72,6 @@ def find_docs_code_snippets_paths() -> Iterator[str]:
7972
docs_folder = pathlib.Path(__file__).parent
8073
for filename in docs_folder.rglob('*.md'):
8174
yield str(filename.relative_to(docs_folder))
82-
for filename in docs_folder.rglob('*.rst'):
83-
yield str(filename.relative_to(docs_folder))
8475

8576

8677
@pytest.mark.parametrize('path', find_docs_code_snippets_paths())
@@ -138,120 +129,6 @@ def deindent_snippet(snippet: str) -> str:
138129
return '\n'.join(deindented_lines)
139130

140131

141-
def find_rst_code_snippets(content: str) -> List[Tuple[str, int]]:
142-
snippets = find_code_snippets(
143-
r'\n.. code-block:: python\n(?:\s+:.*?\n)*\n(.*?)(?:\n\S|\Z)', content
144-
)
145-
return [(deindent_snippet(content), line_number) for content, line_number in snippets]
146-
147-
148-
def find_rst_test_overrides(content: str) -> List[Tuple[Pattern, str]]:
149-
# Find ".. test-substitution::"
150-
test_sub_text = find_code_snippets(r'.. test-substitution::\n(([^\n]*\n){2})', content)
151-
substitutions = [line.split('\n')[:-1] for line, _ in test_sub_text]
152-
return [(re.compile(match.lstrip()), sub.lstrip()) for match, sub in substitutions]
153-
154-
155-
def test_find_rst_code_snippets():
156-
snippets = find_rst_code_snippets(
157-
"""
158-
A 3 by 3 grid of qubits using
159-
160-
.. code-block:: python
161-
162-
print("hello world")
163-
164-
The next level up.
165-
166-
.. code-block:: python
167-
:emphasize-lines: 3,5
168-
169-
print("hello 1")
170-
171-
for i in range(10):
172-
print(f"hello {i}")
173-
174-
More text.
175-
176-
.. code-block:: python
177-
178-
print("last line")
179-
"""
180-
)
181-
182-
assert snippets == [
183-
('print("hello world")\n', 4),
184-
('print("hello 1")\n\nfor i in range(10):\n print(f"hello {i}")\n', 10),
185-
('print("last line")\n', 20),
186-
]
187-
188-
189-
def test_find_rst_overrides():
190-
overrides = find_rst_test_overrides(
191-
"""
192-
A 3 by 3 grid of qubits using
193-
194-
.. code-block:: python
195-
196-
print("hello world")
197-
print("golden")
198-
199-
.. test-substitution::
200-
hello world
201-
goodbye cruel world
202-
203-
.. test-substitution::
204-
golden
205-
yellow
206-
"""
207-
)
208-
assert len(overrides) == 2
209-
assert overrides[0][0].match('hello world')
210-
assert overrides[1][0].match('golden')
211-
assert overrides[0][1] == 'goodbye cruel world'
212-
assert overrides[1][1] == 'yellow'
213-
214-
215-
def test_apply_rst_overrides():
216-
content = """
217-
A 3 by 3 grid of qubits using
218-
219-
.. code-block:: python
220-
221-
print("hello world")
222-
print("golden")
223-
224-
.. test-substitution::
225-
hello world
226-
goodbye cruel world
227-
228-
.. test-substitution::
229-
golden
230-
yellow
231-
"""
232-
overrides = find_rst_test_overrides(content)
233-
print(overrides)
234-
assert (
235-
apply_overrides(content, overrides)
236-
== """
237-
A 3 by 3 grid of qubits using
238-
239-
.. code-block:: python
240-
241-
print("goodbye cruel world")
242-
print("yellow")
243-
244-
.. test-substitution::
245-
goodbye cruel world
246-
goodbye cruel world
247-
248-
.. test-substitution::
249-
yellow
250-
yellow
251-
"""
252-
)
253-
254-
255132
def test_find_markdown_code_snippets():
256133
snippets = find_markdown_code_snippets(
257134
"""
@@ -359,10 +236,6 @@ def assert_file_has_working_code_snippets(path: str, assume_import: bool):
359236
overrides = find_markdown_test_overrides(content)
360237
content = apply_overrides(content, overrides)
361238
snippets = find_markdown_code_snippets(content)
362-
else:
363-
overrides = find_rst_test_overrides(content)
364-
content = apply_overrides(content, overrides)
365-
snippets = find_rst_code_snippets(content)
366239
assert_code_snippets_run_in_sequence(snippets, assume_import)
367240

368241

0 commit comments

Comments
 (0)