# Copyright      2021  Fangjun Kuang (csukuangfj@gmail.com)

# See ../../LICENSE for clarification regarding multiple authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: test

on:
  push:
    branches:
      - master
  pull_request:
    types: [labeled]

jobs:
  test:
    if: github.event.label.name == 'ready' || github.event_name == 'push'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        # os: [ubuntu-18.04, macos-10.15]
        # disable macOS test for now.
        os: [ubuntu-18.04]
        python-version: [3.7, 3.8]
        torch: ["1.8.0", "1.10.0"]
        torchaudio: ["0.8.0", "0.10.0"]
        k2-version: ["1.9.dev20211101"]
        exclude:
          - torch: "1.8.0"
            torchaudio: "0.10.0"
          - torch: "1.10.0"
            torchaudio: "0.8.0"

      fail-fast: false

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

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

      - name: Install libnsdfile and libsox
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          sudo apt update
          sudo apt install -q -y libsndfile1-dev libsndfile1 ffmpeg
          sudo apt install -q -y --fix-missing sox libsox-dev libsox-fmt-all

      - name: Install Python dependencies
        run: |
          python3 -m pip install --upgrade pip pytest
          # numpy 1.20.x does not support python 3.6
          pip install numpy==1.19
          pip install torch==${{ matrix.torch }}+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html
          if [[ ${{ matrix.torchaudio }} == "0.10.0" ]]; then
            pip install torchaudio==${{ matrix.torchaudio }}+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html
          else
            pip install torchaudio==${{ matrix.torchaudio }}
          fi

          pip install k2==${{ matrix.k2-version }}+cpu.torch${{ matrix.torch }} -f https://k2-fsa.org/nightly/
          pip install git+https://github.com/lhotse-speech/lhotse
          # icefall requirements
          pip install -r requirements.txt

      - name: Install graphviz
        if: startsWith(matrix.os, 'ubuntu')
        shell: bash
        run: |
          python3 -m pip install -qq graphviz
          sudo apt-get -qq install graphviz

      - name: Install graphviz
        if: startsWith(matrix.os, 'macos')
        shell: bash
        run: |
          python3 -m pip install -qq graphviz
          brew install -q graphviz

      - name: Run tests
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          ls -lh
          export PYTHONPATH=$PWD:$PWD/lhotse:$PYTHONPATH
          echo $PYTHONPATH
          pytest -v -s ./test
          # runt tests for conformer ctc
          cd egs/librispeech/ASR/conformer_ctc
          pytest -v -s

          if [[ ${{ matrix.torchaudio }} == "0.10.0" ]]; then
            cd ../transducer
            pytest -v -s

            cd ../transducer_stateless
            pytest -v -s

            cd ../transducer_lstm
            pytest -v -s
          fi

      - name: Run tests
        if: startsWith(matrix.os, 'macos')
        run: |
          ls -lh
          export PYTHONPATH=$PWD:$PWD/lhotse:$PYTHONPATH
          lib_path=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
          echo "lib_path: $lib_path"
          export DYLD_LIBRARY_PATH=$lib_path:$DYLD_LIBRARY_PATH
          pytest -v -s ./test

          # run tests for conformer ctc
          cd egs/librispeech/ASR/conformer_ctc
          pytest -v -s

          if [[ ${{ matrix.torchaudio }} == "0.10.0" ]]; then
            cd ../transducer
            pytest -v -s

            cd ../transducer_stateless
            pytest -v -s

            cd ../transducer_lstm
            pytest -v -s
          fi
