|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +import os |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from vllm.envs import env_list_with_choices, env_with_choices |
| 10 | + |
| 11 | + |
| 12 | +class TestEnvWithChoices: |
| 13 | + """Test cases for env_with_choices function.""" |
| 14 | + |
| 15 | + def test_default_value_returned_when_env_not_set(self): |
| 16 | + """Test default is returned when env var is not set.""" |
| 17 | + env_func = env_with_choices("NONEXISTENT_ENV", "default", |
| 18 | + ["option1", "option2"]) |
| 19 | + assert env_func() == "default" |
| 20 | + |
| 21 | + def test_none_default_returned_when_env_not_set(self): |
| 22 | + """Test that None is returned when env not set and default is None.""" |
| 23 | + env_func = env_with_choices("NONEXISTENT_ENV", None, |
| 24 | + ["option1", "option2"]) |
| 25 | + assert env_func() is None |
| 26 | + |
| 27 | + def test_valid_value_returned_case_sensitive(self): |
| 28 | + """Test that valid value is returned in case sensitive mode.""" |
| 29 | + with patch.dict(os.environ, {"TEST_ENV": "option1"}): |
| 30 | + env_func = env_with_choices("TEST_ENV", |
| 31 | + "default", ["option1", "option2"], |
| 32 | + case_sensitive=True) |
| 33 | + assert env_func() == "option1" |
| 34 | + |
| 35 | + def test_valid_lowercase_value_returned_case_insensitive(self): |
| 36 | + """Test that lowercase value is accepted in case insensitive mode.""" |
| 37 | + with patch.dict(os.environ, {"TEST_ENV": "option1"}): |
| 38 | + env_func = env_with_choices("TEST_ENV", |
| 39 | + "default", ["OPTION1", "OPTION2"], |
| 40 | + case_sensitive=False) |
| 41 | + assert env_func() == "option1" |
| 42 | + |
| 43 | + def test_valid_uppercase_value_returned_case_insensitive(self): |
| 44 | + """Test that uppercase value is accepted in case insensitive mode.""" |
| 45 | + with patch.dict(os.environ, {"TEST_ENV": "OPTION1"}): |
| 46 | + env_func = env_with_choices("TEST_ENV", |
| 47 | + "default", ["option1", "option2"], |
| 48 | + case_sensitive=False) |
| 49 | + assert env_func() == "OPTION1" |
| 50 | + |
| 51 | + def test_invalid_value_raises_error_case_sensitive(self): |
| 52 | + """Test that invalid value raises ValueError in case sensitive mode.""" |
| 53 | + with patch.dict(os.environ, {"TEST_ENV": "invalid"}): |
| 54 | + env_func = env_with_choices("TEST_ENV", |
| 55 | + "default", ["option1", "option2"], |
| 56 | + case_sensitive=True) |
| 57 | + with pytest.raises(ValueError, |
| 58 | + match="Invalid value 'invalid' for TEST_ENV"): |
| 59 | + env_func() |
| 60 | + |
| 61 | + def test_case_mismatch_raises_error_case_sensitive(self): |
| 62 | + """Test that case mismatch raises ValueError in case sensitive mode.""" |
| 63 | + with patch.dict(os.environ, {"TEST_ENV": "OPTION1"}): |
| 64 | + env_func = env_with_choices("TEST_ENV", |
| 65 | + "default", ["option1", "option2"], |
| 66 | + case_sensitive=True) |
| 67 | + with pytest.raises(ValueError, |
| 68 | + match="Invalid value 'OPTION1' for TEST_ENV"): |
| 69 | + env_func() |
| 70 | + |
| 71 | + def test_invalid_value_raises_error_case_insensitive(self): |
| 72 | + """Test that invalid value raises ValueError when case insensitive.""" |
| 73 | + with patch.dict(os.environ, {"TEST_ENV": "invalid"}): |
| 74 | + env_func = env_with_choices("TEST_ENV", |
| 75 | + "default", ["option1", "option2"], |
| 76 | + case_sensitive=False) |
| 77 | + with pytest.raises(ValueError, |
| 78 | + match="Invalid value 'invalid' for TEST_ENV"): |
| 79 | + env_func() |
| 80 | + |
| 81 | + def test_callable_choices_resolved_correctly(self): |
| 82 | + """Test that callable choices are resolved correctly.""" |
| 83 | + |
| 84 | + def get_choices(): |
| 85 | + return ["dynamic1", "dynamic2"] |
| 86 | + |
| 87 | + with patch.dict(os.environ, {"TEST_ENV": "dynamic1"}): |
| 88 | + env_func = env_with_choices("TEST_ENV", "default", get_choices) |
| 89 | + assert env_func() == "dynamic1" |
| 90 | + |
| 91 | + def test_callable_choices_with_invalid_value(self): |
| 92 | + """Test that callable choices raise error for invalid values.""" |
| 93 | + |
| 94 | + def get_choices(): |
| 95 | + return ["dynamic1", "dynamic2"] |
| 96 | + |
| 97 | + with patch.dict(os.environ, {"TEST_ENV": "invalid"}): |
| 98 | + env_func = env_with_choices("TEST_ENV", "default", get_choices) |
| 99 | + with pytest.raises(ValueError, |
| 100 | + match="Invalid value 'invalid' for TEST_ENV"): |
| 101 | + env_func() |
| 102 | + |
| 103 | + |
| 104 | +class TestEnvListWithChoices: |
| 105 | + """Test cases for env_list_with_choices function.""" |
| 106 | + |
| 107 | + def test_default_list_returned_when_env_not_set(self): |
| 108 | + """Test that default list is returned when env var is not set.""" |
| 109 | + env_func = env_list_with_choices("NONEXISTENT_ENV", |
| 110 | + ["default1", "default2"], |
| 111 | + ["option1", "option2"]) |
| 112 | + assert env_func() == ["default1", "default2"] |
| 113 | + |
| 114 | + def test_empty_default_list_returned_when_env_not_set(self): |
| 115 | + """Test that empty default list is returned when env not set.""" |
| 116 | + env_func = env_list_with_choices("NONEXISTENT_ENV", [], |
| 117 | + ["option1", "option2"]) |
| 118 | + assert env_func() == [] |
| 119 | + |
| 120 | + def test_single_valid_value_parsed_correctly(self): |
| 121 | + """Test that single valid value is parsed correctly.""" |
| 122 | + with patch.dict(os.environ, {"TEST_ENV": "option1"}): |
| 123 | + env_func = env_list_with_choices("TEST_ENV", [], |
| 124 | + ["option1", "option2"]) |
| 125 | + assert env_func() == ["option1"] |
| 126 | + |
| 127 | + def test_multiple_valid_values_parsed_correctly(self): |
| 128 | + """Test that multiple valid values are parsed correctly.""" |
| 129 | + with patch.dict(os.environ, {"TEST_ENV": "option1,option2"}): |
| 130 | + env_func = env_list_with_choices("TEST_ENV", [], |
| 131 | + ["option1", "option2"]) |
| 132 | + assert env_func() == ["option1", "option2"] |
| 133 | + |
| 134 | + def test_values_with_whitespace_trimmed(self): |
| 135 | + """Test that values with whitespace are trimmed correctly.""" |
| 136 | + with patch.dict(os.environ, {"TEST_ENV": " option1 , option2 "}): |
| 137 | + env_func = env_list_with_choices("TEST_ENV", [], |
| 138 | + ["option1", "option2"]) |
| 139 | + assert env_func() == ["option1", "option2"] |
| 140 | + |
| 141 | + def test_empty_values_filtered_out(self): |
| 142 | + """Test that empty values are filtered out.""" |
| 143 | + with patch.dict(os.environ, {"TEST_ENV": "option1,,option2,"}): |
| 144 | + env_func = env_list_with_choices("TEST_ENV", [], |
| 145 | + ["option1", "option2"]) |
| 146 | + assert env_func() == ["option1", "option2"] |
| 147 | + |
| 148 | + def test_empty_string_returns_default(self): |
| 149 | + """Test that empty string returns default.""" |
| 150 | + with patch.dict(os.environ, {"TEST_ENV": ""}): |
| 151 | + env_func = env_list_with_choices("TEST_ENV", ["default"], |
| 152 | + ["option1", "option2"]) |
| 153 | + assert env_func() == ["default"] |
| 154 | + |
| 155 | + def test_only_commas_returns_default(self): |
| 156 | + """Test that string with only commas returns default.""" |
| 157 | + with patch.dict(os.environ, {"TEST_ENV": ",,,"}): |
| 158 | + env_func = env_list_with_choices("TEST_ENV", ["default"], |
| 159 | + ["option1", "option2"]) |
| 160 | + assert env_func() == ["default"] |
| 161 | + |
| 162 | + def test_case_sensitive_validation(self): |
| 163 | + """Test case sensitive validation.""" |
| 164 | + with patch.dict(os.environ, {"TEST_ENV": "option1,OPTION2"}): |
| 165 | + env_func = env_list_with_choices("TEST_ENV", [], |
| 166 | + ["option1", "option2"], |
| 167 | + case_sensitive=True) |
| 168 | + with pytest.raises(ValueError, |
| 169 | + match="Invalid value 'OPTION2' in TEST_ENV"): |
| 170 | + env_func() |
| 171 | + |
| 172 | + def test_case_insensitive_validation(self): |
| 173 | + """Test case insensitive validation.""" |
| 174 | + with patch.dict(os.environ, {"TEST_ENV": "OPTION1,option2"}): |
| 175 | + env_func = env_list_with_choices("TEST_ENV", [], |
| 176 | + ["option1", "option2"], |
| 177 | + case_sensitive=False) |
| 178 | + assert env_func() == ["OPTION1", "option2"] |
| 179 | + |
| 180 | + def test_invalid_value_in_list_raises_error(self): |
| 181 | + """Test that invalid value in list raises ValueError.""" |
| 182 | + with patch.dict(os.environ, {"TEST_ENV": "option1,invalid,option2"}): |
| 183 | + env_func = env_list_with_choices("TEST_ENV", [], |
| 184 | + ["option1", "option2"]) |
| 185 | + with pytest.raises(ValueError, |
| 186 | + match="Invalid value 'invalid' in TEST_ENV"): |
| 187 | + env_func() |
| 188 | + |
| 189 | + def test_callable_choices_resolved_correctly(self): |
| 190 | + """Test that callable choices are resolved correctly.""" |
| 191 | + |
| 192 | + def get_choices(): |
| 193 | + return ["dynamic1", "dynamic2"] |
| 194 | + |
| 195 | + with patch.dict(os.environ, {"TEST_ENV": "dynamic1,dynamic2"}): |
| 196 | + env_func = env_list_with_choices("TEST_ENV", [], get_choices) |
| 197 | + assert env_func() == ["dynamic1", "dynamic2"] |
| 198 | + |
| 199 | + def test_callable_choices_with_invalid_value(self): |
| 200 | + """Test that callable choices raise error for invalid values.""" |
| 201 | + |
| 202 | + def get_choices(): |
| 203 | + return ["dynamic1", "dynamic2"] |
| 204 | + |
| 205 | + with patch.dict(os.environ, {"TEST_ENV": "dynamic1,invalid"}): |
| 206 | + env_func = env_list_with_choices("TEST_ENV", [], get_choices) |
| 207 | + with pytest.raises(ValueError, |
| 208 | + match="Invalid value 'invalid' in TEST_ENV"): |
| 209 | + env_func() |
| 210 | + |
| 211 | + def test_duplicate_values_preserved(self): |
| 212 | + """Test that duplicate values in the list are preserved.""" |
| 213 | + with patch.dict(os.environ, {"TEST_ENV": "option1,option1,option2"}): |
| 214 | + env_func = env_list_with_choices("TEST_ENV", [], |
| 215 | + ["option1", "option2"]) |
| 216 | + assert env_func() == ["option1", "option1", "option2"] |
0 commit comments