Skip to content

Testing works #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"
Expand Down
82 changes: 82 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -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