# SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
#
# SPDX-License-Identifier: MIT

name: CI

on:
  push:
    branches:
      - master
      - kdsoap-2.1
      - kdsoap-2.2
  pull_request:
    branches:
      - master
      - kdsoap-2.1
      - kdsoap-2.2

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: true
      matrix:
        os:
          - ubuntu-22.04
          - windows-2022
          - macos-12

        build_type:
          - Debug
          - Release

        link_type:
          - static
          - shared

        config:
          - qt_version: "5.15"
          - qt_version: "6.5.*"

    steps:
      - name: Install Qt with options and default aqtversion
        uses: jurplel/install-qt-action@v3
        with:
          aqtversion: null # use whatever the default is
          modules: ${{ matrix.config.modules }}
          version: ${{ matrix.config.qt_version }}
          cache: true

      - name: Checkout sources
        uses: actions/checkout@v4

      - name: Fetch Git submodule
        run: git submodule update --init --recursive

      - name: Install ninja-build tool (must be after Qt due PATH changes)
        uses: turtlesec-no/get-ninja@main

      - name: Make sure MSVC is found when Ninja generator is in use
        if: ${{ runner.os == 'Windows' }}
        uses: ilammy/msvc-dev-cmd@v1

      - name: Configure project
        run: >
          cmake -S . -B ./build -G Ninja
          --warn-uninitialized -Werror=dev
          -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
          -DKDSoap_QT6=${{ startsWith(matrix.config.qt_version, '6.') }}
          -DKDSoap_STATIC=${{ matrix.build_type == 'static' }}
          -DKDSoap_TESTS=${{ matrix.build_type == 'Debug' }}
          -DKDSoap_EXAMPLES=${{ matrix.build_type == 'Debug' }}
          -DKDSoap_DOCS=${{ matrix.build_type == 'Debug' && runner.os == 'Linux' }}

      - name: Build Project
        run: cmake --build ./build

      - name: Run tests on Linux (offscreen)
        if: ${{ matrix.build_type == 'Debug' && runner.os == 'Linux' }}
        run: >
          ctest --test-dir ./build -C ${{ matrix.build_type }} --output-on-failure
          -E 'kdsoap-test_wsdl_rpc'
        env:
          QT_QPA_PLATFORM: offscreen

      - name: Run tests on Windows
        if: ${{ matrix.build_type == 'Debug' && runner.os == 'Windows' }}
        run: ctest --test-dir ./build -C ${{ matrix.build_type }} --output-on-failure

      - name: Run tests on macOS
        if: ${{ matrix.build_type == 'Debug' && runner.os != 'macOS' }}
        run: >
          ctest --test-dir ./build -C ${{ matrix.build_type }} --output-on-failure
          -E 'kdsoap-test_serverlib|kdsoap-test_wsdl_document'

      - name: Read tests log when it fails
        uses: andstor/file-reader-action@v1
        if: ${{ failure() && matrix.build_type == 'Debug' }}
        with:
          path: "./build/Testing/Temporary/LastTest.log"
