|
| 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 | +"""Unit tests for --enable_features CLI option.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import click |
| 20 | +from click.testing import CliRunner |
| 21 | +from google.adk.cli.cli_tools_click import _apply_feature_overrides |
| 22 | +from google.adk.cli.cli_tools_click import feature_options |
| 23 | +from google.adk.features._feature_registry import _FEATURE_OVERRIDES |
| 24 | +from google.adk.features._feature_registry import _WARNED_FEATURES |
| 25 | +from google.adk.features._feature_registry import FeatureName |
| 26 | +from google.adk.features._feature_registry import is_feature_enabled |
| 27 | +import pytest |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(autouse=True) |
| 31 | +def reset_feature_overrides(): |
| 32 | + """Reset feature overrides and warnings before/after each test.""" |
| 33 | + _FEATURE_OVERRIDES.clear() |
| 34 | + _WARNED_FEATURES.clear() |
| 35 | + yield |
| 36 | + _FEATURE_OVERRIDES.clear() |
| 37 | + _WARNED_FEATURES.clear() |
| 38 | + |
| 39 | + |
| 40 | +class TestApplyFeatureOverrides: |
| 41 | + """Tests for _apply_feature_overrides helper function.""" |
| 42 | + |
| 43 | + def test_single_feature(self): |
| 44 | + """Single feature name is applied correctly.""" |
| 45 | + _apply_feature_overrides(("JSON_SCHEMA_FOR_FUNC_DECL",)) |
| 46 | + assert is_feature_enabled(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL) |
| 47 | + |
| 48 | + def test_comma_separated_features(self): |
| 49 | + """Comma-separated feature names are applied correctly.""" |
| 50 | + _apply_feature_overrides(( |
| 51 | + "JSON_SCHEMA_FOR_FUNC_DECL,PROGRESSIVE_SSE_STREAMING", |
| 52 | + )) |
| 53 | + assert is_feature_enabled(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL) |
| 54 | + assert is_feature_enabled(FeatureName.PROGRESSIVE_SSE_STREAMING) |
| 55 | + |
| 56 | + def test_multiple_flag_values(self): |
| 57 | + """Multiple --enable_features flags are applied correctly.""" |
| 58 | + _apply_feature_overrides(( |
| 59 | + "JSON_SCHEMA_FOR_FUNC_DECL", |
| 60 | + "PROGRESSIVE_SSE_STREAMING", |
| 61 | + )) |
| 62 | + assert is_feature_enabled(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL) |
| 63 | + assert is_feature_enabled(FeatureName.PROGRESSIVE_SSE_STREAMING) |
| 64 | + |
| 65 | + def test_whitespace_handling(self): |
| 66 | + """Whitespace around feature names is stripped.""" |
| 67 | + _apply_feature_overrides((" JSON_SCHEMA_FOR_FUNC_DECL , COMPUTER_USE ",)) |
| 68 | + assert is_feature_enabled(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL) |
| 69 | + assert is_feature_enabled(FeatureName.COMPUTER_USE) |
| 70 | + |
| 71 | + def test_empty_string_ignored(self): |
| 72 | + """Empty strings in the list are ignored.""" |
| 73 | + _apply_feature_overrides(("",)) |
| 74 | + # No error should be raised |
| 75 | + |
| 76 | + def test_unknown_feature_warns(self, capsys): |
| 77 | + """Unknown feature names emit a warning.""" |
| 78 | + _apply_feature_overrides(("UNKNOWN_FEATURE_XYZ",)) |
| 79 | + captured = capsys.readouterr() |
| 80 | + assert "WARNING" in captured.err |
| 81 | + assert "UNKNOWN_FEATURE_XYZ" in captured.err |
| 82 | + assert "Valid names are:" in captured.err |
| 83 | + |
| 84 | + |
| 85 | +class TestFeatureOptionsDecorator: |
| 86 | + """Tests for feature_options decorator.""" |
| 87 | + |
| 88 | + def test_decorator_adds_enable_features_option(self): |
| 89 | + """Decorator adds --enable_features option to command.""" |
| 90 | + |
| 91 | + @click.command() |
| 92 | + @feature_options() |
| 93 | + def test_cmd(): |
| 94 | + pass |
| 95 | + |
| 96 | + runner = CliRunner() |
| 97 | + result = runner.invoke(test_cmd, ["--help"]) |
| 98 | + assert "--enable_features" in result.output |
| 99 | + |
| 100 | + def test_enable_features_applied_before_command(self): |
| 101 | + """Features are enabled before the command function runs.""" |
| 102 | + feature_was_enabled = [] |
| 103 | + |
| 104 | + @click.command() |
| 105 | + @feature_options() |
| 106 | + def test_cmd(): |
| 107 | + feature_was_enabled.append( |
| 108 | + is_feature_enabled(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL) |
| 109 | + ) |
| 110 | + |
| 111 | + runner = CliRunner() |
| 112 | + runner.invoke( |
| 113 | + test_cmd, |
| 114 | + ["--enable_features=JSON_SCHEMA_FOR_FUNC_DECL"], |
| 115 | + catch_exceptions=False, |
| 116 | + ) |
| 117 | + assert feature_was_enabled == [True] |
| 118 | + |
| 119 | + def test_multiple_enable_features_flags(self): |
| 120 | + """Multiple --enable_features flags work correctly.""" |
| 121 | + enabled_features = [] |
| 122 | + |
| 123 | + @click.command() |
| 124 | + @feature_options() |
| 125 | + def test_cmd(): |
| 126 | + enabled_features.append( |
| 127 | + is_feature_enabled(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL) |
| 128 | + ) |
| 129 | + enabled_features.append( |
| 130 | + is_feature_enabled(FeatureName.PROGRESSIVE_SSE_STREAMING) |
| 131 | + ) |
| 132 | + |
| 133 | + runner = CliRunner() |
| 134 | + runner.invoke( |
| 135 | + test_cmd, |
| 136 | + [ |
| 137 | + "--enable_features=JSON_SCHEMA_FOR_FUNC_DECL", |
| 138 | + "--enable_features=PROGRESSIVE_SSE_STREAMING", |
| 139 | + ], |
| 140 | + catch_exceptions=False, |
| 141 | + ) |
| 142 | + assert enabled_features == [True, True] |
| 143 | + |
| 144 | + def test_comma_separated_enable_features(self): |
| 145 | + """Comma-separated feature names work correctly.""" |
| 146 | + enabled_features = [] |
| 147 | + |
| 148 | + @click.command() |
| 149 | + @feature_options() |
| 150 | + def test_cmd(): |
| 151 | + enabled_features.append( |
| 152 | + is_feature_enabled(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL) |
| 153 | + ) |
| 154 | + enabled_features.append( |
| 155 | + is_feature_enabled(FeatureName.PROGRESSIVE_SSE_STREAMING) |
| 156 | + ) |
| 157 | + |
| 158 | + runner = CliRunner() |
| 159 | + runner.invoke( |
| 160 | + test_cmd, |
| 161 | + [ |
| 162 | + "--enable_features=JSON_SCHEMA_FOR_FUNC_DECL,PROGRESSIVE_SSE_STREAMING" |
| 163 | + ], |
| 164 | + catch_exceptions=False, |
| 165 | + ) |
| 166 | + assert enabled_features == [True, True] |
| 167 | + |
| 168 | + def test_no_enable_features_flag(self): |
| 169 | + """Command works without --enable_features flag.""" |
| 170 | + enabled_features = [] |
| 171 | + |
| 172 | + @click.command() |
| 173 | + @feature_options() |
| 174 | + def test_cmd(): |
| 175 | + enabled_features.append( |
| 176 | + is_feature_enabled(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL) |
| 177 | + ) |
| 178 | + |
| 179 | + runner = CliRunner() |
| 180 | + result = runner.invoke(test_cmd, [], catch_exceptions=False) |
| 181 | + assert result.exit_code == 0 |
| 182 | + assert enabled_features == [False] |
| 183 | + |
| 184 | + def test_preserves_function_metadata(self): |
| 185 | + """Decorator preserves the wrapped function's metadata.""" |
| 186 | + |
| 187 | + @click.command() |
| 188 | + @feature_options() |
| 189 | + def my_test_command(): |
| 190 | + """My docstring.""" |
| 191 | + pass |
| 192 | + |
| 193 | + # The callback should have preserved metadata |
| 194 | + assert ( |
| 195 | + "my_test_command" in my_test_command.name |
| 196 | + or my_test_command.callback.__name__ == "my_test_command" |
| 197 | + ) |
0 commit comments