From 7c1975b0fcc14508ccaa47478626040290450ad0 Mon Sep 17 00:00:00 2001 From: Dan Allan Date: Mon, 12 May 2025 14:22:02 -0700 Subject: [PATCH] Testing works --- pixi.toml | 3 ++ test.sh | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100755 test.sh diff --git a/pixi.toml b/pixi.toml index c3c4bd2..13628a3 100644 --- a/pixi.toml +++ b/pixi.toml @@ -21,6 +21,7 @@ build = { cmd = [ "-T", # show tracebacks "--keep-going", # do not stop on error ] } +test = "test.sh" clean = "rm -rf _build/*" start = "jupyter lab" @@ -33,6 +34,8 @@ matplotlib-base = ">=3.9" ipympl = ">=0.9" jupyterlab = ">=4.2" jupyterlab-myst = ">=2.4" +pytest = ">=8.3.5,<9" +nbval = ">=0.11.0,<0.12" [pypi-dependencies] sphinx = ">=8.0.2" diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..3b4bfeb --- /dev/null +++ b/test.sh @@ -0,0 +1,82 @@ +#!/bin/bash + +notebook_files=() + +convert_file() { + local file="$1" + # Convert to ipynb format, to be consumed by pytest nbval plugin. + jupytext --to ipynb $file + if [ $? -ne 0 ]; then + error_occurred=1 + echo "Errors when converting $file" + # Exit early + exit 1 + fi + notebook_file="${file%.md}.ipynb" + # Stash file in array to be cleaned up at the end. + notebook_files+=("${notebook_file}") + local status=$? +} + + +# If no arguments were provided, exit with error and show usage. +if [ $# -eq 0 ]; then + echo "Usage: $0 [filepaths...] | --all" >&2 + exit 1 +fi + +# Variable to track if any errors occur +error_occurred=0 + +# If --all is passed, locate eligible files and execute them all. +if [ "$1" == "--all" ]; then + files=$(find "tutorials" -name "*.md" | grep -v .ipynb_checkpoints) + for file in $files; do + if [ -f "$file" ]; then + # Extract the kernel information from the Jupytext Markdown file + kernel_info=$(grep -A 10 '^---$' "$file" | grep -E 'kernelspec') + # Skip if no kernel information was found + if [ -z "$kernel_info" ]; then + continue + fi + convert_file "$file" + if [ $? -ne 0 ]; then + error_occurred=1 + fi + else + echo "File not found: $file" >&2 + fi + done +else + # If filepaths are passed, execute them. + for file in "$@"; do + if [ -f "$file" ]; then + convert_file "$file" + if [ $? -ne 0 ]; then + error_occurred=1 + fi + else + echo "File not found: $file" >&2 + # Exit early + exit 1 + fi + done +fi + +pytest --nbval + +# Clean up ipynb files that were converted. Any stray ipynb files that were +# _not_ the result of conversion from Markdown will be left alone. +for file in "${notebook_files[@]}"; do + rm -v "$file" +done + +if [ $error_occurred -ne 0 ]; then + echo "Some files executed with unexpected errors." >&2 + exit 1 +else + echo "All files executed successfully." >&2 + exit 0 +fi + +