|
| 1 | +# Copyright 2025 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 | + |
| 15 | +import os |
| 16 | +import warnings |
| 17 | + |
| 18 | +from google.adk.features._feature_decorator import experimental |
| 19 | +from google.adk.features._feature_decorator import stable |
| 20 | +from google.adk.features._feature_decorator import working_in_progress |
| 21 | +from google.adk.features._feature_registry import _FEATURE_REGISTRY |
| 22 | +from google.adk.features._feature_registry import _get_feature_config |
| 23 | +from google.adk.features._feature_registry import _register_feature |
| 24 | +from google.adk.features._feature_registry import _WARNED_FEATURES |
| 25 | +from google.adk.features._feature_registry import FeatureConfig |
| 26 | +from google.adk.features._feature_registry import FeatureStage |
| 27 | +import pytest |
| 28 | + |
| 29 | + |
| 30 | +@working_in_progress("WIP_CLASS") |
| 31 | +class IncompleteFeature: |
| 32 | + |
| 33 | + def run(self): |
| 34 | + return "running" |
| 35 | + |
| 36 | + |
| 37 | +@working_in_progress("WIP_FUNCTION") |
| 38 | +def wip_function(): |
| 39 | + return "executing" |
| 40 | + |
| 41 | + |
| 42 | +@experimental("EXPERIMENTAL_CLASS") |
| 43 | +class ExperimentalClass: |
| 44 | + |
| 45 | + def run(self): |
| 46 | + return "running" |
| 47 | + |
| 48 | + |
| 49 | +@experimental("EXPERIMENTAL_FUNCTION") |
| 50 | +def experimental_function(): |
| 51 | + return "executing" |
| 52 | + |
| 53 | + |
| 54 | +@stable("STABLE_CLASS") |
| 55 | +class StableClass: |
| 56 | + |
| 57 | + def run(self): |
| 58 | + return "running" |
| 59 | + |
| 60 | + |
| 61 | +@stable("STABLE_FUNCTION") |
| 62 | +def stable_function(): |
| 63 | + return "executing" |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture(autouse=True) |
| 67 | +def reset_env_and_registry(monkeypatch): |
| 68 | + """Reset environment variables and registry before each test.""" |
| 69 | + # Clean up environment variables |
| 70 | + for key in list(os.environ.keys()): |
| 71 | + if key.startswith("ADK_ENABLE_") or key.startswith("ADK_DISABLE_"): |
| 72 | + monkeypatch.delenv(key, raising=False) |
| 73 | + |
| 74 | + # Add an existing feature to the registry |
| 75 | + _register_feature( |
| 76 | + "ENABLED_EXPERIMENTAL_FEATURE", |
| 77 | + FeatureConfig(FeatureStage.EXPERIMENTAL, default_on=True), |
| 78 | + ) |
| 79 | + |
| 80 | + _register_feature( |
| 81 | + "EXPERIMENTAL_FUNCTION", |
| 82 | + FeatureConfig(FeatureStage.EXPERIMENTAL, default_on=True), |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def test_working_in_progress_stage_mismatch(): |
| 87 | + """Test that working_in_progress is used with a non-WIP stage.""" |
| 88 | + try: |
| 89 | + |
| 90 | + @working_in_progress("ENABLED_EXPERIMENTAL_FEATURE") |
| 91 | + def unused_function(): # pylint: disable=unused-variable |
| 92 | + return "unused" |
| 93 | + |
| 94 | + assert False, "Expected ValueError to be raised." |
| 95 | + except ValueError as e: |
| 96 | + assert ( |
| 97 | + "Feature 'ENABLED_EXPERIMENTAL_FEATURE' is being defined with stage" |
| 98 | + " 'FeatureStage.WIP', but it was previously registered with stage" |
| 99 | + " 'FeatureStage.EXPERIMENTAL'." |
| 100 | + in str(e) |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def test_working_in_progress_class_raises_error(): |
| 105 | + """Test that WIP class raises RuntimeError by default.""" |
| 106 | + |
| 107 | + try: |
| 108 | + IncompleteFeature() |
| 109 | + assert False, "Expected RuntimeError to be raised." |
| 110 | + except RuntimeError as e: |
| 111 | + assert "Feature WIP_CLASS is not enabled." in str(e) |
| 112 | + |
| 113 | + |
| 114 | +def test_working_in_progress_class_bypass_with_env_var(monkeypatch): |
| 115 | + """Test that WIP class can be bypassed with env var.""" |
| 116 | + |
| 117 | + monkeypatch.setenv("ADK_ENABLE_WIP_CLASS", "true") |
| 118 | + |
| 119 | + with warnings.catch_warnings(record=True) as w: |
| 120 | + feature = IncompleteFeature() |
| 121 | + feature.run() |
| 122 | + assert len(w) == 1 |
| 123 | + assert "[WIP] feature WIP_CLASS is enabled." in str(w[0].message) |
| 124 | + |
| 125 | + |
| 126 | +def test_working_in_progress_function_raises_error(): |
| 127 | + """Test that WIP function raises RuntimeError by default.""" |
| 128 | + |
| 129 | + try: |
| 130 | + wip_function() |
| 131 | + assert False, "Expected RuntimeError to be raised." |
| 132 | + except RuntimeError as e: |
| 133 | + assert "Feature WIP_FUNCTION is not enabled." in str(e) |
| 134 | + |
| 135 | + |
| 136 | +def test_working_in_progress_function_bypass_with_env_var(monkeypatch): |
| 137 | + """Test that WIP function can be bypassed with env var.""" |
| 138 | + |
| 139 | + monkeypatch.setenv("ADK_ENABLE_WIP_FUNCTION", "true") |
| 140 | + |
| 141 | + with warnings.catch_warnings(record=True) as w: |
| 142 | + wip_function() |
| 143 | + assert len(w) == 1 |
| 144 | + assert "[WIP] feature WIP_FUNCTION is enabled." in str(w[0].message) |
| 145 | + |
| 146 | + |
| 147 | +def test_disabled_experimental_class_raises_error(): |
| 148 | + """Test that disabled experimental class raises RuntimeError by default.""" |
| 149 | + |
| 150 | + try: |
| 151 | + ExperimentalClass() |
| 152 | + assert False, "Expected RuntimeError to be raised." |
| 153 | + except RuntimeError as e: |
| 154 | + assert "Feature EXPERIMENTAL_CLASS is not enabled." in str(e) |
| 155 | + |
| 156 | + |
| 157 | +def test_disabled_experimental_class_bypass_with_env_var(monkeypatch): |
| 158 | + """Test that disabled experimental class can be bypassed with env var.""" |
| 159 | + |
| 160 | + monkeypatch.setenv("ADK_ENABLE_EXPERIMENTAL_CLASS", "true") |
| 161 | + |
| 162 | + with warnings.catch_warnings(record=True) as w: |
| 163 | + feature = ExperimentalClass() |
| 164 | + feature.run() |
| 165 | + assert len(w) == 1 |
| 166 | + assert "[EXPERIMENTAL] feature EXPERIMENTAL_CLASS is enabled." in str( |
| 167 | + w[0].message |
| 168 | + ) |
| 169 | + |
| 170 | + |
| 171 | +def test_enabled_experimental_function_does_not_raise_error(): |
| 172 | + """Test that enabled experimental function does not raise error.""" |
| 173 | + |
| 174 | + with warnings.catch_warnings(record=True) as w: |
| 175 | + experimental_function() |
| 176 | + assert len(w) == 1 |
| 177 | + assert "[EXPERIMENTAL] feature EXPERIMENTAL_FUNCTION is enabled." in str( |
| 178 | + w[0].message |
| 179 | + ) |
| 180 | + |
| 181 | + |
| 182 | +def test_enabled_experimental_function_disabled_by_env_var(monkeypatch): |
| 183 | + """Test that enabled experimental function can be disabled by env var.""" |
| 184 | + |
| 185 | + monkeypatch.setenv("ADK_DISABLE_EXPERIMENTAL_FUNCTION", "true") |
| 186 | + |
| 187 | + try: |
| 188 | + experimental_function() |
| 189 | + assert False, "Expected RuntimeError to be raised." |
| 190 | + except RuntimeError as e: |
| 191 | + assert "Feature EXPERIMENTAL_FUNCTION is not enabled." in str(e) |
| 192 | + |
| 193 | + |
| 194 | +def test_stable_class_does_not_raise_error_or_warn(): |
| 195 | + """Test that stable class does not raise error or warn.""" |
| 196 | + |
| 197 | + with warnings.catch_warnings(record=True) as w: |
| 198 | + StableClass().run() |
| 199 | + assert not w |
| 200 | + |
| 201 | + |
| 202 | +def test_stable_function_does_not_raise_error_or_warn(): |
| 203 | + """Test that stable function does not raise error or warn.""" |
| 204 | + |
| 205 | + with warnings.catch_warnings(record=True) as w: |
| 206 | + stable_function() |
| 207 | + assert not w |
0 commit comments