Skip to content

Commit 5944762

Browse files
committed
Fix bad return bug
Previously (due to a hangover from an earlier implementation), if we were missing a name or title from the code block, we would return _only_ the `example_cell` in question, not the whole `chapter`. This was causing a few odd bugs but now has been fixed, and I've added a test to guard against this in the future.
1 parent dc33590 commit 5944762

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

jupyter_book_to_htmlbook/code_processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def process_code_examples(chapter):
138138
logging.warning(
139139
"Missing first two line comments for uuid and title." +
140140
f"Unable to apply example formatting to {example_cell}.")
141-
return example_cell
141+
return chapter
142142

143143
# ensure comments are within the first three spans (since we
144144
# expect an empty span to start based on their highlighter)

tests/test_code_processing.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,29 @@ def test_malformed_example_missing_title(self, caplog):
405405
assert "Unable to apply example formatting" in log
406406
assert not result.find("div", class_="highlight").get("data-type")
407407

408+
def test_malformed_example_does_not_destroy_chapter(self, caplog):
409+
"""
410+
Once we had a bad return given failures; this test is to
411+
ensure that even if we don't add example formatting, we also
412+
do not only return the bad block.
413+
"""
414+
expect_fail = BeautifulSoup("""
415+
<div id="do_not_lose_me">Hello!</div>
416+
<div class="cell tag_example docutils container">
417+
<div class="cell_input docutils container">
418+
<div class="highlight-ipython3 notranslate"><div class="highlight">
419+
<pre><span></span><span class="c1"># This is an example title</span>
420+
421+
<span class="k">def</span> <span class="nf">h</span><span class="p">():</span>
422+
<span class="k">pass</span></pre></div></div></div></div>
423+
""", "html.parser")
424+
caplog.set_level(logging.DEBUG)
425+
result = process_code_examples(expect_fail)
426+
log = caplog.text
427+
assert "Unable to apply example formatting" in log
428+
assert not result.find("div", class_="highlight").get("data-type")
429+
assert result.find("div", id="do_not_lose_me")
430+
408431
def test_malformed_example_with_extra_comments_later(self, caplog):
409432
"""
410433
Ensure that we're logging failures (e.g., when an author doesn't

0 commit comments

Comments
 (0)