name: test

on: [push, pull_request]

env:
  WIN_GRAPHVIZ_URL: "https://gitlab.com/graphviz/graphviz/-/package_files/6164164/download"
  WIN_GRAPHVIZ_EXE: "C:\\Temp\\graphviz-install-2.46.0-win64.exe"
  WIN_GRAPHVIZ_DIR: "C:\\Program Files\\Graphviz"

jobs:
  ubuntu:
    runs-on: Ubuntu-latest

    strategy:
      matrix:
        python-version: ["3.10", "3.11", "3.12", "3.13-dev", "pypy-3.10"]

    steps:
      - name: Checkout pygraphviz
        uses: actions/checkout@v4

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

      - name: Update apt-get
        run: sudo apt-get update

      - name: Install graphviz
        run: sudo apt-get install graphviz graphviz-dev

      - name: Install packages
        run: |
          pip install --upgrade pip
          pip install -r requirements/test.txt
          pip install .
          pip list

      - name: Test pygraphviz
        run: |
          pytest --doctest-modules --durations=10 --pyargs pygraphviz

  fedora:
    strategy:
      matrix:
        release:
          - 39
          - 40

    runs-on: Ubuntu-latest
    container: "fedora:${{ matrix.release }}"

    steps:
      - name: Checkout pygraphviz
        uses: actions/checkout@v4

      - name: Install graphviz
        run: sudo dnf install --nogpg -y graphviz graphviz-devel

      - name: Install Python developer tools
        run: sudo dnf install --nogpg -y python3-devel

      - name: Install gcc
        run: sudo dnf install -y --nogpg gcc

      - name: Install packages
        run: |
          pip install --upgrade pip
          pip install -r requirements/test.txt
          pip install .
          pip list

      - name: Test pygraphviz
        run: |
          pytest --doctest-modules --durations=10 --pyargs pygraphviz

  brew:
    runs-on: macOS-latest

    strategy:
      matrix:
        python-version: ["3.10", "3.11", "3.12", "3.13-dev"]

    steps:
      - name: Checkout pygraphviz
        uses: actions/checkout@v4

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

      - name: Install graphviz
        run: brew install graphviz

      - name: Install packages
        run: |
          pip install --upgrade pip
          pip install -r requirements/test.txt
          pip install --config-settings="--global-option=build_ext" \
                      --config-settings="--global-option=-I$(brew --prefix graphviz)/include/" \
                      --config-settings="--global-option=-L$(brew --prefix graphviz)/lib/" \
                      .
          pip list

      - name: Test pygraphviz
        run: |
          pytest --doctest-modules --durations=10 --pyargs pygraphviz

  macports:
    runs-on: macOS-latest

    strategy:
      matrix:
        python-version: ["3.10", "3.11", "3.12", "3.13-dev"]

    steps:
      - name: Checkout pygraphviz
        uses: actions/checkout@v4

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

      - name: Download macports pkg
        run: wget https://github.com/macports/macports-base/releases/download/v2.9.3/MacPorts-2.9.3-14-Sonoma.pkg

      - name: Install macports
        run: sudo installer -pkg MacPorts-2.9.3-14-Sonoma.pkg -target /

      - name: Install graphviz
        run: sudo /opt/local/bin/port install graphviz

      - name: Install packages
        run: |
          pip install --upgrade pip setuptools
          pip install -r requirements/test.txt
          pip install --config-settings="--global-option=build_ext" \
                      --config-settings="--global-option=-I/opt/local/include/" \
                      --config-settings="--global-option=-L/opt/local/lib/" \
                      .
          pip list

      - name: Test pygraphviz
        run: |
          export PATH=/opt/local/bin:/opt/local/sbin:$PATH
          pytest --doctest-modules --durations=10 --pyargs pygraphviz

  windows:
    runs-on: windows-latest

    strategy:
      matrix:
        python-version: ["3.10", "3.11", "3.12", "3.13-dev"]

    steps:
      - name: Checkout pygraphviz
        uses: actions/checkout@v4

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

      # This and the following step need to be changed when Graphviz has new releases.
      - name: Download graphviz source
        run: Invoke-WebRequest -Uri $env:WIN_GRAPHVIZ_URL -OutFile $env:WIN_GRAPHVIZ_EXE

      - name: Install exe
        run: Start-Process -Wait -FilePath $env:WIN_GRAPHVIZ_EXE -ArgumentList '/S' -PassThru

      - name: Update path
        run: echo "$($env:WIN_GRAPHVIZ_DIR)\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

      # We need ``python -m pip install -U pip`` b/c Windows won't modify running programs.
      - name: Install packages
        run: |
          python -m pip install --upgrade pip
          python -m pip install -r requirements/test.txt
          python -m pip install --config-settings="--global-option=build_ext" `
            --config-settings="--global-option=-I$($env:WIN_GRAPHVIZ_DIR)\include" `
            --config-settings="--global-option=-L$($env:WIN_GRAPHVIZ_DIR)\lib" `
            .
          python -m pip list

      # We are leaving this in to help us remember some useful PowerShell syntax.
      # The following commands are also helpful for debugging:  Copy-Item, Get-Content
      - name: List
        run: Get-ChildItem "$($env:WIN_GRAPHVIZ_DIR)\bin"

      # Set PY_IGNORE_IMPORTMISMATCH b/c otherwise --doctest-modules is confused.
      - name: Test pygraphviz
        run: |
          [Environment]::SetEnvironmentVariable("PY_IGNORE_IMPORTMISMATCH", 1)
          pytest --doctest-modules --durations=10 --pyargs pygraphviz
