name: Test Valgrind

# like the Docker tests, but running valgrind only on *.c/*.h changes.

on:
  push:
    branches:
      - "**"
    paths: &paths
      - ".github/workflows/test-valgrind.yml"
      - "**.c"
      - "**.h"
  pull_request:
    paths: *paths
  workflow_dispatch:

permissions:
  contents: read

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

env:
  FORCE_COLOR: 1

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        docker: [
          ubuntu-24.04-noble-amd64-valgrind,
        ]
        dockerTag: [main]
        split: [1, 2, 3, 4]

    name: ${{ matrix.docker }} ${{ matrix.split }}

    steps:
    - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      with:
        persist-credentials: false

    - name: Build system information
      run: python3 .github/workflows/system-info.py

    - name: Docker pull
      run: |
        docker pull pythonpillow/${{ matrix.docker }}:${{ matrix.dockerTag }}

    - name: Build and Run Valgrind
      run: |
        # The Pillow user in the docker container is UID 1001
        sudo chown -R 1001 $GITHUB_WORKSPACE
        docker run --name pillow_container -e "PILLOW_VALGRIND_TEST=true" -e "SPLIT_COUNT=4" -e "SPLIT_INDEX=${{ matrix.split }}" -v $GITHUB_WORKSPACE:/Pillow pythonpillow/${{ matrix.docker }}:${{ matrix.dockerTag }}
        sudo chown -R runner $GITHUB_WORKSPACE
