Skip to content

Commit fcfd808

Browse files
vuldercjdb
authored andcommitted
Update topic checker implementation
1 parent bd06835 commit fcfd808

File tree

2 files changed

+94
-33
lines changed

2 files changed

+94
-33
lines changed

tools/tests/topic_updater_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,42 @@ def test_newline_between_heading_and_meta_text(self):
298298
"No newline between heading and meta text.")
299299
self.assertTrue(
300300
start_test_scope_lines[2].startswith("_ Example section"))
301+
302+
303+
class TestSkeletonTopicChecker(unittest.TestCase):
304+
"""
305+
Test skeleton topic checker function. This test covers different scenarios
306+
that should or should not be detected by the topic checker.
307+
"""
308+
@classmethod
309+
def setUpClass(cls):
310+
""" Setup small test skeleton which contains all edge cases. """
311+
cls.test_skeleton = tu.Skeleton(TEST_INPUTS / "test_skeleton.md")
312+
313+
def test_if_good_topic_passes_check(self):
314+
"""
315+
Checks that we do not fail on correct topics and user provided document
316+
content.
317+
"""
318+
topic_file_path = TEST_INPUTS / "user_content_heading_topic.md"
319+
with open(topic_file_path, "r") as topic_file:
320+
self.assertTrue(
321+
self.test_skeleton.check_if_topic_file_matches(topic_file))
322+
323+
def test_that_check_detects_missing_sections(self):
324+
"""
325+
Checks that check detects missing sections in topic documents.
326+
"""
327+
topic_file_path = TEST_INPUTS / "missing_sections.md"
328+
with open(topic_file_path, "r") as topic_file:
329+
self.assertFalse(
330+
self.test_skeleton.check_if_topic_file_matches(topic_file))
331+
332+
def test_that_check_detects_wrong_sections(self):
333+
"""
334+
Checks that check detects wrong sections in topic documents.
335+
"""
336+
topic_file_path = TEST_INPUTS / "fix_wrong_sections.md"
337+
with open(topic_file_path, "r") as topic_file:
338+
self.assertFalse(
339+
self.test_skeleton.check_if_topic_file_matches(topic_file))

topic_updater.py

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -114,46 +114,60 @@ def get_title_heading(self) -> SectionHeading:
114114
"First heading in the skeleton was not the title.")
115115
return self.headings[0]
116116

117-
def check_if_topic_file_matches(self, topic_file_path: Path) -> bool:
117+
def check_if_topic_file_matches(self, topic_file: tp.TextIO) -> bool:
118118
"""
119119
Checks if the topic file headings and meta text matches the skeleton.
120120
Prints the differences between the topic file and the skeleton, if a
121121
miss mach is detected.
122122
123123
Args:
124-
topic_file_path: path to the topic file to check
124+
topic_file: the topic markdown file to update
125125
126126
Returns: `True` if the topic matches, otherwise, `False`
127127
"""
128-
with open(topic_file_path, "r") as topic_file:
129-
current_heading_iter = iter(self.headings)
130-
current_heading = None
131-
expected_meta_text: tp.List[str] = []
128+
current_heading_iter = iter(self.headings)
129+
current_heading = None
130+
processing_meta_text = False
131+
expected_meta_text: tp.List[str] = []
132132

133-
for line in topic_file.readlines():
134-
if line.startswith("#"):
135-
if current_heading is not None and expected_meta_text:
136-
print("Found missing italics:")
137-
print(f"Expected: {expected_meta_text[0]}")
138-
return False
139-
140-
current_heading = next(current_heading_iter)
141-
expected_meta_text = copy(current_heading.meta_text)
142-
if line.startswith("_"):
143-
if not expected_meta_text:
144-
print("Did not expect further italics but found:")
145-
print(line)
146-
return False
147-
148-
if line == expected_meta_text[0]:
149-
expected_meta_text.pop(0)
150-
else:
151-
print("Found italics text did not match the skeleton:")
152-
print(f"Found:\n{line}")
153-
print(f"Expected:\n{expected_meta_text[0]}")
154-
return False
155-
156-
return True
133+
for line in topic_file.readlines():
134+
if line.startswith("#"):
135+
if current_heading and expected_meta_text:
136+
print("Found missing italics:")
137+
print(f"Expected: {expected_meta_text[0]}")
138+
return False
139+
140+
current_heading = next(current_heading_iter)
141+
expected_meta_text = copy(
142+
self.lookup_heading(current_heading.header_text).meta_text)
143+
processing_meta_text = False
144+
145+
# Check if the required section headings are present
146+
if line.startswith("##") and current_heading:
147+
if current_heading.header_text.split(":")[0] != line.split(
148+
":")[0].strip():
149+
print("Found wrong section title:")
150+
print(f"Found:\n{line.strip()}")
151+
print(f"Expected:\n{current_heading.header_text}")
152+
return False
153+
154+
# Check if the correct meta text is below the section heading
155+
if line.startswith("_") or processing_meta_text:
156+
if not expected_meta_text:
157+
print("Did not expect further italics but found:")
158+
print(line)
159+
return False
160+
161+
if line == expected_meta_text[0]:
162+
processing_meta_text = not line.strip().endswith("_")
163+
expected_meta_text.pop(0)
164+
else:
165+
print("Found italics text did not match the skeleton:")
166+
print(f"Found:\n{line}")
167+
print(f"Expected:\n{expected_meta_text[0]}")
168+
return False
169+
170+
return True
157171

158172
def update_topic_meta_text(self, topic_file: tp.TextIO) -> tp.List[str]:
159173
"""
@@ -292,17 +306,25 @@ def __str__(self) -> str:
292306

293307

294308
def check_skeletons(skeleton: Skeleton,
295-
topic_paths: tp.Iterator[Path]) -> None:
309+
topic_paths: tp.Iterator[Path]) -> bool:
296310
"""
297311
Check of the topics files match the skeleton.
298312
299313
Args:
300314
skeleton: base skeleton to compare the topics against
301315
topic_paths: list of paths to topic files
316+
317+
Returns: True, if all topic files matched the skeleton
302318
"""
319+
all_files_matched = True
303320
for topic_path in topic_paths:
304-
if skeleton.check_if_topic_file_matches(topic_path):
305-
print(f"All meta-text in {topic_path} matched the skeleton.")
321+
with open(topic_path, "r") as topic_file:
322+
if skeleton.check_if_topic_file_matches(topic_file):
323+
print(f"All meta-text in {topic_path} matched the skeleton.")
324+
else:
325+
all_files_matched = False
326+
327+
return all_files_matched
306328

307329

308330
def update_skeletons(skeleton: Skeleton,

0 commit comments

Comments
 (0)