# Syntax reference https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
# Environment reference https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners
name: valgrind

on:
  push:
    branches:
      - 'main'
      - 'releases/**'
      - '2.*'
    tags:
      - '2.*'
  pull_request:

permissions:
  contents: read

jobs:
  build:

    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: ccache
        uses: hendrikmuhs/ccache-action@v1.2
        with:
          key: ${{ github.workflow }}-${{ runner.os }}

      - name: Install missing software
        run: |
          sudo apt-get update
          sudo apt-get install libxml2-utils
          sudo apt-get install valgrind
          sudo apt-get install libboost-container-dev
          sudo apt-get install debuginfod

      - name: Build cppcheck
        run: |
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          make -j$(nproc) CXXOPTS="-Werror -O1 -g" CPPOPTS="-DHAVE_BOOST" HAVE_RULES=yes MATCHCOMPILER=yes CPPCHK_GLIBCXX_DEBUG=

      - name: Build test
        run: |
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          make -j$(nproc) CXXOPTS="-Werror -O1 -g" CPPOPTS="-DHAVE_BOOST" HAVE_RULES=yes MATCHCOMPILER=yes CPPCHK_GLIBCXX_DEBUG= testrunner

      - name: Run valgrind
        run: |
          ec=0
          # disabled all tests invoking processes because the child processes fail with still reachable memory.
          # some of the TestProcessExecutor* tests are also extremely slow.
          excluded_tests="TestProcessExecutorFS \
            TestProcessExecutorFiles \
            TestSuppressions::suppressionsSettingsProcessesFiles \
            TestSuppressions::suppressionsSettingsProcessesFS"
          valgrind --error-limit=yes --leak-check=full --num-callers=50 --show-reachable=yes --track-origins=yes --suppressions=valgrind/testrunner.supp --gen-suppressions=all -s --log-fd=9 --error-exitcode=42 ./testrunner -t -x $excluded_tests 9>memcheck.log || ec=1
          cat memcheck.log
          exit $ec
        # TODO: debuginfod.ubuntu.com is currently not responding to any requests causing it to run into a 40(!) minute timeout
        #env:
        #  DEBUGINFOD_URLS: https://debuginfod.ubuntu.com
          
      - uses: actions/upload-artifact@v4
        if: success() || failure()
        with:
          name: Logs
          path: ./*.log
