#!/usr/bin/env bash

# Ensure Poetry can be found in the PATH.
# This is because Precommit won't use the same
# PATH as in the system shell.
export PATH="$HOME/.local/bin:$PATH"

set -e

# Check if script is run with --precommit argument.
# If so, skip the tests to speed up pre-commit checks and prevent
# them failing because of missing environment variables.
SKIP_TESTS=false
for arg in "$@"; do
  if [ "$arg" = "--precommit" ]; then
    SKIP_TESTS=true
    break
  fi
done

# ALWAYS run linting of python files
echo 'Running mypy...'
poetry run mypy --cache-dir=/dev/null monday_com_orca_backend tests

echo "Running ruff linter..."
poetry run ruff check monday_com_orca_backend tests --no-cache

echo "Running ruff formatter check..."
poetry run ruff format monday_com_orca_backend tests --diff --no-cache

# Only run tests if not skipping for precommit
if [ "$SKIP_TESTS" = false ]; then
  echo 'Running tests...'
  PYTHONDONTWRITEBYTECODE=1 COVERAGE_FILE=build/.coverage poetry run pytest tests/unit/ \
    -p no:cacheprovider \
    --cov monday_com_orca_backend \
    --cov-report xml:build/coverage.xml \
    --cov-report term \
    --junitxml=build/pyunit.xml
fi
