name: test

on:
  pull_request:
  workflow_dispatch:
  workflow_call:
    inputs:
      branch:
        description: "ref branch for this workflow"
        default: "master"
        required: true
        type: string

env:
  CARGO_TERM_COLOR: always

jobs:
  benchsuite:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4.2.2
      with:
        ref: ${{ inputs.branch }}
    - name: Install Prerequisites
      run: bin/install_prerequisites_ubuntu.sh
    - name: Install python3 venv
      run: |
        bin/install_python3_venv.sh
        export PATH="/tmp/hurl-python3-venv/bin:$PATH"
        echo "PATH=$PATH" >> $GITHUB_ENV
        which python3
        python3 --version
        pip --version
    - name: Install Rust
      run: bin/install_rust.sh
    - name: Environment
      run: bin/environment.sh
    - name: Build
      run: |
        bin/release/release.sh
        echo "PATH=${PWD}/target/release:$PATH" >> "${GITHUB_ENV}"
    - name: Archive artifacts
      uses: actions/upload-artifact@v4.4.3
      if: ${{ always() }}
      with:
        name: tests-bench-artifacts
        path: |
          bench/server.log

  test-ubuntu-x64-latest:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        rust: [stable]
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4.2.2
      with:
        ref: ${{ inputs.branch }}
    - name: Install Python 3.10
      uses: actions/setup-python@v5.3.0
      with:
        python-version: '3.10'
    - name: Install Prerequisites
      run: bin/install_prerequisites_ubuntu.sh
    - name: Install python3 venv
      run: |
        bin/install_python3_venv.sh
        export PATH="/tmp/hurl-python3-venv/bin:$PATH"
        echo "PATH=$PATH" >> $GITHUB_ENV
        which python3
        python3 --version
        pip --version
    - name: Install Rust
      run: bin/install_rust.sh
    - name: Environment
      run: bin/environment.sh
    - name: Test Prerequisites
      run: bin/test/test_prerequisites.sh
    - name: Tests units
      run: |
        bin/test/test_unit.sh
        bin/release/release.sh
        echo "PATH=${PWD}/target/release:$PATH" >> "${GITHUB_ENV}"
    - name: Integration Tests
      run: bin/test/test_integ.sh
    - name: Coverage
      run: bin/check/coverage.sh
    - name: Archive production artifacts
      uses: actions/upload-artifact@v4.4.3
      if: ${{ always() }}
      with:
        name: tests-ubuntu-latest-${{ matrix.rust }}-artifacts
        path: |
          ./**/*.log
          ./**/report/*.json
          ./**/report/*.xml

  test-ubuntu-x64-20-04:
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        rust: [stable]
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4.2.2
      with:
        ref: ${{ inputs.branch }}
    - name: Install Python 3.10
      uses: actions/setup-python@v5.3.0
      with:
        python-version: '3.10'
    - name: Install Prerequisites
      run: bin/install_prerequisites_ubuntu.sh
    - name: Install python3 venv
      run: |
        bin/install_python3_venv.sh
        export PATH="/tmp/hurl-python3-venv/bin:$PATH"
        echo "PATH=$PATH" >> $GITHUB_ENV
        which python3
        python3 --version
        pip --version
    - name: Install Rust
      run: bin/install_rust.sh
    - name: Environment
      run: bin/environment.sh
    - name: Test Prerequisites
      run: bin/test/test_prerequisites.sh
    - name: Tests units
      run: |
        bin/test/test_unit.sh
        bin/release/release.sh
        echo "PATH=${PWD}/target/release:$PATH" >> "${GITHUB_ENV}"
    - name: Integration Tests
      run: bin/test/test_integ.sh
    - name: Archive production artifacts
      uses: actions/upload-artifact@v4.4.3
      if: ${{ always() }}
      with:
        name: tests-ubuntu-20.04-${{ matrix.rust }}-artifacts
        path: |
          ./**/*.log
          ./**/report/*.json
          ./**/report/*.xml

  test-docker-archlinux-x64:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4.2.2
      with:
        ref: ${{ inputs.branch }}
    - name: Build, Test units and Integration tests
      uses: addnab/docker-run-action@v3
      with:
        image: archlinux
        options: --volume ${{ github.workspace }}:/work --workdir /work --privileged --env CARGO_TERM_COLOR=always
        run: |
          set -e
          echo "::group::Install Prerequisites"
            bin/install_prerequisites_archlinux.sh
          echo "::endgroup::"
          echo "::group::Install python3 venv"
            bin/install_python3_venv.sh
            export PATH=/tmp/hurl-python3-venv/bin:$PATH
            which python3
            python3 --version
            pip --version
          echo "::endgroup::"
          echo "::group::Install Rust"
            bin/install_rust.sh
          echo "::endgroup::"
          echo "::group::Environment"
            bin/environment.sh
          echo "::endgroup::"
          echo "::group::Tests"
            bin/test/test.sh
          echo "::endgroup::"
    - name: Archive production artifacts
      uses: actions/upload-artifact@v4.4.3
      if: ${{ always() }}
      with:
        name: tests-archlinux-x64-artifacts
        path: |
          ./**/*.log

  test-docker-fedora-x64:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4.2.2
      with:
        ref: ${{ inputs.branch }}
    - name: Build, Test units and Integration tests
      uses: addnab/docker-run-action@v3
      with:
        image: fedora
        options: --volume ${{ github.workspace }}:/work --workdir /work --privileged --env CARGO_TERM_COLOR=always
        run: |
          set -e
          echo "::group::Disable PAM for sudo with root and no tty"
            bin/disable_pam_for_sudo.sh
          echo "::endgroup::"          
          echo "::group::Install Prerequisites"
            bin/install_prerequisites_fedora.sh
          echo "::endgroup::"
          echo "::group::Install python3 venv"
            bin/install_python3_venv.sh
            export PATH=/tmp/hurl-python3-venv/bin:$PATH
            which python3
            python3 --version
            pip --version
          echo "::endgroup::"
          echo "::group::Install Rust"
            bin/install_rust.sh
          echo "::endgroup::"
          echo "::group::Environment"
            bin/environment.sh
          echo "::endgroup::"
          echo "::group::Tests"
            bin/test/test.sh
          echo "::endgroup::"
    - name: find artifacts
      run: |
        pwd
        find . -name "*.log"
    - name: Archive production artifacts
      uses: actions/upload-artifact@v4.4.3
      if: ${{ always() }}
      with:
        name: tests-fedora-x64-artifacts
        path: |
          ./**/*.log

  test-docker-alpine-x64:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4.2.2
      with:
        ref: ${{ inputs.branch }}
    - name: Build, Test units and Integration tests
      uses: addnab/docker-run-action@v3
      with:
        image: alpine
        options: --volume ${{ github.workspace }}:/work --workdir /work --privileged --env CARGO_TERM_COLOR=always
        run: |
          set -e
          apk update --quiet
          apk add --quiet bash
          echo "::group::Install Prerequisites"
            bin/install_prerequisites_alpine.sh
          echo "::endgroup::"
          echo "::group::Install python3 venv"
            bin/install_python3_venv.sh
            export PATH=/tmp/hurl-python3-venv/bin:$PATH
            which python3
            python3 --version
            pip --version
          echo "::endgroup::"
          echo "::group::Install Rust"
            bin/install_rust.sh
          echo "::endgroup::"
          echo "::group::Environment"
            bin/environment.sh
          echo "::endgroup::"
          echo "::group::Tests"
            bin/test/test.sh
          echo "::endgroup::"
    - name: find artifacts
      run: |
        pwd
        find . -name "*.log"
    - name: Archive production artifacts
      uses: actions/upload-artifact@v4.4.3
      if: ${{ always() }}
      with:
        name: tests-alpine-x64-artifacts
        path: |
          ./**/*.log

  test-macos-x64:
    runs-on: macos-13
    strategy:
      matrix:
        rust: [stable]
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4.2.2
      with:
        ref: ${{ inputs.branch }}
    - name: Install Prerequisites
      run: |
        bin/install_prerequisites_macos.sh
        echo "PATH=$PATH" >> $GITHUB_ENV
    - name: Install python3 venv
      run: |
        bin/install_python3_venv.sh
        export PATH="/tmp/hurl-python3-venv/bin:$PATH"
        echo "PATH=$PATH" >> $GITHUB_ENV
        which python3
        python3 --version
        pip --version
    - name: Install Rust
      run: bin/install_rust.sh
    - name: Environment
      run: bin/environment.sh
    - name: Test Prerequisites
      run: |
        bin/test/test_prerequisites.sh
    - name: Tests units
      run: |
        bin/test/test_unit.sh
        bin/release/release.sh
        echo "PATH=${PWD}/target/release:$PATH" >> "${GITHUB_ENV}"
    - name: Integration Tests
      run: bin/test/test_integ.sh
    - name: find artifacts
      run: |
        pwd
        find . -name "*.log"
    - name: Archive production artifacts
      uses: actions/upload-artifact@v4.4.3
      if: ${{ always() }}
      with:
        name: tests-macos-x64-${{ matrix.rust }}-artifacts
        path: |
          ./**/*.log

  test-macos-aarch64:
    runs-on: macos-14
    strategy:
      matrix:
        rust: [stable]
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4.2.2
      with:
        ref: ${{ inputs.branch }}
    - name: Install Prerequisites
      run: |
        bin/install_prerequisites_macos.sh
        echo "PATH=$PATH" >> $GITHUB_ENV
    - name: Install python3 venv
      run: |
        bin/install_python3_venv.sh
        export PATH="/tmp/hurl-python3-venv/bin:$PATH"
        echo "PATH=$PATH" >> $GITHUB_ENV
        which python3
        python3 --version
        pip --version
    - name: Install Rust
      run: bin/install_rust.sh
    - name: Environment
      run: bin/environment.sh
    - name: Test Prerequisites
      run: |
        bin/test/test_prerequisites.sh
    - name: Tests units
      run: |
        bin/test/test_unit.sh
        bin/release/release.sh
        echo "PATH=${PWD}/target/release:$PATH" >> "${GITHUB_ENV}"
    - name: Integration Tests
      run: bin/test/test_integ.sh
    - name: find artifacts
      run: |
        pwd
        find . -name "*.log"
    - name: Archive production artifacts
      uses: actions/upload-artifact@v4.4.3
      if: ${{ always() }}
      with:
        name: tests-macos-aarch64-${{ matrix.rust }}-artifacts
        path: |
          ./**/*.log

  test-windows-x64:
    runs-on: windows-latest
    strategy:
      matrix:
        rust: [stable]
    env:
      VCPKGRS_DYNAMIC: 1
    steps:
    - name: Set git to use LF
      run: |
        git config --global core.autocrlf false
        git config --global core.eol lf
    - name: Checkout repository
      uses: actions/checkout@v4.2.2
      with:
        ref: ${{ inputs.branch }}
    - name: Install Rust
      run: |
        .\bin\install_rust.ps1
    - name: Manage vcpkg cache
      uses: actions/cache@v4.1.2
      with:
        path: C:\vcpkg
        key: ${{ runner.os }}-test-windows-x64
    - name: Environment
      run: .\bin\environment.ps1
    - name: Install Prerequisites
      run: .\bin\install_prerequisites_windows.ps1
    - name: Build and test
      run: .\bin\test\test.ps1
    - name: Archive production artifacts
      uses: actions/upload-artifact@v4.4.3
      if: ${{ always() }}
      with:
        name: tests-win64-${{ matrix.rust }}-artifacts
        path: |
          ./**/*.log

  test-windows-wsl2-x64:
    runs-on: windows-latest
    steps:
    - name: Set git to use LF
      run: |
        git config --global core.autocrlf false
        git config --global core.eol lf
    - name: Checkout repository
      uses: actions/checkout@v4.2.2
      with:
       ref: ${{ inputs.branch }}
    - name: Update wsl
      run: |
        wsl --update
        wsl --version
    - name: Setup wsl Ubuntu
      uses: Vampire/setup-wsl@v3.1.3
      with:
        distribution: Ubuntu-22.04
    - name: Convert to wsl2
      run: |
        wsl --set-version Ubuntu 2
        wsl --list --verbose --all
    - shell: wsl-bash {0}
      run: |
        # Build, Test units and Integration tests
        echo "::group::Move to linux compatible workspace"
          win_workspace="${{ github.workspace }}"
          wsl_workspace="/mnt/$(echo "${win_workspace}" | tr -d ':'  | tr '\\' '/' | tr '[:upper:]' '[:lower:]')"
          linux_workspace="/tmp/hurl"
          cp -frp "${wsl_workspace}" "${linux_workspace}"
          cd "${linux_workspace}"
          pwd
          ls
        echo "::endgroup::"
        echo "::group::Install Prerequisites"
          bin/install_prerequisites_ubuntu.sh
        echo "::endgroup::"
        echo "::group::Install python3 venv"
          bin/install_python3_venv.sh
          export PATH=/tmp/hurl-python3-venv/bin:$PATH
          which python3
          python3 --version
          pip --version
        echo "::endgroup::"
        echo "::group::Install Rust"
          bin/install_rust.sh
        echo "::endgroup::"
        echo "::group::Environment"
          bin/environment.sh
        echo "::endgroup::"
        echo "::group::Tests"
          bin/test/test.sh
        echo "::endgroup::"
    - name: Archive production artifacts
      uses: actions/upload-artifact@v4.4.3
      if: ${{ always() }}
      with:
        name: tests-win64-wsl2-artifacts
        path: |
          ./**/*.log
