From bc2de829697795cecebc696b342a11c2a95314aa Mon Sep 17 00:00:00 2001 From: Asher Norland Date: Thu, 17 Apr 2025 08:27:57 -0400 Subject: [PATCH 1/2] Test and Fix CMake's `CacheVariable` Schema --- cppython/plugins/cmake/schema.py | 20 +++++----- tests/unit/plugins/cmake/test_schema.py | 51 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 tests/unit/plugins/cmake/test_schema.py diff --git a/cppython/plugins/cmake/schema.py b/cppython/plugins/cmake/schema.py index dba1005..d7456c2 100644 --- a/cppython/plugins/cmake/schema.py +++ b/cppython/plugins/cmake/schema.py @@ -5,7 +5,7 @@ configuration presets, and synchronization data. """ -from enum import Enum, auto +from enum import StrEnum from pathlib import Path from typing import Annotated @@ -15,20 +15,20 @@ from cppython.core.schema import CPPythonModel, SyncData -class VariableType(Enum): +class VariableType(StrEnum): """Defines the types of variables that can be used in CMake cache. Args: Enum: Base class for creating enumerations. """ - BOOL = (auto(),) # Boolean ON/OFF value. - PATH = (auto(),) # Path to a directory. - FILEPATH = (auto(),) # Path to a file. - STRING = (auto(),) # Generic string value. - INTERNAL = (auto(),) # Do not present in GUI at all. - STATIC = (auto(),) # Value managed by CMake, do not change. - UNINITIALIZED = auto() # Type not yet specified. + BOOL = 'BOOL' + PATH = 'PATH' + FILEPATH = 'FILEPATH' + STRING = 'STRING' + INTERNAL = 'INTERNAL' + STATIC = 'STATIC' + UNINITIALIZED = 'UNINITIALIZED' class CacheVariable(CPPythonModel, extra='forbid'): @@ -39,7 +39,7 @@ class CacheVariable(CPPythonModel, extra='forbid'): value: The value of the variable, which can be a boolean or string. """ - type: None | VariableType + type: None | VariableType = None value: bool | str diff --git a/tests/unit/plugins/cmake/test_schema.py b/tests/unit/plugins/cmake/test_schema.py new file mode 100644 index 0000000..382f4f3 --- /dev/null +++ b/tests/unit/plugins/cmake/test_schema.py @@ -0,0 +1,51 @@ +"""Tests for the CMake schema""" + +from cppython.plugins.cmake.schema import CacheVariable, VariableType + + +class TestCacheVariable: + """Tests for the CacheVariable class""" + + @staticmethod + def test_cache_variable_bool() -> None: + """Tests the CacheVariable class with a boolean value""" + var = CacheVariable(type=VariableType.BOOL, value=True) + assert var.type == VariableType.BOOL + assert var.value is True + + @staticmethod + def test_cache_variable_string() -> None: + """Tests the CacheVariable class with a string value""" + var = CacheVariable(type=VariableType.STRING, value='SomeValue') + assert var.type == VariableType.STRING + assert var.value == 'SomeValue' + + @staticmethod + def test_cache_variable_null_type() -> None: + """Tests the CacheVariable class with a null type""" + var = CacheVariable(type=None, value='Unset') + assert var.type is None + assert var.value == 'Unset' + + @staticmethod + def test_cache_variable_type_enum_values() -> None: + """Tests the CacheVariable class with enum values""" + # Ensure all CMake types are present + expected = {'BOOL', 'PATH', 'FILEPATH', 'STRING', 'INTERNAL', 'STATIC', 'UNINITIALIZED'} + actual = {v.value for v in VariableType} + assert expected == actual + + @staticmethod + def test_cache_variable_bool_value_as_string() -> None: + """Tests the CacheVariable class with a boolean value as a string""" + # CMake allows bool as "TRUE"/"FALSE" as well + var = CacheVariable(type=VariableType.BOOL, value='TRUE') + assert var.value == 'TRUE' + + @staticmethod + def test_cache_variable_type_optional() -> None: + """Tests the CacheVariable class with an optional type""" + # type is optional + var = CacheVariable(value='SomeValue') + assert var.type is None + assert var.value == 'SomeValue' From 5e89d68ee278c81cf2a01de4f1260518d7b903f3 Mon Sep 17 00:00:00 2001 From: Asher Norland Date: Thu, 17 Apr 2025 08:29:21 -0400 Subject: [PATCH 2/2] Fix Lint Issues --- cppython/plugins/cmake/builder.py | 2 ++ cppython/plugins/cmake/resolution.py | 2 -- tests/unit/plugins/cmake/test_presets.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cppython/plugins/cmake/builder.py b/cppython/plugins/cmake/builder.py index d489c2a..6320112 100644 --- a/cppython/plugins/cmake/builder.py +++ b/cppython/plugins/cmake/builder.py @@ -120,6 +120,7 @@ def generate_root_preset(preset_file: Path, cppython_preset_file: Path, cmake_da Args: preset_file: Preset file to modify cppython_preset_file: Path to the cppython preset file to include + cmake_data: The CMake data to use Returns: A CMakePresets object @@ -180,6 +181,7 @@ def write_root_presets(preset_file: Path, cppython_preset_file: Path, cmake_data Args: preset_file: Preset file to modify cppython_preset_file: Path to the cppython preset file to include + cmake_data: The CMake data to use """ initial_root_preset = None diff --git a/cppython/plugins/cmake/resolution.py b/cppython/plugins/cmake/resolution.py index aecad09..2711162 100644 --- a/cppython/plugins/cmake/resolution.py +++ b/cppython/plugins/cmake/resolution.py @@ -24,6 +24,4 @@ def resolve_cmake_data(data: dict[str, Any], core_data: CorePluginData) -> CMake if not modified_preset_file.is_absolute(): modified_preset_file = root_directory / modified_preset_file - - return CMakeData(preset_file=modified_preset_file, configuration_name=parsed_data.configuration_name) diff --git a/tests/unit/plugins/cmake/test_presets.py b/tests/unit/plugins/cmake/test_presets.py index 4d5852a..da0c2af 100644 --- a/tests/unit/plugins/cmake/test_presets.py +++ b/tests/unit/plugins/cmake/test_presets.py @@ -22,7 +22,7 @@ def test_generate_root_preset_new(tmp_path: Path) -> None: result = builder.generate_root_preset(preset_file, cppython_preset_file, cmake_data) assert result.configurePresets is not None assert any(p.name == 'test-configuration' for p in result.configurePresets) - + preset = next(p for p in result.configurePresets if p.name == 'test-configuration') assert preset.inherits == 'cppython'