name: Testing
on:
  push:
    branches:
      - master
      - next
  pull_request:

jobs:
  tests:
    runs-on: ubuntu-latest
    container: mcr.microsoft.com/dotnet/sdk:6.0
    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0

    - name: Build
      run: |
        cd back
        dotnet build '-p:SkipTranscoder=true' -p:CopyLocalLockFileAssemblies=true
        cp ./out/bin/Kyoo.Abstractions/Debug/net6.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll ./tests/Kyoo.Tests/bin/Debug/net6.0/

    - name: Test
      run: |
        cd back
        dotnet test --no-build '-p:CollectCoverage=true;CoverletOutputFormat=opencover' --logger "trx;LogFileName=TestOutputResults.xml"
      env:
        POSTGRES_HOST: postgres
        POSTGRES_USERNAME: postgres
        POSTGRES_PASSWORD: postgres

    - name: Sanitize coverage output
      if: ${{ always() }}
      run: sed -i "s'$(pwd)/back'.'" back/tests/Kyoo.Tests/coverage.opencover.xml

    - name: Upload tests results
      if: ${{ always() }}
      uses: actions/upload-artifact@v2
      with:
        name: results.xml
        path: "**/TestOutputResults.xml"

    - name: Upload coverage report
      if: ${{ always() }}
      uses: actions/upload-artifact@v2
      with:
        name: coverage.xml
        path: "**/coverage.opencover.xml"
