| 1 |
"""Test annotatedyaml loader.""" |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import importlib |
| 5 |
import io |
| 6 |
import os |
| 7 |
import pathlib |
| 8 |
from collections.abc import Generator |
| 9 |
from typing import Any |
| 10 |
from unittest.mock import Mock, patch |
| 11 |
|
| 12 |
import pytest |
| 13 |
import voluptuous as vol |
| 14 |
import yaml as pyyaml |
| 15 |
|
| 16 |
import annotatedyaml as yaml_util |
| 17 |
from annotatedyaml import YAMLException |
| 18 |
from annotatedyaml import loader as yaml_loader |
| 19 |
from tests.common import YAML_CONFIG_FILE |
| 20 |
|
| 21 |
|
| 22 |
def _get_annotation(item: Any) -> tuple[str, int | str] | None: |
| 23 |
if not hasattr(item, "__config_file__"): |
| 24 |
return None |
| 25 |
|
| 26 |
return (item.__config_file__, getattr(item, "__line__", "?")) |
| 27 |
|
| 28 |
|
| 29 |
@pytest.fixture(params=["enable_c_loader", "disable_c_loader"]) |
| 30 |
def try_both_loaders(request: pytest.FixtureRequest) -> Generator[None]: |
| 31 |
"""Disable the yaml c loader.""" |
| 32 |
if request.param != "disable_c_loader": |
| 33 |
yield |
| 34 |
return |
| 35 |
try: |
| 36 |
cloader = pyyaml.CSafeLoader |
| 37 |
except ImportError: |
| 38 |
return |
| 39 |
del pyyaml.CSafeLoader |
| 40 |
importlib.reload(yaml_loader) |
| 41 |
yield |
| 42 |
pyyaml.CSafeLoader = cloader |
| 43 |
importlib.reload(yaml_loader) |
| 44 |
|
| 45 |
|
| 46 |
@pytest.fixture(params=["enable_c_dumper", "disable_c_dumper"]) |
| 47 |
def try_both_dumpers(request: pytest.FixtureRequest) -> Generator[None]: |
| 48 |
"""Disable the yaml c dumper.""" |
| 49 |
if request.param != "disable_c_dumper": |
| 50 |
yield |
| 51 |
return |
| 52 |
try: |
| 53 |
cdumper = pyyaml.CSafeDumper |
| 54 |
except ImportError: |
| 55 |
return |
| 56 |
del pyyaml.CSafeDumper |
| 57 |
importlib.reload(yaml_loader) |
| 58 |
yield |
| 59 |
pyyaml.CSafeDumper = cdumper |
| 60 |
importlib.reload(yaml_loader) |
| 61 |
|
| 62 |
|
| 63 |
@pytest.mark.usefixtures("try_both_loaders") |
| 64 |
def test_simple_list() -> None: |
| 65 |
"""Test simple list.""" |
| 66 |
conf = "config:\n - simple\n - list" |
| 67 |
with io.StringIO(conf) as file: |
| 68 |
doc = yaml_loader.parse_yaml(file) |
| 69 |
assert doc["config"] == ["simple", "list"] |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.usefixtures("try_both_loaders") |
| 73 |
def test_simple_dict() -> None: |
| 74 |
"""Test simple dict.""" |
| 75 |
conf = "key: value" |
| 76 |
with io.StringIO(conf) as file: |
| 77 |
doc = yaml_loader.parse_yaml(file) |
| 78 |
assert doc["key"] == "value" |
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.parametrize("mock_yaml", ["message:\n {{ states.state }}"]) |
| 82 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 83 |
def test_unhashable_key(mock_yaml: None) -> None: |
| 84 |
"""Test an unhashable key.""" |
| 85 |
with pytest.raises(YAMLException): |
| 86 |
yaml_loader.load_yaml(YAML_CONFIG_FILE) |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.parametrize("mock_yaml", ["a: a\nnokeyhere"]) |
| 90 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 91 |
def test_no_key(mock_yaml: None) -> None: |
| 92 |
"""Test item without a key.""" |
| 93 |
with pytest.raises(YAMLException): |
| 94 |
yaml_util.load_yaml(YAML_CONFIG_FILE) |
| 95 |
|
| 96 |
|
| 97 |
@pytest.mark.usefixtures("try_both_loaders") |
| 98 |
def test_environment_variable() -> None: |
| 99 |
"""Test config file with environment variable.""" |
| 100 |
os.environ["PASSWORD"] = "secret_password" |
| 101 |
conf = "password: !env_var PASSWORD" |
| 102 |
with io.StringIO(conf) as file: |
| 103 |
doc = yaml_loader.parse_yaml(file) |
| 104 |
assert doc["password"] == "secret_password" |
| 105 |
del os.environ["PASSWORD"] |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.usefixtures("try_both_loaders") |
| 109 |
def test_environment_variable_default() -> None: |
| 110 |
"""Test config file with default value for environment variable.""" |
| 111 |
conf = "password: !env_var PASSWORD secret_password" |
| 112 |
with io.StringIO(conf) as file: |
| 113 |
doc = yaml_loader.parse_yaml(file) |
| 114 |
assert doc["password"] == "secret_password" |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.usefixtures("try_both_loaders") |
| 118 |
def test_invalid_environment_variable() -> None: |
| 119 |
"""Test config file with no environment variable sat.""" |
| 120 |
conf = "password: !env_var PASSWORD" |
| 121 |
with pytest.raises(YAMLException), io.StringIO(conf) as file: |
| 122 |
yaml_loader.parse_yaml(file) |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.parametrize( |
| 126 |
("mock_yaml_files", "value"), |
| 127 |
[ |
| 128 |
({"test.yaml": "value"}, "value"), |
| 129 |
({"test.yaml": None}, {}), |
| 130 |
({"test.yaml": "123"}, 123), |
| 131 |
], |
| 132 |
) |
| 133 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 134 |
def test_include_yaml(mock_yaml_files: None, value: Any) -> None: |
| 135 |
"""Test include yaml.""" |
| 136 |
conf = "key: !include test.yaml" |
| 137 |
with io.StringIO(conf) as file: |
| 138 |
doc = yaml_loader.parse_yaml(file) |
| 139 |
assert doc["key"] == value |
| 140 |
|
| 141 |
|
| 142 |
@patch("annotatedyaml.loader.os.walk") |
| 143 |
@pytest.mark.parametrize( |
| 144 |
("mock_yaml_files", "value"), |
| 145 |
[ |
| 146 |
({"/test/one.yaml": "one", "/test/two.yaml": "two"}, ["one", "two"]), |
| 147 |
({"/test/one.yaml": "1", "/test/two.yaml": "2"}, [1, 2]), |
| 148 |
({"/test/one.yaml": "1", "/test/two.yaml": None}, [1]), |
| 149 |
], |
| 150 |
) |
| 151 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 152 |
def test_include_dir_list(mock_walk: Mock, mock_yaml_files: None, value: Any) -> None: |
| 153 |
"""Test include dir list yaml.""" |
| 154 |
mock_walk.return_value = [["/test", [], ["two.yaml", "one.yaml"]]] |
| 155 |
|
| 156 |
conf = "key: !include_dir_list /test" |
| 157 |
with io.StringIO(conf) as file: |
| 158 |
doc = yaml_loader.parse_yaml(file) |
| 159 |
assert sorted(doc["key"]) == sorted(value) |
| 160 |
|
| 161 |
|
| 162 |
@patch("annotatedyaml.loader.os.walk") |
| 163 |
@pytest.mark.parametrize( |
| 164 |
"mock_yaml_files", |
| 165 |
[ |
| 166 |
{ |
| 167 |
"/test/zero.yaml": "zero", |
| 168 |
"/test/tmp2/one.yaml": "one", |
| 169 |
"/test/tmp2/two.yaml": "two", |
| 170 |
} |
| 171 |
], |
| 172 |
) |
| 173 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 174 |
def test_include_dir_list_recursive(mock_walk: Mock, mock_yaml_files: None) -> None: |
| 175 |
"""Test include dir recursive list yaml.""" |
| 176 |
mock_walk.return_value = [ |
| 177 |
["/test", ["tmp2", ".ignore", "ignore"], ["zero.yaml"]], |
| 178 |
["/test/tmp2", [], ["one.yaml", "two.yaml"]], |
| 179 |
["/test/ignore", [], [".ignore.yaml"]], |
| 180 |
] |
| 181 |
|
| 182 |
conf = "key: !include_dir_list /test" |
| 183 |
with io.StringIO(conf) as file: |
| 184 |
assert ".ignore" in mock_walk.return_value[0][1], "Expecting .ignore in here" |
| 185 |
doc = yaml_loader.parse_yaml(file) |
| 186 |
assert "tmp2" in mock_walk.return_value[0][1] |
| 187 |
assert ".ignore" not in mock_walk.return_value[0][1] |
| 188 |
assert sorted(doc["key"]) == sorted(["zero", "one", "two"]) |
| 189 |
|
| 190 |
|
| 191 |
@patch("annotatedyaml.loader.os.walk") |
| 192 |
@pytest.mark.parametrize( |
| 193 |
("mock_yaml_files", "value"), |
| 194 |
[ |
| 195 |
( |
| 196 |
{"/test/first.yaml": "one", "/test/second.yaml": "two"}, |
| 197 |
{"first": "one", "second": "two"}, |
| 198 |
), |
| 199 |
( |
| 200 |
{"/test/first.yaml": "1", "/test/second.yaml": "2"}, |
| 201 |
{"first": 1, "second": 2}, |
| 202 |
), |
| 203 |
( |
| 204 |
{"/test/first.yaml": "1", "/test/second.yaml": None}, |
| 205 |
{"first": 1, "second": {}}, |
| 206 |
), |
| 207 |
], |
| 208 |
) |
| 209 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 210 |
def test_include_dir_named(mock_walk: Mock, mock_yaml_files: None, value: Any) -> None: |
| 211 |
"""Test include dir named yaml.""" |
| 212 |
mock_walk.return_value = [ |
| 213 |
["/test", [], ["first.yaml", "second.yaml", "secrets.yaml"]] |
| 214 |
] |
| 215 |
|
| 216 |
conf = "key: !include_dir_named /test" |
| 217 |
with io.StringIO(conf) as file: |
| 218 |
doc = yaml_loader.parse_yaml(file) |
| 219 |
assert doc["key"] == value |
| 220 |
|
| 221 |
|
| 222 |
@patch("annotatedyaml.loader.os.walk") |
| 223 |
@pytest.mark.parametrize( |
| 224 |
"mock_yaml_files", |
| 225 |
[ |
| 226 |
{ |
| 227 |
"/test/first.yaml": "one", |
| 228 |
"/test/tmp2/second.yaml": "two", |
| 229 |
"/test/tmp2/third.yaml": "three", |
| 230 |
} |
| 231 |
], |
| 232 |
) |
| 233 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 234 |
def test_include_dir_named_recursive(mock_walk: Mock, mock_yaml_files: None) -> None: |
| 235 |
"""Test include dir named yaml.""" |
| 236 |
mock_walk.return_value = [ |
| 237 |
["/test", ["tmp2", ".ignore", "ignore"], ["first.yaml"]], |
| 238 |
["/test/tmp2", [], ["second.yaml", "third.yaml"]], |
| 239 |
["/test/ignore", [], [".ignore.yaml"]], |
| 240 |
] |
| 241 |
|
| 242 |
conf = "key: !include_dir_named /test" |
| 243 |
correct = {"first": "one", "second": "two", "third": "three"} |
| 244 |
with io.StringIO(conf) as file: |
| 245 |
assert ".ignore" in mock_walk.return_value[0][1], "Expecting .ignore in here" |
| 246 |
doc = yaml_loader.parse_yaml(file) |
| 247 |
assert "tmp2" in mock_walk.return_value[0][1] |
| 248 |
assert ".ignore" not in mock_walk.return_value[0][1] |
| 249 |
assert doc["key"] == correct |
| 250 |
|
| 251 |
|
| 252 |
@patch("annotatedyaml.loader.os.walk") |
| 253 |
@pytest.mark.parametrize( |
| 254 |
("mock_yaml_files", "value"), |
| 255 |
[ |
| 256 |
( |
| 257 |
{"/test/first.yaml": "- one", "/test/second.yaml": "- two\n- three"}, |
| 258 |
["one", "two", "three"], |
| 259 |
), |
| 260 |
( |
| 261 |
{"/test/first.yaml": "- 1", "/test/second.yaml": "- 2\n- 3"}, |
| 262 |
[1, 2, 3], |
| 263 |
), |
| 264 |
( |
| 265 |
{"/test/first.yaml": "- 1", "/test/second.yaml": None}, |
| 266 |
[1], |
| 267 |
), |
| 268 |
], |
| 269 |
) |
| 270 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 271 |
def test_include_dir_merge_list( |
| 272 |
mock_walk: Mock, mock_yaml_files: None, value: Any |
| 273 |
) -> None: |
| 274 |
"""Test include dir merge list yaml.""" |
| 275 |
mock_walk.return_value = [["/test", [], ["first.yaml", "second.yaml"]]] |
| 276 |
|
| 277 |
conf = "key: !include_dir_merge_list /test" |
| 278 |
with io.StringIO(conf) as file: |
| 279 |
doc = yaml_loader.parse_yaml(file) |
| 280 |
assert sorted(doc["key"]) == sorted(value) |
| 281 |
|
| 282 |
|
| 283 |
@patch("annotatedyaml.loader.os.walk") |
| 284 |
@pytest.mark.parametrize( |
| 285 |
"mock_yaml_files", |
| 286 |
[ |
| 287 |
{ |
| 288 |
"/test/first.yaml": "- one", |
| 289 |
"/test/tmp2/second.yaml": "- two", |
| 290 |
"/test/tmp2/third.yaml": "- three\n- four", |
| 291 |
} |
| 292 |
], |
| 293 |
) |
| 294 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 295 |
def test_include_dir_merge_list_recursive( |
| 296 |
mock_walk: Mock, mock_yaml_files: None |
| 297 |
) -> None: |
| 298 |
"""Test include dir merge list yaml.""" |
| 299 |
mock_walk.return_value = [ |
| 300 |
["/test", ["tmp2", ".ignore", "ignore"], ["first.yaml"]], |
| 301 |
["/test/tmp2", [], ["second.yaml", "third.yaml"]], |
| 302 |
["/test/ignore", [], [".ignore.yaml"]], |
| 303 |
] |
| 304 |
|
| 305 |
conf = "key: !include_dir_merge_list /test" |
| 306 |
with io.StringIO(conf) as file: |
| 307 |
assert ".ignore" in mock_walk.return_value[0][1], "Expecting .ignore in here" |
| 308 |
doc = yaml_loader.parse_yaml(file) |
| 309 |
assert "tmp2" in mock_walk.return_value[0][1] |
| 310 |
assert ".ignore" not in mock_walk.return_value[0][1] |
| 311 |
assert sorted(doc["key"]) == sorted(["one", "two", "three", "four"]) |
| 312 |
|
| 313 |
|
| 314 |
@patch("annotatedyaml.loader.os.walk") |
| 315 |
@pytest.mark.parametrize( |
| 316 |
("mock_yaml_files", "value"), |
| 317 |
[ |
| 318 |
( |
| 319 |
{ |
| 320 |
"/test/first.yaml": "key1: one", |
| 321 |
"/test/second.yaml": "key2: two\nkey3: three", |
| 322 |
}, |
| 323 |
{"key1": "one", "key2": "two", "key3": "three"}, |
| 324 |
), |
| 325 |
( |
| 326 |
{ |
| 327 |
"/test/first.yaml": "key1: 1", |
| 328 |
"/test/second.yaml": "key2: 2\nkey3: 3", |
| 329 |
}, |
| 330 |
{"key1": 1, "key2": 2, "key3": 3}, |
| 331 |
), |
| 332 |
( |
| 333 |
{ |
| 334 |
"/test/first.yaml": "key1: 1", |
| 335 |
"/test/second.yaml": None, |
| 336 |
}, |
| 337 |
{"key1": 1}, |
| 338 |
), |
| 339 |
], |
| 340 |
) |
| 341 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 342 |
def test_include_dir_merge_named( |
| 343 |
mock_walk: Mock, mock_yaml_files: None, value: Any |
| 344 |
) -> None: |
| 345 |
"""Test include dir merge named yaml.""" |
| 346 |
mock_walk.return_value = [["/test", [], ["first.yaml", "second.yaml"]]] |
| 347 |
|
| 348 |
conf = "key: !include_dir_merge_named /test" |
| 349 |
with io.StringIO(conf) as file: |
| 350 |
doc = yaml_loader.parse_yaml(file) |
| 351 |
assert doc["key"] == value |
| 352 |
|
| 353 |
|
| 354 |
@patch("annotatedyaml.loader.os.walk") |
| 355 |
@pytest.mark.parametrize( |
| 356 |
"mock_yaml_files", |
| 357 |
[ |
| 358 |
{ |
| 359 |
"/test/first.yaml": "key1: one", |
| 360 |
"/test/tmp2/second.yaml": "key2: two", |
| 361 |
"/test/tmp2/third.yaml": "key3: three\nkey4: four", |
| 362 |
} |
| 363 |
], |
| 364 |
) |
| 365 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 366 |
def test_include_dir_merge_named_recursive( |
| 367 |
mock_walk: Mock, mock_yaml_files: None |
| 368 |
) -> None: |
| 369 |
"""Test include dir merge named yaml.""" |
| 370 |
mock_walk.return_value = [ |
| 371 |
["/test", ["tmp2", ".ignore", "ignore"], ["first.yaml"]], |
| 372 |
["/test/tmp2", [], ["second.yaml", "third.yaml"]], |
| 373 |
["/test/ignore", [], [".ignore.yaml"]], |
| 374 |
] |
| 375 |
|
| 376 |
conf = "key: !include_dir_merge_named /test" |
| 377 |
with io.StringIO(conf) as file: |
| 378 |
assert ".ignore" in mock_walk.return_value[0][1], "Expecting .ignore in here" |
| 379 |
doc = yaml_loader.parse_yaml(file) |
| 380 |
assert "tmp2" in mock_walk.return_value[0][1] |
| 381 |
assert ".ignore" not in mock_walk.return_value[0][1] |
| 382 |
assert doc["key"] == { |
| 383 |
"key1": "one", |
| 384 |
"key2": "two", |
| 385 |
"key3": "three", |
| 386 |
"key4": "four", |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
@patch("annotatedyaml.loader.open", create=True) |
| 391 |
@pytest.mark.usefixtures("try_both_loaders") |
| 392 |
def test_load_yaml_encoding_error(mock_open: Mock) -> None: |
| 393 |
"""Test raising a UnicodeDecodeError.""" |
| 394 |
mock_open.side_effect = UnicodeDecodeError("", b"", 1, 0, "") |
| 395 |
with pytest.raises(YAMLException): |
| 396 |
yaml_loader.load_yaml("test") |
| 397 |
|
| 398 |
|
| 399 |
@pytest.mark.usefixtures("try_both_dumpers") |
| 400 |
def test_dump() -> None: |
| 401 |
"""The that the dump method returns empty None values.""" |
| 402 |
assert yaml_util.dump({"a": None, "b": "b"}) == "a:\nb: b\n" |
| 403 |
|
| 404 |
|
| 405 |
@pytest.mark.usefixtures("try_both_dumpers") |
| 406 |
def test_dump_unicode() -> None: |
| 407 |
"""The that the dump method returns empty None values.""" |
| 408 |
assert yaml_util.dump({"a": None, "b": "привет"}) == "a:\nb: привет\n" |
| 409 |
|
| 410 |
|
| 411 |
@pytest.mark.parametrize("mock_yaml", ['key: [1, "2", 3]']) |
| 412 |
@pytest.mark.usefixtures("try_both_dumpers", "patch_yaml_config") |
| 413 |
def test_representing_yaml_loaded_data(mock_yaml: None) -> None: |
| 414 |
"""Test we can represent YAML loaded data.""" |
| 415 |
data = yaml_loader.load_yaml(YAML_CONFIG_FILE) |
| 416 |
assert yaml_util.dump(data) == "key:\n- 1\n- '2'\n- 3\n" |
| 417 |
|
| 418 |
|
| 419 |
@pytest.mark.parametrize("mock_yaml", ["key: thing1\nkey: thing2"]) |
| 420 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 421 |
def test_duplicate_key(caplog: pytest.LogCaptureFixture, mock_yaml: None) -> None: |
| 422 |
"""Test duplicate dict keys.""" |
| 423 |
yaml_loader.load_yaml(YAML_CONFIG_FILE) |
| 424 |
assert "contains duplicate key" in caplog.text |
| 425 |
|
| 426 |
|
| 427 |
@pytest.mark.parametrize( |
| 428 |
"mock_yaml_files", |
| 429 |
[{YAML_CONFIG_FILE: "key: !secret a", yaml_util.SECRET_YAML: "a: 1\nb: !secret a"}], |
| 430 |
) |
| 431 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 432 |
def test_no_recursive_secrets(mock_yaml_files: None) -> None: |
| 433 |
"""Test that loading of secrets from the secrets file fails correctly.""" |
| 434 |
with pytest.raises(YAMLException) as e: |
| 435 |
yaml_loader.load_yaml(YAML_CONFIG_FILE) |
| 436 |
|
| 437 |
assert e.value.args == ("Secrets not supported in this YAML file",) |
| 438 |
|
| 439 |
|
| 440 |
def test_input_class() -> None: |
| 441 |
"""Test input class.""" |
| 442 |
yaml_input = yaml_util.Input("hello") |
| 443 |
yaml_input2 = yaml_util.Input("hello") |
| 444 |
|
| 445 |
assert yaml_input.name == "hello" |
| 446 |
assert yaml_input == yaml_input2 |
| 447 |
|
| 448 |
assert len({yaml_input, yaml_input2}) == 1 |
| 449 |
|
| 450 |
|
| 451 |
@pytest.mark.usefixtures("try_both_loaders", "try_both_dumpers") |
| 452 |
def test_input() -> None: |
| 453 |
"""Test loading inputs.""" |
| 454 |
data = {"hello": yaml_util.Input("test_name")} |
| 455 |
assert yaml_util.parse_yaml(yaml_util.dump(data)) == data |
| 456 |
|
| 457 |
|
| 458 |
@pytest.mark.skipif( |
| 459 |
not os.environ.get("HASS_CI"), |
| 460 |
reason="This test validates that the CI has the C loader available", |
| 461 |
) |
| 462 |
def test_c_loader_is_available_in_ci() -> None: |
| 463 |
"""Verify we are testing the C loader in the CI.""" |
| 464 |
assert yaml_util.loader.HAS_C_LOADER is True |
| 465 |
|
| 466 |
|
| 467 |
@pytest.mark.usefixtures("try_both_loaders") |
| 468 |
@pytest.mark.asyncio |
| 469 |
async def test_loading_actual_file_with_syntax_error() -> None: |
| 470 |
"""Test loading a real file with syntax errors.""" |
| 471 |
fixture_path = pathlib.Path(__file__).parent.joinpath("fixtures", "bad.yaml.txt") |
| 472 |
loop = asyncio.get_event_loop() |
| 473 |
|
| 474 |
with pytest.raises(YAMLException): |
| 475 |
await loop.run_in_executor(None, yaml_loader.load_yaml, fixture_path, None) |
| 476 |
|
| 477 |
|
| 478 |
@pytest.mark.usefixtures("try_both_loaders") |
| 479 |
def test_string_annotated() -> None: |
| 480 |
"""Test strings are annotated with file + line.""" |
| 481 |
conf = ( |
| 482 |
"key1: str\n" |
| 483 |
"key2:\n" |
| 484 |
" blah: blah\n" |
| 485 |
"key3:\n" |
| 486 |
" - 1\n" |
| 487 |
" - 2\n" |
| 488 |
" - 3\n" |
| 489 |
"key4: yes\n" |
| 490 |
"key5: 1\n" |
| 491 |
"key6: 1.0\n" |
| 492 |
) |
| 493 |
expected_annotations = { |
| 494 |
"key1": [("<file>", 1), ("<file>", 1)], |
| 495 |
"key2": [("<file>", 2), ("<file>", 3)], |
| 496 |
"key3": [("<file>", 4), ("<file>", 5)], |
| 497 |
"key4": [("<file>", 8), (None, None)], |
| 498 |
"key5": [("<file>", 9), (None, None)], |
| 499 |
"key6": [("<file>", 10), (None, None)], |
| 500 |
} |
| 501 |
with io.StringIO(conf) as file: |
| 502 |
doc = yaml_loader.parse_yaml(file) |
| 503 |
for key, value in doc.items(): |
| 504 |
assert getattr(key, "__config_file__", None) == expected_annotations[key][0][0] |
| 505 |
assert getattr(key, "__line__", None) == expected_annotations[key][0][1] |
| 506 |
assert ( |
| 507 |
getattr(value, "__config_file__", None) == expected_annotations[key][1][0] |
| 508 |
) |
| 509 |
assert getattr(value, "__line__", None) == expected_annotations[key][1][1] |
| 510 |
|
| 511 |
|
| 512 |
@pytest.mark.usefixtures("try_both_loaders") |
| 513 |
def test_string_used_as_vol_schema() -> None: |
| 514 |
"""Test the subclassed strings can be used in voluptuous schemas.""" |
| 515 |
conf = "wanted_data:\n key_1: value_1\n key_2: value_2\n" |
| 516 |
with io.StringIO(conf) as file: |
| 517 |
doc = yaml_loader.parse_yaml(file) |
| 518 |
|
| 519 |
|
| 520 |
schema = vol.Schema( |
| 521 |
{vol.Required(key): value for key, value in doc["wanted_data"].items()}, |
| 522 |
) |
| 523 |
|
| 524 |
schema(doc["wanted_data"]) |
| 525 |
schema({"key_1": "value_1", "key_2": "value_2"}) |
| 526 |
with pytest.raises(vol.Invalid): |
| 527 |
schema({"key_1": "value_2", "key_2": "value_1"}) |
| 528 |
|
| 529 |
|
| 530 |
@pytest.mark.parametrize( |
| 531 |
("mock_yaml", "expected_data"), [("", {}), ("bla:", {"bla": None})] |
| 532 |
) |
| 533 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 534 |
def test_load_yaml_dict(mock_yaml: None, expected_data: Any) -> None: |
| 535 |
"""Test item without a key.""" |
| 536 |
assert yaml_util.load_yaml_dict(YAML_CONFIG_FILE) == expected_data |
| 537 |
|
| 538 |
|
| 539 |
@pytest.mark.parametrize("mock_yaml", ["abc", "123", "[]"]) |
| 540 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 541 |
def test_load_yaml_dict_fail(mock_yaml: None) -> None: |
| 542 |
"""Test item without a key.""" |
| 543 |
with pytest.raises(YAMLException): |
| 544 |
yaml_loader.load_yaml_dict(YAML_CONFIG_FILE) |
| 545 |
|
| 546 |
|
| 547 |
@pytest.mark.parametrize( |
| 548 |
"tag", |
| 549 |
[ |
| 550 |
"!include", |
| 551 |
"!include_dir_named", |
| 552 |
"!include_dir_merge_named", |
| 553 |
"!include_dir_list", |
| 554 |
"!include_dir_merge_list", |
| 555 |
], |
| 556 |
) |
| 557 |
@pytest.mark.usefixtures("try_both_loaders") |
| 558 |
def test_include_without_parameter(tag: str) -> None: |
| 559 |
"""Test include extensions without parameters.""" |
| 560 |
with ( |
| 561 |
io.StringIO(f"key: {tag}") as file, |
| 562 |
pytest.raises(YAMLException, match=f"{tag} needs an argument"), |
| 563 |
): |
| 564 |
yaml_loader.parse_yaml(file) |
| 565 |
|
| 566 |
|
| 567 |
@pytest.mark.parametrize( |
| 568 |
("open_exception", "load_yaml_exception"), |
| 569 |
[ |
| 570 |
(FileNotFoundError, OSError), |
| 571 |
(NotADirectoryError, YAMLException), |
| 572 |
(PermissionError, YAMLException), |
| 573 |
], |
| 574 |
) |
| 575 |
@pytest.mark.usefixtures("try_both_loaders") |
| 576 |
def test_load_yaml_wrap_oserror( |
| 577 |
open_exception: Exception, |
| 578 |
load_yaml_exception: Exception, |
| 579 |
) -> None: |
| 580 |
"""Test load_yaml wraps OSError in AnnotatedYAMLOSError.""" |
| 581 |
with ( |
| 582 |
patch("annotatedyaml.loader.open", side_effect=open_exception), |
| 583 |
pytest.raises(load_yaml_exception), |
| 584 |
): |
| 585 |
yaml_loader.load_yaml("bla") |
| 586 |
|
| 587 |
|
| 588 |
@pytest.mark.parametrize("mock_yaml", ["key: !include missing.yaml"]) |
| 589 |
@pytest.mark.usefixtures("try_both_loaders", "patch_yaml_config") |
| 590 |
def test_load_missing_included_file(mock_yaml: None) -> None: |
| 591 |
"""Test loading a file that includes a missing file.""" |
| 592 |
with pytest.raises(YAMLException): |
| 593 |
yaml_loader.load_yaml(YAML_CONFIG_FILE) |
| 594 |
|
| 595 |
|
| 596 |
@pytest.mark.parametrize("mock_yaml", ['key: [1, "2", 3]']) |
| 597 |
@pytest.mark.usefixtures("try_both_dumpers", "patch_yaml_config") |
| 598 |
def test_getting_annotation(mock_yaml: None) -> None: |
| 599 |
"""Test we can fetch annotations in pure python.""" |
| 600 |
data = yaml_loader.load_yaml(YAML_CONFIG_FILE) |
| 601 |
assert _get_annotation(data) == ("test.yaml", 1) |