name: Server Tests
on:
  push:
    branches:
      - main
  pull_request: {}
jobs:
  tests:
    runs-on: ubuntu-latest
    services:
      redis-cache:
        image: redis:alpine
        ports:
          - 13000:6379
      redis-queue:
        image: redis:alpine
        ports:
          - 11000:6379
      redis-socketio:
        image: redis:alpine
        ports:
          - 12000:6379
      mariadb:
        image: anandology/mariadb-utf8mb4:10.3
        ports:
          - 3306:3306
        env:
          MYSQL_ROOT_PASSWORD: root
        options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
    steps:
    - uses: actions/checkout@v2
    - name: setup python
      uses: actions/setup-python@v2
      with:
        python-version: '3.10'
    - name: setup node
      uses: actions/setup-node@v2
      with:
        node-version: '18'
        check-latest: true
    - name: setup cache for bench
      uses: actions/cache@v4
      with:
        path: ~/bench-cache
        key: ${{ runner.os }}
    - name: install bench
      run: |
        pip3 install frappe-bench
        which bench
    - name: bench init
      run: |
        if [ -d ~/bench-cache/bench.tgz ]
        then
           (cd && tar xzf ~/bench-cache/bench.tgz)
        else
          bench init ~/frappe-bench --skip-redis-config-generation --skip-assets --python "$(which python)"
          mkdir -p ~/bench-cache
          (cd && tar czf ~/bench-cache/bench.tgz frappe-bench)
        fi
    - name: add lms app to bench
      working-directory: /home/runner/frappe-bench
      run: bench get-app lms $GITHUB_WORKSPACE
    - name: create bench site
      working-directory: /home/runner/frappe-bench
      run: bench new-site --mariadb-root-password root --admin-password admin frappe.local
    - name: install lms app
      working-directory: /home/runner/frappe-bench
      run: bench --site frappe.local install-app lms
    - name: setup requirements
      working-directory: /home/runner/frappe-bench
      run: bench setup requirements --dev
    - name: allow tests
      working-directory: /home/runner/frappe-bench
      run: bench --site frappe.local set-config allow_tests true
    - name: bench build
      working-directory: /home/runner/frappe-bench
      run: bench --site frappe.local build
    - name: run tests
      working-directory: /home/runner/frappe-bench
      run: bench --site frappe.local run-tests --app lms