Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/scripts/check_notebook_has_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python3
"""Fail when a page converted by jupytext contains no code cells.

Executing such a notebook succeeds trivially, so its test would pass without
running anything from the page. This happens, for example, when a ``:::`` line
(a MyST directive) makes jupytext read the page as pandoc markdown, which turns
ordinary code fences into markdown rather than code cells.
"""

import json
import sys


def main(path):
with open(path) as stream:
cells = json.load(stream)["cells"]
code = sum(cell["cell_type"] == "code" for cell in cells)
print(f"{path}: {len(cells)} cells, {code} code")
if not code:
print("FAILED: notebook has no code cells; the page test would execute nothing",
file=sys.stderr)
return 1
return 0


if __name__ == "__main__":
sys.exit(main(sys.argv[1]))
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ add_nb_test("2-gen-and-fastsim/2-1-event-generation/README")
add_nb_test("2-gen-and-fastsim/2-2-fastsim-delphes/README")
add_nb_test("3-analysis/3-1-higgs-analysis/README")
add_nb_test("4-full-detector-sim/FccCaloPerformance/CaloFullSimExercise")

# Executing a notebook without code cells passes trivially; make that visible.
# Scoped to the event-generation page for now.
function(add_nb_code_guard testpage)
add_test(NAME ${testpage}-has-code-cells
COMMAND python3 ${CMAKE_SOURCE_DIR}/.github/scripts/check_notebook_has_code.py
${CMAKE_SOURCE_DIR}/${testpage}.ipynb)
endfunction()

add_nb_code_guard("2-gen-and-fastsim/2-1-event-generation/README")