From 89302021ef7a7e4bb88cd202a451e2eb7c097785 Mon Sep 17 00:00:00 2001 From: Dor Harpaz Date: Tue, 7 May 2024 18:37:18 +0300 Subject: [PATCH] test testbook --- .github/workflows/test-notebooks.yml | 35 ++++++++++++++-------------- tests/test_notebooks.py | 33 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 tests/test_notebooks.py diff --git a/.github/workflows/test-notebooks.yml b/.github/workflows/test-notebooks.yml index d06988d2..0d466ef8 100644 --- a/.github/workflows/test-notebooks.yml +++ b/.github/workflows/test-notebooks.yml @@ -16,19 +16,7 @@ jobs: - name: 'Install dependencies' run: | python -m pip install -U -r requirements.txt - python -m pip install -U pytest - - - name: Get changed files - id: changed-files - run: | - if ${{ github.event_name == 'pull_request' }}; then - echo "changed_files=$(git diff --name-only HEAD^ HEAD | xargs)" >> $GITHUB_OUTPUT - fi - - name: List changed files - run: | - for file in ${{ steps.changed-files.outputs.changed_files }}; do - echo "$file was changed" - done + python -m pip install -U pytest testbook - name: Get changed files - all id: changed-files-all @@ -40,10 +28,21 @@ jobs: files: | **.ipynb + - name: Set environment variables + run: | + if [ "${{ github.event_name }}" == 'pull_request' ]; then + echo "SHOULD_TEST_ALL_FILES=false" >> $GITHUB_ENV + echo "HAS_ANY_FILE_CHANGED=${{ steps.changed-files-all.outputs.any_changed }}" >> $GITHUB_ENV + echo "LIST_OF_FILE_CHANGED=${{ steps.changed-files-all.outputs.all_changed_files }}" >> $GITHUB_ENV + echo "HAS_ANY_IPYNB_CHANGED=${{ steps.changed-files-ipynb.outputs.any_changed }}" >> $GITHUB_ENV + echo "LIST_OF_IPYNB_CHANGED=${{ steps.changed-files-ipynb.outputs.all_changed_files }}" >> $GITHUB_ENV + elif [ "${{ github.event_name }}" == 'workflow_dispatch' ]; then + echo "SHOULD_TEST_ALL_FILES=true" >> $GITHUB_ENV + echo "HAS_ANY_FILE_CHANGED=None" >> $GITHUB_ENV + echo "LIST_OF_FILE_CHANGED=None" >> $GITHUB_ENV + echo "HAS_ANY_IPYNB_CHANGED=None" >> $GITHUB_ENV + echo "LIST_OF_IPYNB_CHANGED=None" >> $GITHUB_ENV + fi + - name: 'Run tests' run: python -m pytest tests - env: - HAS_ANY_FILE_CHANGED: "${{ steps.changed-files-all.outputs.any_changed }}" - LIST_OF_FILE_CHANGED: "${{ steps.changed-files-all.outputs.all_changed_files }}" - HAS_ANY_IPYNB_CHANGED: "${{ steps.changed-files-ipynb.outputs.any_changed }}" - LIST_OF_IPYNB_CHANGED: "${{ steps.changed-files-ipynb.outputs.all_changed_files }}" diff --git a/tests/test_notebooks.py b/tests/test_notebooks.py new file mode 100644 index 00000000..49c28eb5 --- /dev/null +++ b/tests/test_notebooks.py @@ -0,0 +1,33 @@ +from testbook import testbook +import os + +TIMEOUT: int = 60 + + +def test_notebooks(): + if os.environ.get("SHOULD_TEST_ALL_FILES", "") == "true": + notebooks_to_test = get_all_notebooks() + else: + if os.environ.get("HAS_ANY_IPYNB_CHANGED", "") == "true": + notebooks_to_test = os.environ.get("LIST_OF_IPYNB_CHANGED", "").split() + else: + return + + # for notebook_path in notebooks_to_test: + # with testbook( + # notebook_path, + # execute=True, + # timeout=TIMEOUT, + # allow_errors=False, + # ) as tb: + # pass # we simply wish it to run without errors + + print(notebooks_to_test) + assert False + + +def get_all_notebooks(): + for root, dirs, files in os.walk(directory): + for file in files: + if file.endswith(".ipynb"): + yield os.path.join(root, file)