Skip to content

Test and Fix CMake's CacheVariable Schema #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cppython/plugins/cmake/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions cppython/plugins/cmake/resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 10 additions & 10 deletions cppython/plugins/cmake/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'):
Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/cmake/test_presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
51 changes: 51 additions & 0 deletions tests/unit/plugins/cmake/test_schema.py
Original file line number Diff line number Diff line change
@@ -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'
Loading