name: Testing Catch2

on:
  push:
    branches: [ 'master', 'main', 'develop', 'actions_tests' ]
    tags: [ 'v*' ]
  pull_request:
    branches: [ 'master', 'main', 'develop' ]
  #schedule:
  #  - cron: '15 8 * * 3' # Run weekly

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive

    - name: Extract CoolProp version from CMakeLists.txt
      shell: bash
      run: |
        set -x
        COOLPROP_VERSION=$(python dev/extract_version.py --cmake-only)
        echo COOLPROP_VERSION=$COOLPROP_VERSION >> $GITHUB_ENV
        # Create the build directory too
        mkdir build

    - name: Configure CMake
      working-directory: ./build
      shell: bash
      run: cmake -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE -DCOOLPROP_SHARED_LIBRARY:BOOL=ON -DCOOLPROP_CATCH_MODULE:BOOL=ON ..

    - name: Build
      working-directory: ./build
      shell: bash
      run: |
        set -x
        cmake --build . --target install -j $(nproc) --config $BUILD_TYPE

    - name: Test
      working-directory: ./build
      shell: bash
      # Execute tests defined by the CMake configuration.
      # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
      run: |
        ctest -j $(nproc)

    - name: Rerun failed Tests
      if: failure()
      working-directory: ./build
      shell: bash
      # Execute tests defined by the CMake configuration.
      # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
      run: |
        ctest -j $(nproc) --rerun-failed --output-on-failure

    # TODO: pick one style, for now I'm doing this so I can report zero new failures compared to catch1
    - name: Rerun failed Tests in CatchTestRunner directly
      if: failure()
      working-directory: ./build
      shell: bash
      # Execute tests defined by the CMake configuration.
      # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
      run: |
        ./CatchTestRunner
