name: CI
on:
    push:
        branches:
        - main
    pull_request:
    schedule:
        - cron:  '17 3 * * 0'

jobs:
    flake8:
        name: Flake8
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -
            uses: actions/setup-python@v1
            with:
                # matches compat target in setup.py
                python-version: '3.6'
        -   name: "Main Script"
            run: |
                curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-flake8.sh
                . ./prepare-and-run-flake8.sh "$(basename $GITHUB_REPOSITORY)" ./test examples

    pylint:
        name: Pylint
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                sed 's/python=3/python=3.7/' .test-conda-env-py3.yml > .test-conda-env.yml
                USE_CONDA_BUILD=1
                curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-pylint.sh
                . ./prepare-and-run-pylint.sh "$(basename $GITHUB_REPOSITORY)" test/test_*.py

    pytest:
        name: Conda Pytest
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project-within-miniconda.sh
                . ./build-and-test-py-project-within-miniconda.sh

    pytest_intel:
        name: Conda Pytest with Intel CL
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O https://raw.githubusercontent.com/illinois-scicomp/machine-shop-maintenance/main/install-intel-icd.sh
                sudo bash ./install-intel-icd.sh
                CONDA_ENVIRONMENT=.test-conda-env-py3.yml
                echo "- ocl-icd-system" >> "$CONDA_ENVIRONMENT"
                sed -i "/pocl/ d" "$CONDA_ENVIRONMENT"
                export PYOPENCL_TEST=intel
                source /opt/enable-intel-cl.sh
                curl -L -O https://tiker.net/ci-support-v0
                . ./ci-support-v0
                build_py_project_in_conda_env
                test_py_project

    pytest_no_arg_check:
        name: Conda Pytest without arg check
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project-within-miniconda.sh
                export _LOOPY_SKIP_ARG_CHECKS=1
                . ./build-and-test-py-project-within-miniconda.sh

    pytest_twice:
        name: Conda Pytest Twice (for cache behavior)
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O https://tiker.net/ci-support-v0
                . ./ci-support-v0
                build_py_project_in_conda_env
                ( test_py_project )
                ( test_py_project )

    examples:
        name: Conda Examples
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O https://tiker.net/ci-support-v0
                . ./ci-support-v0
                build_py_project_in_conda_env

                # ipython_genutils for https://github.com/jupyter/nbconvert/issues/1725
                with_echo pip install matplotlib ipykernel nbconvert ipython_genutils

                install_ispc

                export PYOPENCL_TEST=portable:pthread

                . ./.ci/examples-funcs.sh
                run_py_examples
                run_ipynb_examples
                run_floopy_examples

    docs:
        name: Documentation
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -
            uses: actions/setup-python@v1
            with:
                python-version: '3.x'
        -   name: "Main Script"
            run: |
                PROJECT=loopy
                curl -L -O https://tiker.net/ci-support-v0
                . ci-support-v0
                build_py_project_in_conda_env
                build_docs

    downstream_tests:
        strategy:
            matrix:
                downstream_project: [meshmode, grudge, pytential, pytato]
            fail-fast: false
        name: Tests for downstream project ${{ matrix.downstream_project }}
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            env:
                DOWNSTREAM_PROJECT: ${{ matrix.downstream_project }}
            run: |
                curl -L -O https://tiker.net/ci-support-v0
                . ./ci-support-v0
                test_downstream "$DOWNSTREAM_PROJECT"

    downstream_firedrake:
        name: Tests for downstream project Firedrake
        runs-on: ubuntu-latest
        container:
            image: 'firedrakeproject/firedrake'
        steps:
        -   uses: actions/checkout@v1
        -   name: "Main script"
            run: |
                sudo chown -R $(whoami) /github/home || true
                sudo chown -R $(whoami) /__w/loopy/ || true

                git submodule update --init

                . /home/firedrake/firedrake/bin/activate

                pip install --editable .

                cd /home/firedrake/firedrake/src/firedrake

                # https://github.com/OP2/PyOP2/pull/627
                (cd ../PyOP2 && git pull https://github.com/OP2/PyOP2.git e56d26f219e962cf9423fc84406a8a0656eb364f)

                # patch so exception messages get shown
                curl -L https://gist.githubusercontent.com/inducer/17d7134ace215f0df1f3627eac4195c7/raw/63edfaf2ec8bf06987896569a4f24264df490e9e/firedrake-debug-patch.diff | patch -p1

                pytest --tb=native -rsxw --durations=10 -m 'not parallel' tests/multigrid/

        -   name: "Prepare for tmate"
            run: |
                # helps with tmate debugging
                sudo chmod a+rwX -R $(whoami) /__w/_temp || true

        # -   uses: mxschmitt/action-tmate@v3

# vim: sw=4
