name: Test

on:
  push:
    branches:
      - main
  pull_request: {}

concurrency:
  group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.run_number || github.ref }}
  cancel-in-progress: true

jobs:
  tests:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version: [3.9]
      
    env:
      PANTS_CONFIG_FILES: pants.ci.toml
    
    steps:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: 0

    - name: Setup cache
      uses: actions/cache@v2
      id: cache
      with:
        path: |
          ~/.cache/pants/setup
          ~/.cache/pants/lmdb_store
          ~/.cache/pants/named_caches
        key: ${{ runner.os }}-${{ matrix.python-version }}-

    - name: Setup Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}

    - name: Bootstrap Pants
      run: |
        ./pants --version

    - name: Check BUILD files
      run: ./pants tailor --check update-build-files --check
    
    - name: Check and lint
      run: | 
        ./pants --changed-since=origin/main check lint

    - name: Test
      run: |
        ./pants --changed-since=origin/main --changed-dependees=transitive test

    - name: Upload pants log
      uses: actions/upload-artifact@v2
      with:
        name: pants-log
        path: .pants.d/pants.log
      if: always()
