Skip to content

Commit 618f451

Browse files
authored
Test and Fix CMake's CacheVariable Schema (#114)
1 parent 410822d commit 618f451

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

cppython/plugins/cmake/builder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def generate_root_preset(preset_file: Path, cppython_preset_file: Path, cmake_da
120120
Args:
121121
preset_file: Preset file to modify
122122
cppython_preset_file: Path to the cppython preset file to include
123+
cmake_data: The CMake data to use
123124
124125
Returns:
125126
A CMakePresets object
@@ -180,6 +181,7 @@ def write_root_presets(preset_file: Path, cppython_preset_file: Path, cmake_data
180181
Args:
181182
preset_file: Preset file to modify
182183
cppython_preset_file: Path to the cppython preset file to include
184+
cmake_data: The CMake data to use
183185
"""
184186
initial_root_preset = None
185187

cppython/plugins/cmake/resolution.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,4 @@ def resolve_cmake_data(data: dict[str, Any], core_data: CorePluginData) -> CMake
2424
if not modified_preset_file.is_absolute():
2525
modified_preset_file = root_directory / modified_preset_file
2626

27-
28-
2927
return CMakeData(preset_file=modified_preset_file, configuration_name=parsed_data.configuration_name)

cppython/plugins/cmake/schema.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
configuration presets, and synchronization data.
66
"""
77

8-
from enum import Enum, auto
8+
from enum import StrEnum
99
from pathlib import Path
1010
from typing import Annotated
1111

@@ -15,20 +15,20 @@
1515
from cppython.core.schema import CPPythonModel, SyncData
1616

1717

18-
class VariableType(Enum):
18+
class VariableType(StrEnum):
1919
"""Defines the types of variables that can be used in CMake cache.
2020
2121
Args:
2222
Enum: Base class for creating enumerations.
2323
"""
2424

25-
BOOL = (auto(),) # Boolean ON/OFF value.
26-
PATH = (auto(),) # Path to a directory.
27-
FILEPATH = (auto(),) # Path to a file.
28-
STRING = (auto(),) # Generic string value.
29-
INTERNAL = (auto(),) # Do not present in GUI at all.
30-
STATIC = (auto(),) # Value managed by CMake, do not change.
31-
UNINITIALIZED = auto() # Type not yet specified.
25+
BOOL = 'BOOL'
26+
PATH = 'PATH'
27+
FILEPATH = 'FILEPATH'
28+
STRING = 'STRING'
29+
INTERNAL = 'INTERNAL'
30+
STATIC = 'STATIC'
31+
UNINITIALIZED = 'UNINITIALIZED'
3232

3333

3434
class CacheVariable(CPPythonModel, extra='forbid'):
@@ -39,7 +39,7 @@ class CacheVariable(CPPythonModel, extra='forbid'):
3939
value: The value of the variable, which can be a boolean or string.
4040
"""
4141

42-
type: None | VariableType
42+
type: None | VariableType = None
4343
value: bool | str
4444

4545

tests/unit/plugins/cmake/test_presets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_generate_root_preset_new(tmp_path: Path) -> None:
2222
result = builder.generate_root_preset(preset_file, cppython_preset_file, cmake_data)
2323
assert result.configurePresets is not None
2424
assert any(p.name == 'test-configuration' for p in result.configurePresets)
25-
25+
2626
preset = next(p for p in result.configurePresets if p.name == 'test-configuration')
2727
assert preset.inherits == 'cppython'
2828

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Tests for the CMake schema"""
2+
3+
from cppython.plugins.cmake.schema import CacheVariable, VariableType
4+
5+
6+
class TestCacheVariable:
7+
"""Tests for the CacheVariable class"""
8+
9+
@staticmethod
10+
def test_cache_variable_bool() -> None:
11+
"""Tests the CacheVariable class with a boolean value"""
12+
var = CacheVariable(type=VariableType.BOOL, value=True)
13+
assert var.type == VariableType.BOOL
14+
assert var.value is True
15+
16+
@staticmethod
17+
def test_cache_variable_string() -> None:
18+
"""Tests the CacheVariable class with a string value"""
19+
var = CacheVariable(type=VariableType.STRING, value='SomeValue')
20+
assert var.type == VariableType.STRING
21+
assert var.value == 'SomeValue'
22+
23+
@staticmethod
24+
def test_cache_variable_null_type() -> None:
25+
"""Tests the CacheVariable class with a null type"""
26+
var = CacheVariable(type=None, value='Unset')
27+
assert var.type is None
28+
assert var.value == 'Unset'
29+
30+
@staticmethod
31+
def test_cache_variable_type_enum_values() -> None:
32+
"""Tests the CacheVariable class with enum values"""
33+
# Ensure all CMake types are present
34+
expected = {'BOOL', 'PATH', 'FILEPATH', 'STRING', 'INTERNAL', 'STATIC', 'UNINITIALIZED'}
35+
actual = {v.value for v in VariableType}
36+
assert expected == actual
37+
38+
@staticmethod
39+
def test_cache_variable_bool_value_as_string() -> None:
40+
"""Tests the CacheVariable class with a boolean value as a string"""
41+
# CMake allows bool as "TRUE"/"FALSE" as well
42+
var = CacheVariable(type=VariableType.BOOL, value='TRUE')
43+
assert var.value == 'TRUE'
44+
45+
@staticmethod
46+
def test_cache_variable_type_optional() -> None:
47+
"""Tests the CacheVariable class with an optional type"""
48+
# type is optional
49+
var = CacheVariable(value='SomeValue')
50+
assert var.type is None
51+
assert var.value == 'SomeValue'

0 commit comments

Comments
 (0)