|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for a2a_experimental decorator and warning suppression.""" |
| 15 | + |
| 16 | +import warnings |
| 17 | + |
| 18 | +from google.adk.a2a.experimental import a2a_experimental |
| 19 | +import pytest |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(autouse=True) |
| 23 | +def clear_suppression_env_vars(monkeypatch): |
| 24 | + monkeypatch.delenv( |
| 25 | + "ADK_SUPPRESS_EXPERIMENTAL_FEATURE_WARNINGS", raising=False |
| 26 | + ) |
| 27 | + monkeypatch.delenv( |
| 28 | + "ADK_SUPPRESS_A2A_EXPERIMENTAL_FEATURE_WARNINGS", raising=False |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def test_a2a_experimental_class_warns_by_default(): |
| 33 | + """Classes decorated with @a2a_experimental warn when instantiated.""" |
| 34 | + |
| 35 | + @a2a_experimental |
| 36 | + class A2aExperimentalClass: |
| 37 | + |
| 38 | + def run(self): |
| 39 | + return "running" |
| 40 | + |
| 41 | + with warnings.catch_warnings(record=True) as w: |
| 42 | + warnings.simplefilter("always") |
| 43 | + feature = A2aExperimentalClass() |
| 44 | + assert feature.run() == "running" |
| 45 | + assert len(w) == 1 |
| 46 | + assert "[EXPERIMENTAL] A2aExperimentalClass:" in str(w[0].message) |
| 47 | + |
| 48 | + |
| 49 | +def test_a2a_experimental_function_warns_by_default(): |
| 50 | + """Functions decorated with @a2a_experimental warn when called.""" |
| 51 | + |
| 52 | + @a2a_experimental |
| 53 | + def a2a_experimental_function(): |
| 54 | + return "running fn" |
| 55 | + |
| 56 | + with warnings.catch_warnings(record=True) as w: |
| 57 | + warnings.simplefilter("always") |
| 58 | + assert a2a_experimental_function() == "running fn" |
| 59 | + assert len(w) == 1 |
| 60 | + assert "[EXPERIMENTAL] a2a_experimental_function:" in str(w[0].message) |
| 61 | + |
| 62 | + |
| 63 | +def test_a2a_experimental_warning_suppressed_by_general_env_var(monkeypatch): |
| 64 | + """General suppression env var silences @a2a_experimental warnings.""" |
| 65 | + |
| 66 | + @a2a_experimental |
| 67 | + class GeneralSuppressedA2aClass: |
| 68 | + |
| 69 | + def run(self): |
| 70 | + return "running" |
| 71 | + |
| 72 | + @a2a_experimental |
| 73 | + def general_suppressed_a2a_function(): |
| 74 | + return "running fn" |
| 75 | + |
| 76 | + monkeypatch.setenv("ADK_SUPPRESS_EXPERIMENTAL_FEATURE_WARNINGS", "yes") |
| 77 | + |
| 78 | + with warnings.catch_warnings(record=True) as w: |
| 79 | + warnings.simplefilter("always") |
| 80 | + feature = GeneralSuppressedA2aClass() |
| 81 | + assert feature.run() == "running" |
| 82 | + assert general_suppressed_a2a_function() == "running fn" |
| 83 | + assert not w |
| 84 | + |
| 85 | + |
| 86 | +def test_a2a_experimental_warning_suppressed_by_a2a_env_var(monkeypatch): |
| 87 | + """A2A-specific suppression env var silences @a2a_experimental warnings.""" |
| 88 | + |
| 89 | + @a2a_experimental |
| 90 | + class A2aSuppressedClass: |
| 91 | + |
| 92 | + def run(self): |
| 93 | + return "running" |
| 94 | + |
| 95 | + @a2a_experimental |
| 96 | + def a2a_suppressed_function(): |
| 97 | + return "running fn" |
| 98 | + |
| 99 | + monkeypatch.setenv("ADK_SUPPRESS_A2A_EXPERIMENTAL_FEATURE_WARNINGS", "on") |
| 100 | + |
| 101 | + with warnings.catch_warnings(record=True) as w: |
| 102 | + warnings.simplefilter("always") |
| 103 | + feature = A2aSuppressedClass() |
| 104 | + assert feature.run() == "running" |
| 105 | + assert a2a_suppressed_function() == "running fn" |
| 106 | + assert not w |
0 commit comments