name: unity-test

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

jobs:
  unity-test:
    strategy:
      fail-fast: false
      matrix:
        unity:
          [
            "2018.3.14f1",
            "2018.4.25f1",
            "2019.1.14f1",
            "2019.2.11f1",
            "2019.3.15f1",
            "2019.4.8f1",
            "2020.1.5f1",
          ]

    runs-on: ubuntu-latest
    container:
      # Use Unity image from https://hub.docker.com/r/gableroux/unity3d/tags
      image: gableroux/unity3d:${{ matrix.unity }}

    steps:
      # Activate Unity Editor
      - name: Activate Unity Editor
        id: activation
        run: |
          apt-get update > /dev/null
          apt-get install software-properties-common -y > /dev/null
          apt-add-repository ppa:git-core/ppa -y > /dev/null
          apt-get update > /dev/null
          apt-get install git -y > /dev/null
          git clone https://gist.github.com/135c940b45ec4e385861c8711777db7f.git .ulfs
          chmod 755 .ulfs/activate.sh && .ulfs/activate.sh ${{ matrix.unity }}

      # (On failed activation) Upload unity activation file
      - name: Upload unity activation file
        uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: Unity_v${{ matrix.unity }}.alf
          path: ./*.alf

      # Checkout sandbox project
      - uses: actions/checkout@v2
        with:
          ref: "sandbox"
          submodules: "true"

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

      # Install codecoverage package
      - name: "Install codecoverage package"
        if: always() && steps.activation.conclusion == 'success'
        run: |
          [ -n "`echo ${{ matrix.unity }} | grep -e '\(2017.\|2018.\|2019.1.\|2019.2.\)'`" ] && exit 0
          npm i -g openupm-cli
          openupm add com.unity.testtools.codecoverage

      # Run playmode tests
      - name: "Run playmode tests"
        if: always() && steps.activation.conclusion == 'success'
        run: |
          xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' \
            /opt/Unity/Editor/Unity -batchmode -nographics -silent-crashes -logFile -projectPath . \
            -runTests -testPlatform playmode -enableCodeCoverage

      # Run editmode tests
      - name: "Run editmode tests"
        if: always() && steps.activation.conclusion == 'success'
        run: |
          xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' \
            /opt/Unity/Editor/Unity -batchmode -nographics -silent-crashes -logFile -projectPath . \
            -runTests -testPlatform editmode -enableCodeCoverage

      # Setup testspace
      - uses: testspace-com/setup-testspace@v1
        if: always() && steps.activation.conclusion == 'success'
        with:
          domain: ${{ github.repository_owner }}

      # Push test results
      - name: Push test results
        if: always() && steps.activation.conclusion == 'success'
        run: testspace "[${{ matrix.unity }}]TestResults-*.xml" "[Code Coverage]CodeCoverage/**/TestCoverageResults_*.xml"