|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2026 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +# Runs all unit tests for adk codebase. Sets up test environment according to |
| 18 | +# CONTRIBUTING.md. |
| 19 | +# Usage: ./unittests.sh [--version <version>] |
| 20 | + |
| 21 | +set -euo pipefail |
| 22 | + |
| 23 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 24 | +cd "$SCRIPT_DIR" |
| 25 | +cd .. |
| 26 | + |
| 27 | +# Argument Parsing |
| 28 | +ALL_VERSIONS=("3.10" "3.11" "3.12" "3.13" "3.14") |
| 29 | +versions_to_run=() |
| 30 | + |
| 31 | +if [[ $# -eq 0 ]]; then |
| 32 | + versions_to_run=("${ALL_VERSIONS[@]}") |
| 33 | +elif [[ "$1" == "--version" ]]; then |
| 34 | + if [[ -z "${2:-}" ]]; then |
| 35 | + echo "Error: Missing version for --version flag." >&2 |
| 36 | + echo "Usage: $0 --version <version>" >&2 |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + # Validate version |
| 40 | + if ! [[ " ${ALL_VERSIONS[*]} " =~ " $2 " ]]; then |
| 41 | + echo "Error: Invalid version '$2'. Supported versions: ${ALL_VERSIONS[*]}" >&2 |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | + versions_to_run=("$2") |
| 45 | +else |
| 46 | + echo "Error: Unknown argument '$1'." >&2 |
| 47 | + echo "Usage: $0 [--version <version>]" >&2 |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | + |
| 52 | +# Capture original venv for restoration |
| 53 | +ORIGINAL_VENV="${VIRTUAL_ENV:-}" |
| 54 | + |
| 55 | +restore_venv() { |
| 56 | + # Deactivate the unittest_venv if it is active |
| 57 | + if command -v deactivate &> /dev/null; then |
| 58 | + deactivate |
| 59 | + fi |
| 60 | + |
| 61 | + if [[ -d ".unittest_venv" ]]; then |
| 62 | + echo "Cleaning up .unittest_venv..." |
| 63 | + rm -rf .unittest_venv |
| 64 | + fi |
| 65 | + |
| 66 | + if [[ -n "$ORIGINAL_VENV" ]]; then |
| 67 | + echo "Reactivating pre-existing venv: $ORIGINAL_VENV" |
| 68 | + source "$ORIGINAL_VENV/bin/activate" |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +# Ensure the environment is restored when the script exits. |
| 73 | +trap restore_venv EXIT |
| 74 | + |
| 75 | +# 1. deactivate the current venv |
| 76 | +if [[ -n "${VIRTUAL_ENV:-}" ]]; then |
| 77 | + echo "Deactivating current venv: $VIRTUAL_ENV" |
| 78 | + if command -v deactivate &> /dev/null; then |
| 79 | + deactivate |
| 80 | + fi |
| 81 | + |
| 82 | +fi |
| 83 | + |
| 84 | +for version in "${versions_to_run[@]}"; do |
| 85 | + echo "" |
| 86 | + echo "==================================================" |
| 87 | + echo " RUNNING TESTS FOR PYTHON $version" |
| 88 | + echo "==================================================" |
| 89 | + |
| 90 | + # 2. create a unittest_venv just for unit tests |
| 91 | + echo "Creating/Using unittest_venv for python${version} in .unittest_venv..." |
| 92 | + uv venv --python "${version}" .unittest_venv --clear |
| 93 | + source .unittest_venv/bin/activate |
| 94 | + |
| 95 | + # 3. perform the unit tests |
| 96 | + echo "Setting up test environment in .unittest_venv..." |
| 97 | + uv sync --extra test --extra eval --extra a2a --active |
| 98 | + |
| 99 | + echo "Running unit tests..." |
| 100 | + TEST_EXIT_CODE=0 |
| 101 | + pytest ./tests/unittests || TEST_EXIT_CODE=$? |
| 102 | + |
| 103 | + # 4. report the unit tests status as is |
| 104 | + if [[ $TEST_EXIT_CODE -ne 0 ]]; then |
| 105 | + echo "" |
| 106 | + echo "--------------------------------------------------" |
| 107 | + echo "Unit tests failed for Python $version with exit code $TEST_EXIT_CODE" |
| 108 | + echo "--------------------------------------------------" |
| 109 | + exit $TEST_EXIT_CODE |
| 110 | + fi |
| 111 | +done |
| 112 | + |
| 113 | + |
| 114 | +# 5. reactivate the pre-existing venv if the unit test succeeds |
| 115 | +echo "" |
| 116 | +echo "--------------------------------------------------" |
| 117 | +echo "Unit tests passed for all specified versions!" |
| 118 | +echo "--------------------------------------------------" |
0 commit comments