name: wf-cliprdr CI

on:
  workflow_dispatch:
  pull_request:
    paths:
      - "libs/clipboard/src/windows/**"
      - "tests/test_invariant_wf_cliprdr.c"
      - ".github/workflows/wf-cliprdr-ci.yml"
  push:
    branches:
      - master
    paths:
      - "libs/clipboard/src/windows/**"
      - "tests/test_invariant_wf_cliprdr.c"
      - ".github/workflows/wf-cliprdr-ci.yml"

permissions:
  contents: read

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

jobs:
  test:
    name: wf_cliprdr invariant test
    runs-on: windows-2022

    steps:
      - name: Checkout source code
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          persist-credentials: false

      - name: Set up MSVC
        uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756
        with:
          arch: x64

      - name: Setup vcpkg with GitHub Actions binary cache
        uses: lukka/run-vcpkg@b1a0dd252f06b9e25b3c022a9a03bd7a427fb6a2 # v11
        with:
          vcpkgDirectory: C:\vcpkg
          doNotCache: false

      - name: Install vcpkg dependency
        shell: pwsh
        run: |
          & "$env:VCPKG_ROOT\vcpkg.exe" install check:x64-windows --classic --x-install-root="$env:VCPKG_ROOT\installed"

      - name: Build test
        shell: pwsh
        run: |
          $testRoot = Join-Path $env:GITHUB_WORKSPACE 'build\wf-cliprdr'
          New-Item -ItemType Directory -Force $testRoot | Out-Null

          $testSource = (($env:GITHUB_WORKSPACE -replace '\\', '/') + '/tests/test_invariant_wf_cliprdr.c')
          $cmakeLists = @(
            'cmake_minimum_required(VERSION 3.20)'
            'project(test_invariant_wf_cliprdr C)'
            ''
            'set(CMAKE_C_STANDARD 11)'
            'set(CMAKE_C_STANDARD_REQUIRED ON)'
            'set(CMAKE_C_EXTENSIONS OFF)'
            ''
            'find_package(check CONFIG REQUIRED)'
            ''
            'add_executable(test_invariant_wf_cliprdr'
            '  "TEST_SOURCE"'
            ')'
            ''
            'target_link_libraries(test_invariant_wf_cliprdr PRIVATE'
            '  $<$<TARGET_EXISTS:Check::check>:Check::check>'
            '  $<$<NOT:$<TARGET_EXISTS:Check::check>>:Check::checkShared>'
            ')'
          ) -join [Environment]::NewLine
          $cmakeLists.Replace('TEST_SOURCE', $testSource) | Set-Content -NoNewline (Join-Path $testRoot 'CMakeLists.txt')

          cmake -S $testRoot -B (Join-Path $testRoot 'out') -G "Visual Studio 17 2022" -A x64 -DCMAKE_TOOLCHAIN_FILE="$env:VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake" -DVCPKG_TARGET_TRIPLET=x64-windows
          cmake --build (Join-Path $testRoot 'out') --config Release

      - name: Run test
        shell: pwsh
        run: .\build\wf-cliprdr\out\Release\test_invariant_wf_cliprdr.exe
