name: unit-tests

on:
  push:
    branches:
      - master
      - 2.3.X

jobs:
  test-all:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python-version: ['3.6', '3.7', '3.8', '3.9', '3.10', '3.11']
        include:
          - os: ubuntu-latest
            python-version: '3.10'
            send-coverage: true
            pytest-args: --cov=pydicom --cov-append

    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 2

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

    - name: Get pip cache dir
      id: pip-cache
      run: |
        python -m pip install -U pip
        echo "::set-output name=dir::$(pip cache dir)"

    - name: pip cache
      uses: actions/cache@v2
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: py${{ matrix.python-version }}-${{ matrix.os }}-pip

    - name: Cache pydicom test data
      id: cache-testdata
      uses: actions/cache@v2
      with:
        path: ${{ env.HOME }}/.pydicom
        key: pydicom-testdata

    - name: Initial test environment setup
      run: |
        python -m pip install -e .
        python -m pip install -U wheel pytest pytest-cov

    # Core library tests
    - name: Test core library
      run: |
        pytest ${{ matrix.pytest-args }}

    - name: Test external sources using pydicom-data
      run: |
        python -m pip install git+https://github.com/pydicom/pydicom-data
        pytest ${{ matrix.pytest-args}} pydicom/tests/test_data_manager.py

    # Pixel data tests
    - name: Install and test with numpy
      run: |
        python -m pip install -U numpy
        pytest ${{ matrix.pytest-args }}

    - name: Install and test Pillow
      run: |
        python -m pip install -U pillow
        pytest ${{ matrix.pytest-args }} pydicom/tests/test_pillow_pixel_data.py
        python -m pip uninstall -y pillow

    - name: Install and test jpeg_ls
      run: |
        python -m pip install -U cython
        python -m pip install git+https://github.com/Who8MyLunch/CharPyLS
        pytest ${{ matrix.pytest-args }} pydicom/tests/test_jpeg_ls_pixel_data.py pydicom/tests/test_JPEG_LS_transfer_syntax.py
        python -m pip uninstall -y CharPyLS

    - name: Install and test GDCM
      run: |
        python -m pip install python-gdcm
        python -c 'import gdcm; print(gdcm.GDCM_VERSION)'
        pytest ${{ matrix.pytest-args }} pydicom/tests/test_gdcm_pixel_data.py
        pytest ${{ matrix.pytest-args }} pydicom/tests/test_encoders_gdcm.py
        python -m pip uninstall -y python-gdcm

    - name: Install and test pylibjpeg
      run: |
        python -m pip install pylibjpeg
        python -m pip uninstall -y pylibjpeg-openjpeg pylibjpeg-rle
        pytest ${{ matrix.pytest-args }} pydicom/tests/test_pylibjpeg.py
        python -m pip install pylibjpeg-openjpeg pylibjpeg-libjpeg pylibjpeg-rle
        pytest ${{ matrix.pytest-args }} pydicom/tests/test_pylibjpeg.py

    - name: Test all pixel handling
      run: |
        python -m pip install pillow python-gdcm
        python -m pip install git+https://github.com/Who8MyLunch/CharPyLS
        pytest ${{ matrix.pytest-args }}

    - name: Send coverage results
      if: ${{ success() && matrix.send-coverage }}
      uses: codecov/codecov-action@v1
