# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Static checks and Unit Tests

on:
  push:
    branches: [ "main",  "stable-*" ]
  pull_request:
    branches:  [ "main",  "stable-*" ]

permissions:
  contents: write
  checks: write
  pull-requests: write

jobs:
  backend-unit-tests:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        ref: ${{ github.head_ref || github.ref }}
        submodules: recursive  # Fetch the submodules
        token: ${{ secrets.TESTS_REPOS_ACCESS_PAT }}
        fetch-depth: 0
    - run: df -h
    - name: "node-cleanup"
      run: |
        sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /opt/hostedtoolcache/CodeQL
        sudo docker image prune --all --force
        sudo docker builder prune -a
    - run: df -h
    - name: Set up Python 3.9
      uses: actions/setup-python@v5
      with:
        python-version: "3.9"
        cache: "pip"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r code-env/python/spec/requirements.github.txt
        pip install -r tests/python/unit/requirements.txt
    - name: Set PYTHONPATH
      run: |
        echo "PYTHONPATH=python-lib" >> $GITHUB_ENV
    - name: Run unit tests
      run: |
        set -o pipefail
        pytest --alluredir=tests/allure_report --doctest-modules --junitxml=junit/test-results.xml --cov=python-lib --cov-report=term-missing:skip-covered tests/python/unit | tee pytest-coverage.txt
    - name: Pytest coverage comment
      uses: MishaKav/pytest-coverage-comment@main
      with:
        pytest-coverage-path: ./pytest-coverage.txt
        junitxml-path: ./junit/test-results.xml
