diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c620c8ab96..07907ef946 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -188,6 +188,24 @@ part before or alongside your code PR. pytest ./tests/unittests ``` + **Alternatively**, use the included `unittests.sh` script which handles + environment setup and restoration automatically: + + ```shell + ./unittests.sh + ``` + + This script will: + - Set up the test environment with minimal dependencies (`test`, `eval`, `a2a`) + - Run the unit tests + - Restore the full development environment (`--all-extras`) + + To also run auto-formatting after successful tests: + + ```shell + ./unittests.sh format + ``` + 6. **Auto-format the code:** **NOTE**: We use `isort` and `pyink` for styles. Use the included diff --git a/unittests.sh b/unittests.sh new file mode 100755 index 0000000000..4bccb85257 --- /dev/null +++ b/unittests.sh @@ -0,0 +1,59 @@ +#!/bin/bash +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# Runs all unit tests for adk codebase. Sets up dev environment re: Contributing.md +# Usage: ./unittests.sh [format] +# format - Optional argument to run autoformat.sh if tests pass + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +# Ensure the environment is restored if the script is interrupted. +cleanup_on_interrupt() { + echo -e "\nScript interrupted. Restoring full development environment..." + uv sync --all-extras + exit 130 # Standard exit code for Ctrl+C +} +trap cleanup_on_interrupt INT TERM + +RUN_FORMAT=false +if [[ "${1:-}" == "format" ]]; then + RUN_FORMAT=true +fi + +echo "Setting up test environment..." +uv sync --extra test --extra eval --extra a2a + +echo "Running unit tests..." +TEST_EXIT_CODE=0 +pytest ./tests/unittests || TEST_EXIT_CODE=$? + +echo "Restoring full development environment..." +uv sync --all-extras + +if [[ $TEST_EXIT_CODE -ne 0 ]]; then + echo "Unit tests failed with exit code $TEST_EXIT_CODE" + exit $TEST_EXIT_CODE +fi + +echo "Unit tests passed!" + +if [[ "$RUN_FORMAT" == "true" ]]; then + echo "Running autoformat..." + ./autoformat.sh +fi