#!/usr/bin/env bash

source tools/ci/activate-venv
echo "Test suite is running under $(python --version)."

set -e
set -x

./tools/lint --groups=backend --skip=gitlint,mypy  # gitlint disabled because flaky
./tools/test-tools
# We need to pass a parallel level to test-backend because CircleCI's
# docker setup means the auto-detection logic sees the ~36 processes
# the Docker host has, not the ~2 processes of resources we're
# allocated.
./tools/test-backend --coverage --include-webhooks --parallel=6

# We run mypy after the backend tests so we get output from the
# backend tests, which tend to uncover more serious problems, first.
./tools/run-mypy --version
./tools/run-mypy

./tools/test-migrations
./tools/setup/optimize-svg
./tools/setup/generate_integration_bots_avatars.py --check-missing
# In CI, we only test links we control in test-documentation to avoid flakes
./tools/test-documentation --skip-external-links
./tools/test-help-documentation --skip-external-links
./tools/test-api
# Currently our compiled requirements files will differ for different python versions
# so we will run test-locked-requirements only for Bionic.
# ./tools/test-locked-requirements
# ./tools/test-run-dev  # https://github.com/zulip/zulip/pull/14233

# This test has been persistently flaky at like 1% frequency, is slow,
# and is for a very specific single feature, so we don't run it by default:
# ./tools/test-queue-worker-reload

# NB: Everything here should be in `tools/test-all`.  If there's a
# reason not to run it there, it should be there as a comment
# explaining why.

# This final check looks for untracked files that may have been
# created by test-backend or provision.
untracked="$(git ls-files --exclude-standard --others)"
if [ -n "$untracked" ]; then
    printf >&2 "Error: untracked files:\n%s\n" "$untracked"
    exit 1
fi
