name: test

on:
  pull_request:
  push:
    branches:
      - master

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        image:
          - ubuntu:rolling
          - fedora:latest
        soup:
          - libsoup3
        include:
          - image: ubuntu:22.04
            soup: libsoup2
          - image: ubuntu:20.04
            soup: libsoup2
          - image: ubuntu:18.04
            soup: libsoup2

    runs-on: ubuntu-latest
    container: ${{ matrix.image }}

    steps:
      - uses: actions/checkout@v2

      - name: Initialise parameters
        id: params
        run: |
          artifact_name="logs-$(echo "${{ matrix.image }}" | sed "s/:/-/")"
          echo "::set-output name=artifact::$artifact_name"

      - name: Install dependencies (Ubuntu)
        if: startsWith(matrix.image, 'ubuntu:')
        run: |
          apt-get update
          DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends gtk-doc-tools libgirepository1.0-dev libglib2.0-dev libjson-glib-dev ninja-build python3-pip python3-setuptools python3-wheel qtbase5-dev qtdeclarative5-dev valac

      - name: Install libsoup3 dependencies (Ubuntu)
        if: startsWith(matrix.image, 'ubuntu:') && matrix.soup == 'libsoup3'
        run: |
          DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends libsoup-3.0-dev

      - name: Install libsoup2.4 dependencies (Ubuntu)
        if: startsWith(matrix.image, 'ubuntu:') && matrix.soup == 'libsoup2'
        run: |
          DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends libsoup2.4-dev

      - name: Install dependencies (Fedora)
        if: startsWith(matrix.image, 'fedora:')
        run: |
          dnf install -y gcc gcc-c++ gobject-introspection-devel glib2-devel gtk-doc json-glib-devel libsoup3-devel ninja-build python3-pip qt5-qtbase-devel qt5-qtdeclarative-devel redhat-rpm-config vala

      - name: Install meson
        run: pip3 install meson

      - name: Configure
        if: matrix.soup == 'libsoup3'
        run: |
          meson _build

      - name: Configure (libsoup2.4)
        if: matrix.soup == 'libsoup2'
        run: |
          meson _build -Dsoup2=true

      - name: Build
        run: |
          ninja -C _build
          ninja -C _build snapd-glib-doc

      - name: Test
        run: ninja -C _build test

      - name: Collect logs on failure
        if: failure()
        uses: actions/upload-artifact@v2
        with:
          name: ${{ steps.params.outputs.artifact }}
          path: _build/meson-logs
