name: CI

on:
  workflow_dispatch:
  push:
    branches:
    - master
    - main
  pull_request:

env:
  FORCE_COLOR: 3

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true


jobs:

  pre-commit:
    name: Format
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v6
      with:
        fetch-depth: 0
    - uses: actions/setup-python@v6
      with:
        python-version: "3.x"
    - uses: pre-commit/action@v3.0.1
    - name: Setup uv
      uses: astral-sh/setup-uv@v8.2.0
    - name: pylint
      run: uvx nox -s pylint -- --output-format=github

  tests:
    name: Tests on 🐍 ${{ matrix.python-version }} ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.9", "3.11", "3.14"]
        os: [ubuntu-latest, windows-latest, macos-latest]
        include:
        - python-version: 'pypy-3.11'
          os: ubuntu-latest
        - python-version: '3.10'
          os: ubuntu-latest
        - python-version: '3.12'
          os: ubuntu-latest
        - python-version: '3.13'
          os: ubuntu-latest
        - python-version: '3.15'
          os: ubuntu-latest
        - python-version: '3.15'
          os: macos-latest
    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v6
      with:
        fetch-depth: 0

    - name: Setup uv (cached)
      uses: astral-sh/setup-uv@v8.2.0
      with:
        enable-cache: true
        cache-suffix: ${{ matrix.os }}-${{ matrix.python-version }}
        cache-dependency-glob: "**/pyproject.toml"

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v6
      with:
        python-version: ${{ matrix.python-version }}
        allow-prereleases: true

    - name: Add locale for locale test
      if: runner.os == 'Linux' && matrix.python-version != '3.11'
      run: sudo locale-gen fr_FR.UTF-8

    - name: Install
      run: |
        uv venv --python ${{ matrix.python-version }}
        uv pip install -e.[test] pytest-github-actions-annotate-failures

    - name: Setup SSH tests
      if: runner.os != 'Windows'
      run: |
        set -eux

        mkdir -p ~/.ssh
        chmod 700 ~/.ssh
        cat <<'EOF' >> ~/.ssh/config
        StrictHostKeyChecking no
        UserKnownHostsFile /dev/null
        NoHostAuthenticationForLocalhost yes
        EOF

        ssh-keygen -q -f ~/.ssh/id_rsa -N '' -t rsa -m PEM
        cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
        chmod 600 ~/.ssh/authorized_keys

        ls -la ~
        ssh localhost -vvv "echo 'Worked!'"

    - name: Test with pytest
      run: uv run pytest --cov --run-optional-tests=ssh,sudo

    - name: Upload coverage
      run: uvx coveralls --service=github
      env:
        COVERALLS_PARALLEL: true
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        COVERALLS_FLAG_NAME: test-${{ matrix.os }}-${{ matrix.python-version }}

  coverage:
    needs: [tests]
    runs-on: ubuntu-slim
    steps:
    - name: Install coveralls
      run: pipx install coveralls
    - name: Coveralls Finished
      run: coveralls --service=github --finish
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
