name: Android Unit Tests

on:
  workflow_dispatch: {}
  push:
    branches: [main]
    paths:
      - .github/workflows/android-unit-tests.yml
      - android/**
      - fastlane/**
      - packages/**/android/**
      - tools/**
      - yarn.lock
      - '!packages/@expo/cli/**'
  pull_request:
    paths:
      - .github/workflows/android-unit-tests.yml
      - android/**
      - fastlane/**
      - packages/**/android/**
      - tools/**
      - yarn.lock
      - '!packages/@expo/cli/**'

concurrency:
  group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    runs-on: ubuntu-20.04
    timeout-minutes: 60
    env:
      ORG_GRADLE_PROJECT_reactNativeArchitectures: x86_64
      GRADLE_OPTS: -Dorg.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=1024m
    steps:
      - name: ⬢ Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: '14.17'
      - name: 👀 Check out repository
        uses: actions/checkout@v3
        with:
          submodules: true
      - name: 🔨 Use JDK 11
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '11'
      - name: ♻️ Restore caches
        uses: ./.github/actions/expo-caches
        id: expo-caches
        with:
          yarn-workspace: 'true'
          yarn-tools: 'true'
          hermes-engine-aar: 'true'
      - name: ➕ Add `bin` to GITHUB_PATH
        run: echo "$(pwd)/bin" >> $GITHUB_PATH
      - name: 🧶 Install node modules in root dir
        if: steps.expo-caches.outputs.yarn-workspace-hit != 'true'
        run: yarn install --frozen-lockfile
      - name: 🚨 Run Spotless lint check
        working-directory: android
        run: ./gradlew spotlessCheck || { echo '::error Spotless lint failed. Run `./gradlew spotlessApply` to automatically fix formatting.' && exit 1; }
      - name: 🎸 Run native Android unit tests
        timeout-minutes: 30
        run: expotools native-unit-tests --platform android
      - name: 💾 Save test results
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: test-results
          path: packages/**/build/test-results/**/*xml
