name: Android Unit Tests

on:
  workflow_dispatch: {}
  push:
    branches: [master]
    paths:
      - .github/workflows/android-unit-tests.yml
      - 'android/**'
      - 'fastlane/**'
      - 'tools/**'
      - yarn.lock
  pull_request:
    branches: [master]
    paths:
      - .github/workflows/android-unit-tests.yml
      - 'android/**'
      - 'fastlane/**'
      - 'tools/**'
      - yarn.lock

jobs:
  test:
    runs-on: ubuntu-18.04
    steps:
      - name: Check out repository
        uses: actions/checkout@v2
        with:
          submodules: true
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - uses: actions/cache@v2
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - run: yarn install --frozen-lockfile
      - uses: actions/cache@v2
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('android/*.gradle*') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - uses: actions/cache@v2
        id: cache-android-ndk
        with:
          path: /usr/local/lib/android/sdk/ndk/19.2.5345600/
          key: ${{ runner.os }}-ndk-19.2.5345600
          restore-keys: |
            ${{ runner.os }}-ndk-
      - name: Install NDK
        if: steps.cache-android-ndk.outputs.cache-hit != 'true'
        run: |
          sudo $ANDROID_HOME/tools/bin/sdkmanager --install "ndk;19.2.5345600"
      - run: echo "$(pwd)/bin" >> $GITHUB_PATH
      - name: Run native Android unit tests
        env:
          ANDROID_NDK_HOME: /usr/local/lib/android/sdk/ndk/19.2.5345600/
        run: expotools native-unit-tests --platform android
      - name: Save test results
        if: always()
        uses: actions/upload-artifact@v2
        with:
          name: test-results
          path: packages/**/build/test-results/**/*xml
