"""Step definitions for PyYAML runtime dependency verification BDD scenarios.""" from __future__ import annotations import json import os import tempfile import yaml from behave import given, then, when from packaging.version import Version @when("I verify that PyYAML can be imported") def step_verify_pyyaml_importable(context) -> None: """Attempt to import PyYAML module.""" context.pyyaml_import_result = "success" @then("PyYAML should be importable without error") def step_pyyaml_import_succeeds(context) -> None: """Verify PyYAML import was successful.""" assert context.pyyaml_import_result == "success", ( f"PyYAML import failed: {context.pyyaml_import_result}" ) @then("its version should be >= 6.0.3 to mitigate CVE-2025-8045") def step_pyyaml_version_check(context) -> None: """Verify PyYAML version meets security floor.""" version_str = getattr(yaml, "__version__", "unknown") assert Version(version_str) >= Version("6.0.3"), ( f"PyYAML version {version_str} is below security floor 6.0.3 (CVE-2025-8045)" ) @given( 'a valid YAML actor config file "test.yaml" with content:', ) def step_give_yaml_config_file(context) -> None: """Create a temporary YAML config file for testing.""" content = context.text context.temp_dir = tempfile.mkdtemp() config_path = os.path.join(context.temp_dir, "test.yaml") with open(config_path, "w", encoding="utf-8") as f: f.write(content) context.config_file = config_path @when("I load the YAML config using PyYAML safe_load") def step_load_yaml_safe_load(context) -> None: """Load the YAML config file using yaml.safe_load.""" with open(context.config_file, encoding="utf-8") as f: context.loaded_config = yaml.safe_load(f) @then("the loaded config should equal {expected}") def step_config_matches_expected(context, expected: str) -> None: """Verify the loaded YAML config matches expected value.""" expected_dict = json.loads(expected) assert context.loaded_config == expected_dict, ( f"Expected {expected_dict}, got {context.loaded_config}" )