name: run units target on GNU/Linux

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  testing:

    strategy:
      matrix:
        # os: [ubuntu-18.04, ubuntu-20.04]
        os: [ubuntu-18.04, ubuntu-20.04]
        compiler: [gcc, clang]

    runs-on: ${{ matrix.os }}

    env:
      CC: ${{ matrix.compiler }}
      BUILDDIR: ${{ matrix.os }}-${{ matrix.compiler }}

      # Our sandbox doesn't work with gcov.
      # Following cases target ctags without gcov.
      SANDBOX_CASES: sandbox,sandbox-crash,sandbox-default-req,sandbox-unknown-submode
      DEBIAN_FRONTEND: noninteractive

    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0
    - name: update package information
      run: sudo apt-get -y -o APT::Immediate-Configure=false update
    - name: install tools and libraries
      run: sudo apt-get -y -o APT::Immediate-Configure=false install valgrind pkg-config automake bash libjansson-dev libyaml-dev libseccomp-dev libxml2-dev gdb python3-docutils libpcre2-dev
    - name: install rubygems and lcov
      if: matrix.compiler == 'gcc' && matrix.os == 'ubuntu-20.04'
      #run: sudo apt-get -y -o APT::Immediate-Configure=false install rubygems lcov
      run: sudo apt-get -y -o APT::Immediate-Configure=false install lcov
    # - name: install lcoveralls
    #   if: matrix.compiler == 'gcc' && matrix.os == 'ubuntu-20.04'
    #   run: sudo gem install lcoveralls
    - name: autogen.sh
      run: ./autogen.sh
    - name: report the version of cc
      run: ${{ matrix.compiler }} --version
    - name: report the version of make
      run: make --version
    - name: configure
      run: |
        mkdir -p ${{ matrix.os }}-"$CC"
        (
          cd $BUILDDIR
          if [ ${{ matrix.compiler }} = 'gcc' -a ${{ matrix.os }} = 'ubuntu-20.04' ]; then
              extra_args=--enable-coverage-gcov
          fi
          ../configure --enable-debugging --enable-iconv ${extra_args}
        )
    - name: make
      run: make -C $BUILDDIR -j2
    - name: report features
      run: |
        (
          cd $BUILDDIR
          ./ctags --list-features
        )
    - name: make check
      run: make -C $BUILDDIR check
    - name: make roundtrip
      run: make -C $BUILDDIR roundtrip
    - name: make dist
      run: make -C $BUILDDIR dist
    - name: make for testing ctags in sandbox
      run: |
        (
          cd $BUILDDIR
          tar zxf universal-ctags*tar.gz
          (
            cd universal-ctags*[0-9]
            mkdir -p ${BUILDDIR}
            (
                cd ${BUILDDIR}
                ../configure --enable-debugging --enable-iconv
            )
            make -C ${BUILDDIR} -j2
          )
        )
    - name: make tmain in sandbox
      run: |
        (
          cd $BUILDDIR/universal-ctags*[0-9]
          make -C ${BUILDDIR} tmain UNITS=${SANDBOX_CASES}
          make -C ${BUILDDIR}  clean
        )
    - name: send coverage info
      if: matrix.compiler == 'gcc' && matrix.os == 'ubuntu-20.04'
      run: |
        # for f in /var/lib/gems/*/gems/lcoveralls-*/lib/lcoveralls/color_formatter.rb; do
        #     sudo sed -i -e 's/severity.capitalize!/severity = severity.capitalize/' $f
        # done
        # for f in /var/lib/gems/*/gems/lcoveralls-*/lib/lcoveralls/runner.rb; do
        #     sudo sed -i -e 's/\(.*format.*f\)\(%\)\('"'"'.*$\)/\1%%\3/' $f
        # done

        (cd ${BUILDDIR}; lcov -c -b . -d . -o coverage.info)

        # Upload to coveralls.
        # lcoveralls --root . --retry-count 5 ${BUILDDIR}/coverage.info

        # Upload to codecov.
        bash <(curl -s https://codecov.io/bash) -f ${BUILDDIR}/coverage.info -Z -X gcov -X coveragepy -X search -X xcode -X gcovout -n travis-${{ matrix.os }}
