from pathlib import Path import pytest from datadog_tools.config import DEFAULT_DEFINITION_FILENAME from datadog_tools.utils.files import get_entity_kind_from_entity_definition_file, get_full_entity_definition_file_path def test_resolves_entity_definition_file_path_with_custom_file(): """Test that a custom entity definition file path is resolved correctly.""" repo = "/var/lib/app" custom_file = "custom.yaml" result = get_full_entity_definition_file_path(repo, "entities", custom_file) assert result == Path(repo).resolve() / custom_file def test_resolves_entity_definition_file_path_with_default_file(): """Test that the default entity definition file path is resolved correctly.""" repo = "/var/lib/app" entity = "service" result = get_full_entity_definition_file_path(repo, entity) assert result == Path(repo).resolve() / entity / DEFAULT_DEFINITION_FILENAME def test_extracts_kind_from_valid_yaml_file(tmp_path): """Test that the kind field is extracted correctly from a valid YAML file.""" yaml_file = tmp_path / "entity.yaml" yaml_file.write_text("kind: service\n", encoding="utf-8") kind = get_entity_kind_from_entity_definition_file(yaml_file) assert kind == "service" def test_returns_none_if_kind_missing_in_yaml(tmp_path): """Test that None is returned if the kind field is missing in the YAML file.""" yaml_file = tmp_path / "entity.yaml" yaml_file.write_text("name: test\n", encoding="utf-8") kind = get_entity_kind_from_entity_definition_file(yaml_file) assert kind is None def test_returns_none_if_kind_is_not_string(tmp_path): """Test that None is returned if the kind field is not a string.""" yaml_file = tmp_path / "entity.yaml" yaml_file.write_text("kind: 123\n", encoding="utf-8") kind = get_entity_kind_from_entity_definition_file(yaml_file) assert kind is None def test_strips_and_lowercases_kind_field(tmp_path): """Test that the kind field is stripped of whitespace and lowercased.""" yaml_file = tmp_path / "entity.yaml" yaml_file.write_text("kind: LibRaRy \n", encoding="utf-8") kind = get_entity_kind_from_entity_definition_file(yaml_file) assert kind == "library" def test_raises_exception_for_invalid_yaml(tmp_path): """Test that an exception is raised for invalid YAML syntax.""" yaml_file = tmp_path / "entity.yaml" yaml_file.write_text("kind: [unclosed", encoding="utf-8") with pytest.raises(Exception): get_entity_kind_from_entity_definition_file(yaml_file)