name: CTS

on:
  push:
    branches: [master, staging]
    tags: [v0.*]
  pull_request:
    types: [labeled, opened, synchronize]

env:
  CARGO_INCREMENTAL: false
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: full
  MSRV: 1.65

jobs:
  cts:
    # Only run if we add this label
    if: contains(github.event.pull_request.labels.*.name, 'needs testing')

    strategy:
      fail-fast: false
      matrix:
        include:
          # Windows
          - name: Windows x86_64
            os: windows-2019
            target: x86_64-pc-windows-msvc
            backends: dx12 # dx11

          # Linux
          #- name: Linux x86_64
          #  os: ubuntu-20.04
          #  target: x86_64-unknown-linux-gnu
          #  backends: vulkan # gl

    name: CTS ${{ matrix.name }}
    runs-on: ${{ matrix.os }}

    steps:
      - name: checkout repo
        uses: actions/checkout@v3
        with:
          path: wgpu

      - name: checkout cts
        run: |
          git clone https://github.com/gpuweb/cts.git
          cd cts
          git checkout $(cat ../wgpu/cts_runner/revision.txt)

      - name: Install MSRV toolchain
        run: |
          rustup toolchain install ${{ env.MSRV }} --no-self-update --profile=minimal --target ${{ matrix.target }}
          rustup default ${{ env.MSRV }}

      - name: caching
        uses: Swatinem/rust-cache@v2
        with:
          key: cts-a # suffix for cache busting
          working-directory: wgpu/cts_runner
          target-dir: wgpu/cts_runner/target

      - name: install llvmpipe and lavapipe
        if: matrix.os == 'ubuntu-20.04'
        run: |
          sudo apt-get update -y -qq
          sudo add-apt-repository ppa:oibaf/graphics-drivers -y
          sudo apt-get update
          sudo apt install -y libxcb-xfixes0-dev mesa-vulkan-drivers
        # libegl1-mesa libgl1-mesa-dri for gl

      # We enable line numbers for panics, but that's it
      - name: disable debug
        shell: bash
        run: |
          mkdir wgpu/.cargo
          echo """[profile.dev]
          debug = 1" > wgpu/.cargo/config.toml

      - name: build CTS runner
        run: |
          cargo build --manifest-path wgpu/cts_runner/Cargo.toml

      - name: run CTS
        shell: bash
        run: |
          cd cts;
          for backend in ${{ matrix.backends }}; do
            echo "======= CTS TESTS $backend ======";
            grep -v '^//' ../wgpu/cts_runner/test.lst | while IFS=$' \t\r\n' read test; do
              echo "=== Running $test ===";
              DENO_WEBGPU_BACKEND=$backend cargo run --manifest-path ../wgpu/cts_runner/Cargo.toml --frozen -- ./tools/run_deno --verbose "$test";
            done
          done
