name: Coverage
on:
  workflow_call:

jobs:
  BuildAndTestAndCoverage:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Fetch tags
        run: git fetch --tags -p
      - name: Check KVM
        run: |
          sudo apt update
          sudo apt install -y kmod qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
          echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm

      - uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: gradle
      - uses: gradle/actions/setup-gradle@v3
      #- uses: android-actions/setup-android@v3
      - name: Configure gradle...
        run: mkdir -p ~/.gradle && echo "org.gradle.daemon=false\nignoreGitVersion=true" >> ~/.gradle/gradle.properties
      - uses: KengoTODA/actions-setup-docker-compose@v1
        with:
          version: "2.23.3"
      - name: run selfoss
        run: |
          docker compose -f .gitea/workflows/assets/docker-compose.yml up -d
      - name: Tests
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: 29
          arch: x86_64
          script: ./gradlew androidApp:connectedAndroidTest
      - uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: failure-espresso
          path: build/reports/androidTests/connected/screenshots
          retention-days: 2
          overwrite: true
          include-hidden-files: true
      - uses: actions/upload-artifact@v3
        with:
          name: coverage-espresso
          path: build/reports/coverage/androidTest/githubConfig/debug/connected
          retention-days: 1
          overwrite: true
          include-hidden-files: true
      - name: Clean
        if: always()
        run: |
          docker compose -f .gitea/workflows/assets/docker-compose.yml stop