# Secrets
#   UNITY_LICENSE:
name: test

on:
  push:
    branches:
      - develop
    tags:
      - "!*"
  pull_request:
    types:
      - opened
      - synchronize

jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      versions: ${{ steps.setup.outputs.versions }}
    steps:
      - id: setup
        run: |
          VERSIONS=`npx unity-changeset list --versions --all --latest-patch --min 2018.3 --json`
          echo "==== Target Unity Versions ===="
          echo "${VERSIONS}"
          echo "::set-output name=versions::${VERSIONS}"

  test:
    runs-on: ubuntu-latest
    needs: setup
    strategy:
      fail-fast: false
      matrix:
        version: ${{ fromJson(needs.setup.outputs.versions) }}

    steps:
      # Checkout sandbox project
      - uses: actions/checkout@v2
        with:
          ref: sandbox
          submodules: true
          fetch-depth: 0

      # Update package submodule
      - name: "Update package submodule"
        working-directory: Packages/dev
        run: git checkout ${{ github.sha }}

      # Cache
      - uses: actions/cache@v2
        with:
          path: Library
          key: Library-${{ matrix.version }}-${{ github.sha }}
          restore-keys: |
            Library-${{ matrix.version }}-
            Library-

      # Run tests
      - name: "Run tests"
        uses: game-ci/unity-test-runner@v2
        with:
          customImage: mobsakai/unity3d:${{ matrix.version }}
          customParameters: -nographics
          # unityVersion: ${{ matrix.version }}
          checkName: ${{ matrix.version }} Test Results
          githubToken: ${{ github.token }}
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
