name: Automated tests
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  merge_group:

env:
  RUSTFLAGS: "-C debuginfo=0"
jobs:
  test_linux:
    name: Execute automated tests on Ubuntu Linux
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
      - run: cargo build --all-features --verbose
      - run: cargo test --all-features --verbose
        env:
          RUST_LOG: debug
      - run: misc/download-mdbook.sh 0.3.5
        env:
          OS_NAME: linux
      - name: Generate the documentation
        run: mdbook build docs/
  test_windows:
    name: Execute automated tests on Windows
    runs-on: windows-2019
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
      - run: cargo build --all-features --verbose
      - run: cargo test --all-features --verbose
        env:
          RUST_LOG: debug
  test_mac:
    name: Execute automated tests on OSX
    runs-on: macos-12
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
      - run: cargo build --all-features --verbose
      - run: cargo test --all-features --verbose
        env:
          RUST_LOG: debug
  long_running_tests:
    name: Execute search tests
    runs-on: ubuntu-20.04
    env:
      RUST_LOG: debug
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
      - uses: actions/cache@v4
        id: corpus-cache
        with:
          path: |
            relannis
            data
            data_ondisk
          key: ${{ runner.os }}-search-compatible-${{ hashFiles('graphannis/tests/searchtest.rs','graphannis/tests/searchtest_queries.csv', 'graphannis/build.rs') }}
      - name: Download test corpora
        if: steps.corpus-cache.outputs.cache-hit != 'true'
        run: test -d relannis/GUM/ -a -d relannis/pcc2.1/ -a -d relannis/subtok.demo || "./misc/download-test-corpora.sh"
      - name: Build CLI binary
        if: steps.corpus-cache.outputs.cache-hit != 'true'
        run: cargo build --release --bin annis
      - name: Import GUM corpus (memory)
        if: steps.corpus-cache.outputs.cache-hit != 'true'
        run: ./target/release/annis data --cmd 'import relannis/GUM'
      - name: Import pcc2.1 corpus (memory)
        if: steps.corpus-cache.outputs.cache-hit != 'true'
        run: ./target/release/annis data --cmd 'import relannis/pcc2.1'
      - name: Import subtok.demo corpus (memory)
        if: steps.corpus-cache.outputs.cache-hit != 'true'
        run: ./target/release/annis data --cmd 'import relannis/subtok.demo'
      - name: Run integration tests (memory)
        run: cargo test --tests --release -- --ignored
        working-directory: graphannis
      - name: "Create data_ondisk directory"
        run: "mkdir -p data_ondisk"
      - name: Import GUM corpus (ondisk)
        if: steps.corpus-cache.outputs.cache-hit != 'true'
        run: ./target/release/annis data_ondisk --cmd 'set-disk-based on' --cmd 'import relannis/GUM'
      - name: Import pcc2.1 corpus (ondisk)
        if: steps.corpus-cache.outputs.cache-hit != 'true'
        run: ./target/release/annis data_ondisk --cmd 'set-disk-based on' --cmd 'import relannis/pcc2.1'
      - name: Import subtok.demo corpus (ondisk)
        if: steps.corpus-cache.outputs.cache-hit != 'true'
        run: ./target/release/annis data_ondisk --cmd 'set-disk-based on' --cmd 'import relannis/subtok.demo'
      - name: Run integration tests (ondisk)
        run: cargo test --tests --release -- --ignored
        working-directory: graphannis
        env:
          ANNIS4_TEST_DATA: "../data_ondisk"
