Skip to content
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

Fix notebook tests #320

Merged
merged 11 commits into from
Dec 20, 2023
51 changes: 51 additions & 0 deletions .github/workflows/notebooks_dispatch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Notebooks Dispatch

on:
# Allows wourkflow to be called by other workflows
workflow_call:
inputs:
clone-irdb:
type: boolean
description: If True, use clone of IRDB repo instead of package download.

jobs:
run_notebooks:
strategy:
matrix:
# Run all operating systems, because this is the first interaction
# that users have with ScopeSim / IRDB.
# However, only use minimum and maximum supported Python version,
# as the IRDB download often fails.
# As of 2023/07/19, this test still often fails. See
# https://github.com/AstarVienna/ScopeSim/issues/254
# For now, only run on one configuration, as having one green test
# is more useful than 5 green and one red. The others can be enabled
# once we fix the problem properly.
# os: [ubuntu-latest, windows-latest, macos-latest]
# python-version: ['3.8', '3.11']
os: [ubuntu-latest]
python-version: ["3.11"]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Set up Poetry
uses: abatilo/actions-poetry@v2
with:
poetry-version: "1.7.1"

- name: Install dependencies
run: |
poetry install --with test --with dev --all-extras

- name: Run notebooks
env:
TQDM_MININTERVAL: 10
CLONE: ${{ inputs.clone-irdb && '--clone-irdb' || '' }}
run: ./runnotebooks.sh $CLONE --delete
34 changes: 4 additions & 30 deletions .github/workflows/notebooks_with_irdb_clone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ on:
branches:
- master
- dev_master
- dev_spectroscopy
pull_request:
branches:
- master
- dev_master
- dev_spectroscopy

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand All @@ -21,31 +19,7 @@ on:


jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
# Run only on a minimal subset of the matrix, as this is ran on many
# commits.
os: [ubuntu-latest]
python-version: ['3.11']

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
# Install this version of ScopeSim. Otherwise the PyPI version of
# ScopeSim will be installed when the test-requriments
# are installed, because ScopeSim is a dependency of
# ScopeSim_Templates.
pip install .
pip install .[dev,test]
- name: Run notebooks
env:
TQDM_MININTERVAL: 10
run: ./runnotebooks.sh --checkout-irdb --delete
call-notebooks-with-clone:
uses: ./.github/workflows/notebooks_dispatch.yml
with:
clone-irdb: true
38 changes: 2 additions & 36 deletions .github/workflows/notebooks_with_irdb_download.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,5 @@ on:


jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
# Run all operating systems, because this is the first interaction
# that users have with ScopeSim / IRDB.
# However, only use minimum and maximum supported Python version,
# as the IRDB download often fails.
# As of 2023/07/19, this test still often fails. See
# https://github.com/AstarVienna/ScopeSim/issues/254
# For now, only run on one configuration, as having one green test
# is more useful than 5 green and one red. The others can be enabled
# once we fix the problem properly.
# os: [ubuntu-latest, windows-latest, macos-latest]
# python-version: ['3.8', '3.11']
os: [ubuntu-latest]
python-version: ['3.11']

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
# Install this version of ScopeSim. Otherwise the PyPI version of
# ScopeSim will be installed when the test-requriments
# are installed, because ScopeSim is a dependency of
# ScopeSim_Templates.
pip install .
pip install .[dev,test]
- name: Run notebooks
# No --checkout-irdb to download the IRDB as a normal end user would.
run: ./runnotebooks.sh --delete
call-notebooks-without-clone:
uses: ./.github/workflows/notebooks_dispatch.yml
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ filterwarnings = [
"ignore:'cgi'*:DeprecationWarning",
"ignore:The py23*:DeprecationWarning",
"ignore:datetime.datetime.utcfromtimestamp()*:DeprecationWarning",
# Pytest Notebook stuff
"ignore:The*:pytest.PytestRemovedIn8Warning",
"ignore:Proactor*:RuntimeWarning"
]

[tool.coverage.report]
Expand Down
49 changes: 26 additions & 23 deletions runnotebooks.sh
Original file line number Diff line number Diff line change
@@ -1,53 +1,56 @@
#!/usr/bin/env bash

