|
| 1 | +import importlib |
1 | 2 | import os
|
2 | 3 | import tempfile
|
3 | 4 | import warnings
|
| 5 | +from unittest import mock |
4 | 6 |
|
5 |
| -from google.adk.utils.feature_decorator import experimental |
6 |
| -from google.adk.utils.feature_decorator import working_in_progress |
| 7 | +from google.adk.utils import feature_decorator |
| 8 | +from google.adk.utils.feature_decorator import experimental, working_in_progress |
7 | 9 |
|
8 | 10 |
|
9 | 11 | @working_in_progress("in complete feature, don't use yet")
|
@@ -299,3 +301,93 @@ def test_experimental_function_empty_parens_warns():
|
299 | 301 | assert "This feature is experimental and may change or be removed" in str(
|
300 | 302 | w[0].message
|
301 | 303 | )
|
| 304 | + |
| 305 | + |
| 306 | +def test_experimental_function_warning_suppressed_with_env_var(): |
| 307 | + """Test experimental function warning suppressed by env var.""" |
| 308 | + with mock.patch.dict(os.environ, {"ADK_DISABLE_EXPERIMENTAL_WARNING": "true"}): |
| 309 | + importlib.reload(feature_decorator) |
| 310 | + from google.adk.utils.feature_decorator import experimental |
| 311 | + |
| 312 | + @experimental |
| 313 | + def test_fn(): |
| 314 | + return "executing" |
| 315 | + |
| 316 | + with warnings.catch_warnings(record=True) as w: |
| 317 | + warnings.simplefilter("always") |
| 318 | + result = test_fn() |
| 319 | + assert result == "executing" |
| 320 | + assert len(w) == 0 |
| 321 | + importlib.reload(feature_decorator) # Reload to clean up |
| 322 | + |
| 323 | + |
| 324 | +def test_experimental_class_warning_suppressed_with_env_var(): |
| 325 | + """Test experimental class warning suppressed by env var.""" |
| 326 | + with mock.patch.dict(os.environ, {"ADK_DISABLE_EXPERIMENTAL_WARNING": "1"}): |
| 327 | + importlib.reload(feature_decorator) |
| 328 | + from google.adk.utils.feature_decorator import experimental |
| 329 | + |
| 330 | + @experimental |
| 331 | + class TestClass: |
| 332 | + def run(self): |
| 333 | + return "running experimental" |
| 334 | + |
| 335 | + with warnings.catch_warnings(record=True) as w: |
| 336 | + warnings.simplefilter("always") |
| 337 | + result = TestClass().run() |
| 338 | + assert result == "running experimental" |
| 339 | + assert len(w) == 0 |
| 340 | + importlib.reload(feature_decorator) # Reload to clean up |
| 341 | + |
| 342 | + |
| 343 | +def test_experimental_env_var_case_insensitive_and_truthy_values(): |
| 344 | + """Test experimental warning suppression with various truthy values.""" |
| 345 | + for val in ["true", "TRUE", "Yes", "on", "1"]: |
| 346 | + with mock.patch.dict(os.environ, {"ADK_DISABLE_EXPERIMENTAL_WARNING": val}): |
| 347 | + importlib.reload(feature_decorator) |
| 348 | + from google.adk.utils.feature_decorator import experimental |
| 349 | + |
| 350 | + @experimental |
| 351 | + def test_fn(): |
| 352 | + return "executing" |
| 353 | + |
| 354 | + with warnings.catch_warnings(record=True) as w: |
| 355 | + warnings.simplefilter("always") |
| 356 | + _ = test_fn() |
| 357 | + assert len(w) == 0 |
| 358 | + importlib.reload(feature_decorator) # Reload to clean up |
| 359 | + |
| 360 | + |
| 361 | +def test_experimental_suppression_loads_from_dotenv_file(tmp_path): |
| 362 | + """Test experimental warning suppression loading from .env file.""" |
| 363 | + try: |
| 364 | + from dotenv import load_dotenv |
| 365 | + except ImportError: |
| 366 | + import pytest |
| 367 | + pytest.skip("python-dotenv not available") |
| 368 | + |
| 369 | + env_file = tmp_path / ".env" |
| 370 | + env_file.write_text("ADK_DISABLE_EXPERIMENTAL_WARNING=true\n") |
| 371 | + |
| 372 | + # Clear the env var from os.environ if present |
| 373 | + with mock.patch.dict(os.environ, {}): |
| 374 | + os.environ.pop("ADK_DISABLE_EXPERIMENTAL_WARNING", None) |
| 375 | + importlib.reload(feature_decorator) |
| 376 | + |
| 377 | + load_dotenv(env_file.as_posix()) |
| 378 | + # We need to reload again *after* load_dotenv has modified os.environ |
| 379 | + importlib.reload(feature_decorator) |
| 380 | + from google.adk.utils.feature_decorator import experimental |
| 381 | + |
| 382 | + @experimental |
| 383 | + def test_fn(): |
| 384 | + return "executing" |
| 385 | + |
| 386 | + try: |
| 387 | + with warnings.catch_warnings(record=True) as w: |
| 388 | + warnings.simplefilter("always") |
| 389 | + _ = test_fn() |
| 390 | + assert len(w) == 0 |
| 391 | + finally: |
| 392 | + os.environ.pop("ADK_DISABLE_EXPERIMENTAL_WARNING", None) |
| 393 | + importlib.reload(feature_decorator) # Reload to clean up |
0 commit comments