name: Run tests

on:
  push:
    branches:
      - main
      - '*.x'
  pull_request:
  # Run weekly on Monday at 1:23 UTC
  schedule:
  - cron:  '23 1 * * 1'
  workflow_dispatch:


jobs:
  test:
    runs-on: ${{ matrix.os }}
    timeout-minutes: 15
    # Disable scheduled CI runs on forks
    if: github.event_name != 'schedule' || github.repository_owner == 'ipython'
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest]
        python-version: ["3.12", "3.13", "3.14"]
        deps: [test_extra]
        # Test all on ubuntu, test ends on macos
        include:
          - os: macos-latest
            python-version: "3.12"
            deps: test_extra
          # free threaded, not with all dependencies
          - os: ubuntu-latest
            python-version: "3.14t"
            deps: test
          # Tests latest development Python version
          - os: ubuntu-latest
            python-version: "3.15-dev"
            deps: test
          - os: ubuntu-latest
            python-version: "3.12"
            deps: test_extra
            want-latest-entry-point-code: true

    steps:
    - uses: actions/checkout@v6
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v6
      with:
        python-version: ${{ matrix.python-version }}
        cache: pip
        cache-dependency-path: |
          pyproject.toml
    - name: Install uv
      uses: astral-sh/setup-uv@v7
    - name: Install latex
      if: runner.os == 'Linux' && matrix.deps == 'test_extra'
      run: echo "disable latex for now, issues in mirros" #sudo apt-get -yq -o Acquire::Retries=3 --no-install-suggests --no-install-recommends install texlive dvipng
    - name: Install and update Python dependencies (binary only)
      if: ${{ ! contains( matrix.python-version, 'dev' ) }}
      run: |
        uv pip install --system setuptools wheel build
        uv pip install --system -e .[${{ matrix.deps }}]
        uv pip install --system check-manifest pytest-cov pytest
    - name: Install and update Python dependencies (dev?)
      if: ${{ contains( matrix.python-version, 'dev' ) }}
      run: |
        uv pip install --system --prerelease=allow setuptools wheel build
        uv pip install --system --prerelease=allow --extra-index-url https://pypi.anaconda.org/scientific-python-nightly-wheels/simple -e .[${{ matrix.deps }}]
        uv pip install --system --prerelease=allow --extra-index-url https://pypi.anaconda.org/scientific-python-nightly-wheels/simple check-manifest pytest-cov
    - name: Try building with Python build
      if: runner.os != 'Windows'  # setup.py does not support sdist on Windows
      run: |
        python -m build
        shasum -a 256 dist/*
    - name: Check manifest
      if: runner.os != 'Windows'  # setup.py does not support sdist on Windows
      run: check-manifest

    - name: Install entry point compatible code (TEMPORARY, April 2024)
      if: matrix.want-latest-entry-point-code
      run: |
        uv pip list --system
        # Not installing matplotlib's entry point code as building matplotlib from source is complex.
        # Rely upon matplotlib to test all the latest entry point branches together.
        uv pip install --system git+https://github.com/ipython/matplotlib-inline.git@main
        uv pip list --system

    - name: pytest
      env:
        COLUMNS: 120
      run: |
        pytest --color=yes -raXxs ${{ startsWith(matrix.python-version, 'pypy') && ' ' || '--cov --cov-report=xml' }} --maxfail=15
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v5
      with:
         token: ${{ secrets.CODECOV_TOKEN }}
         name: Test
         files: /home/runner/work/ipython/ipython/coverage.xml

  oldest-deps:
    # pro-actively check backward compatibility
    runs-on: ${{ matrix.os }}
    timeout-minutes: 15
    # Disable scheduled CI runs on forks
    if: github.event_name != 'schedule' || github.repository_owner == 'ipython'
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          # include windows because of platform-specific direct dependencies
          - windows-latest

    steps:
    - uses: actions/checkout@v6
    - name: Set up uv with Python 3.12
      uses: astral-sh/setup-uv@v7
      with:
        python-version: '3.12'
        enable-cache: true
        activate-environment: true
        prune-cache: false
        cache-dependency-glob: |
          pyproject.toml

    - name: Install Python dependencies (oldest supported versions)
      run: uv pip install --resolution=lowest-direct -e .[test]

    - name: Try building with uv build
      if: runner.os != 'Windows'  # setup.py does not support sdist on Windows
      run: |
        uv build
        shasum -a 256 dist/*

    - name: Check manifest
      if: runner.os != 'Windows'  # setup.py does not support sdist on Windows
      run: uvx check-manifest

    - name: pytest
      env:
        COLUMNS: 120
      run: pytest --color=yes -raXxs
