name: CI

on:
  push:
  pull_request:

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
    inputs:
      core-dump:
        description: "Set to 1 to enable retrieving core dump from crashes"
        default: "0"
jobs:
  test-linux:
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        python-version: ["3.7", "3.10"]

    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          cache: "pip"
          cache-dependency-path: "requirements*.txt"

      - name: Sets env var for security
        if: (github.event_name == 'pull_request' && contains(github.event.pull_request.body, 'security_test')) || (github.event_name == 'push' && contains(github.event.head_commit.message, 'security_test'))
        run: echo "SECURITY_TESTS=True" >> $GITHUB_ENV

      - name: Install dependencies
        run: |
          pip install --upgrade pip wheel
          pip install -r requirements.txt -r requirements-tests.txt
          pip install -e .

      - name: Install security dependencies
        if: contains(env.SECURITY_TESTS, 'True')
        run: |
          wget -O- $TESTSSL_URL$TESTSSL_VER | tar xz
          mv -t deluge/tests/data testssl.sh-$TESTSSL_VER/testssl.sh testssl.sh-$TESTSSL_VER/etc/;
        env:
          TESTSSL_VER: 3.0.6
          TESTSSL_URL: https://codeload.github.com/drwetter/testssl.sh/tar.gz/refs/tags/v

      - name: Setup core dump catch and store
        if: github.event.inputs.core-dump == '1'
        run: |
          sudo mkdir /cores/ && sudo chmod 777 /cores/
          echo "/cores/%E.%p" | sudo tee /proc/sys/kernel/core_pattern
          ulimit -c unlimited
          sudo apt install glibc-tools
          echo "DEBUG_PREFIX=catchsegv python -X dev -m" >> $GITHUB_ENV

      - name: Test with pytest
        run: |
          python -c 'from deluge._libtorrent import lt; print(lt.__version__)';
          $DEBUG_PREFIX pytest -v -m "not (todo or gtkui)" deluge

      - uses: actions/upload-artifact@v3
        # capture all crashes as build artifacts
        if: failure()
        with:
          name: crashes
          path: /cores

  test-windows:
    runs-on: windows-2022
    strategy:
      matrix:
        python-version: ["3.7", "3.10"]

    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          cache: "pip"
          cache-dependency-path: "requirements*.txt"

      - name: Install dependencies
        run: |
          pip install --upgrade pip wheel
          pip install -r requirements.txt -r requirements-tests.txt
          pip install -e .

      - name: Test with pytest
        run: |
          python -c 'import libtorrent as lt; print(lt.__version__)';
          pytest -v -m "not (todo or gtkui or security)" deluge
