name: Build Tests

on: [push, pull_request]

jobs:
  build-test:
    name: Build Test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os:
          - 'fedora:latest'
          - 'debian:testing'
          - 'ubuntu:rolling'
          # Disable CentOS due to missing dependencies
          # - 'centos:7'
          # - 'centos:8'
    container: ${{ matrix.os }}
    steps:
    - name: Clone repository
      uses: actions/checkout@v2

    - name: Install Fedora/CentOS dependencies
      if: ${{ startsWith(matrix.os, 'fedora:') || startsWith(matrix.os, 'centos:') }}
      run: |
        dnf install -y dnf-plugins-core rpm-build
        dnf builddep -y --spec jss.spec

    - name: Install Debian/Ubuntu dependencies
      if: ${{ startsWith(matrix.os, 'debian:') || startsWith(matrix.os, 'ubuntu:') }}
      run: |
        apt-get update
        apt-get install -y \
            cmake zip unzip \
            g++ libnss3-dev libnss3-tools \
            openjdk-11-jdk libcommons-lang3-java libslf4j-java junit4

    - name: Build JSS binaries, Javadoc, and run tests
      run: ./build.sh

  # Compare JNI symbols in the code and in the version script.
  # If there are JNI symbols in the code but not in the version script -> fail.
  symbol-test:
    name: Symbol Test
    runs-on: ubuntu-latest
    steps:
      - name: Clone repository
        uses: actions/checkout@v2

      - name: Get JNI symbols in the code
        run: |
          grep -iroh '^Java_org_mozilla[^(;]*' src/main/java/ | sort -u > /tmp/functions.txt
          cat /tmp/functions.txt

      - name: Get JNI symbols in the version script
        run: |
          grep -iroh '^Java_org_mozilla[^(;]*' lib/ | sort -u > /tmp/version.txt
          cat /tmp/version.txt

      - name: Compare JNI symbols
        run: |
          diff /tmp/functions.txt /tmp/version.txt || true
          comm -23 --check-order /tmp/functions.txt /tmp/version.txt > /tmp/diff.txt
          test ! -s /tmp/diff.txt
