name: Pytest

on:
  push:
    branches:
      - 'master'
  pull_request:
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:


jobs:
  generate_ctypes:
    runs-on: windows-latest
    timeout-minutes: 5
    steps:
        - uses: actions/checkout@v4
        - uses: actions/setup-python@v5
          with:
            python-version: '3.11'
        - run:
            py .\ctypes_generation\generate.py
        - name: Check generated code can execute
          run: py -c "import windows.generated_def"
  tests:
    strategy:
      fail-fast: false
      matrix:
        runs-on: [windows-2019, windows-latest]
        python-version: [2.7, 3.6, 3.11]
        python-architecture: [x86, x64]
        include:
            # Translate architecture to bitness for py.exe commandline
            - python-bitness-to-test: 32
              python-architecture: x86
            - python-bitness-to-test: 64
              python-architecture: x64
    # Not a real dependency : but starting tests when ctypes generation is broken is not useful
    needs: generate_ctypes
    timeout-minutes: 15
    runs-on: ${{ matrix.runs-on }}

    steps:
      - uses: actions/checkout@v4
      # Pfw testing need both 32b & 64b of tested python version for cross-bitness python injection tests

      ## Install the 32bits version of python3 asked
      - name: Set up Python3 ${{ matrix.python-version }} x86
        if: ${{ matrix.python-version != '2.7' }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version}}
          architecture: x86
          update-environment: false
      ## Install the 64bits version of python3 asked
      - name: Set up Python3 ${{ matrix.python-version }} x64
        if: ${{ matrix.python-version != '2.7' }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version}}
          architecture: x64
          update-environment: false

      # Manually install python2.7 (both version at once)
      - name: Set up Python2.7 ${{ matrix.python-version }} x86 & x64
        shell: bash
        if: ${{ matrix.python-version == '2.7' }}
        run: |
            choco install python2 --version=2.7.18 -y --no-progress --params '"/InstallDir:C:\tools\python27"'
            choco install python2 --version=2.7.18 -y --no-progress -x86 --params '"/InstallDir:C:\tools\python2732"' --force

      - name: Listing python versions availables
        run: py -0

      # Install PythonForWindows for both bitness
      - name: Installing PythonForWindows for both bitness
        run: |
            py -${{ matrix.python-version}}-32 setup.py install
            py -${{ matrix.python-version}}-64 setup.py install

      - name: Installing pytest & capstone-windows
        run: py -${{ matrix.python-version}}-${{ matrix.python-bitness-to-test}} -m pip install pytest pytest-timeout capstone-windows

      # Testing
      - name: Testing
        run: py -${{ matrix.python-version}}-${{ matrix.python-bitness-to-test}} -m pytest --junitxml=junit/test-results.xml -s -v tests/

      - name: Publish PyTest Results
        uses: EnricoMi/publish-unit-test-result-action/windows@v2
        if: always()
        with:
          files: junit/test-results.xml
          check_name: PyTest Results for ${{ matrix.python-version}}-${{ matrix.python-bitness-to-test}}
          secondary_rate_limit_wait_seconds: 90
          seconds_between_github_writes: 10
          seconds_between_github_reads: 1
