#!/bin/bash

set -ex

PACKAGE_LIST_FILE=packages_by_dep_dag.txt
# uv's default index strategy (first-match) won't check other indexes once a package is found,
# unlike pip which merges all indexes. Use unsafe-best-match to get pip-like behavior and
# allow wheels.galaxyproject.org wheels to be preferred over PyPI source distributions.
: "${PIP_EXTRA_ARGS:=--index-strategy unsafe-best-match --extra-index-url https://wheels.galaxyproject.org/simple}"
FOR_PULSAR=0
SKIP_PACKAGES=(
    web_client
    meta
)

should_skip_package() {
    local pkg
    for pkg in "${SKIP_PACKAGES[@]}"; do
        [ "$1" = "$pkg" ] && return 0
    done
    return 1
}

for arg in "$@"; do
    if [ "$arg" = "--for-pulsar" ]; then
        PACKAGE_LIST_FILE=packages_for_pulsar_by_dep_dag.txt
        FOR_PULSAR=1
    fi
done

# Don't display the pip progress bar when running under CI
if [ "$CI" = 'true' ]; then
    export PIP_PROGRESS_BAR=off
fi

# Change to packages directory.
cd "$(dirname "$0")"

# Use a throw-away virtualenv
TEST_PYTHON=${TEST_PYTHON:-"python3"}
TEST_ENV_DIR=${TEST_ENV_DIR:-$(mktemp -d -t gxpkgtestenvXXXXXX)}

if command -v uv >/dev/null; then
    uv venv --python "$TEST_PYTHON" "$TEST_ENV_DIR"
    PIP_CMD="$(command -v uv) pip"
else
    "$TEST_PYTHON" -m venv "$TEST_ENV_DIR"
    PIP_CMD='python -m pip'
fi

# shellcheck disable=SC1091
. "${TEST_ENV_DIR}/bin/activate"
if [ "${PIP_CMD}" = 'python -m pip' ]; then
    ${PIP_CMD} install --upgrade pip setuptools wheel
fi
if [ $FOR_PULSAR -eq 0 ]; then
    # shellcheck disable=SC2086 - word splitting is intentional for PIP_EXTRA_ARGS
    ${PIP_CMD} install ${PIP_EXTRA_ARGS} -r ../lib/galaxy/dependencies/pinned-typecheck-requirements.txt
fi

# Ensure ordered by dependency DAG
while read -r package_dir || [ -n "$package_dir" ]; do  # https://stackoverflow.com/questions/12916352/shell-script-read-missing-last-line
    # Ignore empty lines
    if [ -z "$package_dir" ]; then
        continue
    fi
    # Ignore lines beginning with `#`
    if  [[ $package_dir =~ ^#.* ]]; then
        continue
    fi
    if should_skip_package "$package_dir"; then
        printf "\nSkipping package %s\n\n" "$package_dir"
        continue
    fi

    printf "\n========= TESTING PACKAGE %s =========\n\n" "$package_dir"

    cd "$package_dir"

    # Install extras (if needed)
    # shellcheck disable=SC2086 - word splitting is intentional for PIP_EXTRA_ARGS
    if [ "$package_dir" = "util" ]; then
        ${PIP_CMD} install ${PIP_EXTRA_ARGS} '.[image-util,template,jstree,config-template,test]'
    elif [ "$package_dir" = "tool_util" ]; then
        ${PIP_CMD} install ${PIP_EXTRA_ARGS} '.[cwl,mulled,edam,extended-assertions,test]'
    elif grep -q 'test =' setup.cfg 2>/dev/null; then
        ${PIP_CMD} install ${PIP_EXTRA_ARGS} '.[test]'
    else
        ${PIP_CMD} install ${PIP_EXTRA_ARGS} .
    fi

    if [ $FOR_PULSAR -eq 0 ]; then
        marker_args=(-m 'not external_dependency_management')
    else
        marker_args=()
    fi
    # Ignore exit code 5 (no tests ran)
    pytest "${marker_args[@]}" . || test $? -eq 5
    if [ $FOR_PULSAR -eq 0 ]; then
        # make mypy uses uv now and so this legacy code should just run mypy
        # directly to use the venv we have already activated
        mypy .
    fi
    cd ..
done < $PACKAGE_LIST_FILE