echo "# Running Notebooks Tests" >> $GITHUB_STEP_SUMMARY
# https://betterdev.blog/minimal-safe-bash-script-template/
set -Eeuo pipefail
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uch, I messed this up, because I did not try to run the script without any parameters. If $1 is unset, the script breaks. See https://github.com/AstarVienna/ScopeSim_Data/actions/runs/7405748794


# Set stdout as default so the script can be ran on the commandline.
STEP_SUMMARY="${GITHUB_STEP_SUMMARY:-/dev/stdout}"

echo "# Running Notebooks Tests" >> $STEP_SUMMARY

if [[ "x${1}" == "x--clone-irdb" ]] ; then
# Cloning IRDB
if [[ ! -e irdb ]] ; then
echo "_Cloning IRDB_" >> $GITHUB_STEP_SUMMARY
git clone https://github.com/AstarVienna/irdb.git
if [[ ! -e inst_pkgs ]] ; then
echo "_Cloning IRDB_" >> $STEP_SUMMARY
git clone https://github.com/AstarVienna/irdb.git inst_pkgs
fi

echo "## Symlinks" >> $GITHUB_STEP_SUMMARY
echo "## Patch notebooks" >> $STEP_SUMMARY
# https://github.com/koalaman/shellcheck/wiki/SC2044
find . -iname "*.ipynb" -printf '%h\0' | sort -z | uniq -z | while IFS= read -r -d '' dirnotebooks; do
find ./docs -iname "*.ipynb" -printf '%h\0' | sort -z | uniq -z | while IFS= read -r -d '' dirnotebooks; do
echo "${dirnotebooks}"
echo "- ${dirnotebooks}" >> $GITHUB_STEP_SUMMARY
dirinstpkgs="${dirnotebooks}/inst_pkgs"
if [[ (! -e ./docs/source/examples/inst_pkgs) && (! -L ./docs/source/examples/inst_pkgs) ]] ; then
echo "Creating symlink to irdb: ${dirinstpkgs}" >> $GITHUB_STEP_SUMMARY
ln -s irdb "${dirinstpkgs}"
else
echo "Directory exists, not creating symlink: ${dirinstpkgs}" >> $GITHUB_STEP_SUMMARY
fi

# Comment out any download_package[s] in the notebooks.
pushd "${dirnotebooks}" || exit 1
echo "- ${dirnotebooks}" >> $STEP_SUMMARY
pushd "${dirnotebooks}"
# Comment out any download_package[s] in the notebooks.
sed -i -E 's|"(.*\.download_package)|"#\1|g' -- *.ipynb
popd || exit 1
# Comment out explicitly setting the local_packages_path to somewhere.
sed -i -E 's|"(.*__config__\[\\"!SIM.file.local_packages_path)|"#\1|g' -- *.ipynb
popd
done
fi


echo "## Notebooks tested" >> $GITHUB_STEP_SUMMARY
echo "## Notebooks tested" >> $STEP_SUMMARY
# https://github.com/koalaman/shellcheck/wiki/SC2044
find . -iname "*.ipynb" -print0 | while IFS= read -r -d '' fnnotebook
find ./docs -iname "*.ipynb" -print0 | while IFS= read -r -d '' fnnotebook
do
echo "Testing ${fnnotebook} ..."
fnpy="${fnnotebook%.ipynb}.py"

# Convert .ipynb file to .py.
jupytext --to py "${fnnotebook}"
poetry run jupytext --to py "${fnnotebook}"

# Run the python script and quit on first error.
python "${fnpy}" || exit 1
echo "- ${fnnotebook}" >> $GITHUB_STEP_SUMMARY
poetry run python "${fnpy}"
echo "- ${fnnotebook}" >> $STEP_SUMMARY

# Delete generated files if --delete is specified.
# By default do not delete any files.
# The delete functionality is intended to make it easy for developers to test
# all the notebooks on their own machine.
if [ "x$1" = "x--delete" ]
then
echo "Removing ${fnpy}"
rm "${fnpy}"
fi

Expand Down