Skip to content

Commit ea9eea2

Browse files
committed
Test CMakePreset File Layout
1 parent 0be1ce5 commit ea9eea2

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

cppython/schema.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
from abc import abstractmethod
88
from dataclasses import dataclass
9-
from pathlib import PosixPath
9+
from pathlib import Path
1010
from typing import Any, Optional
1111

12-
from pydantic import BaseModel, Extra, Field
13-
from pydantic.types import FilePath
12+
from pydantic import BaseModel, Extra, Field, validator
1413

1514

1615
class Preset(BaseModel):
@@ -19,36 +18,40 @@ class Preset(BaseModel):
1918
"""
2019

2120
name: str
22-
hidden: Optional[bool] = None
21+
hidden: Optional[bool]
2322
inherits: list[str] = []
24-
displayName: Optional[str] = None
25-
description: Optional[str] = None
23+
displayName: Optional[str]
24+
description: Optional[str]
2625

2726

2827
class ConfigurePreset(Preset):
2928
"""
3029
Partial Configure Preset specification
3130
"""
3231

33-
toolchainFile: Optional[FilePath] = None
32+
toolchainFile: Optional[str]
33+
34+
@validator("toolchainFile")
35+
def validate_path(cls, v):
36+
return Path(v).as_posix()
3437

3538

3639
class BuildPreset(Preset):
3740
"""
3841
Partial Build Preset specification
3942
"""
4043

41-
configurePreset: Optional[str] = None
42-
inheritConfigureEnvironment: Optional[bool] = None
44+
configurePreset: Optional[str]
45+
inheritConfigureEnvironment: Optional[bool]
4346

4447

4548
class TestPreset(Preset):
4649
"""
4750
Partial Test Preset specification
4851
"""
4952

50-
configurePreset: Optional[str] = None
51-
inheritConfigureEnvironment: Optional[bool] = None
53+
configurePreset: Optional[str]
54+
inheritConfigureEnvironment: Optional[bool]
5255

5356

5457
class CMakeVersion(BaseModel, extra=Extra.forbid):
@@ -68,12 +71,19 @@ class CMakePresets(BaseModel, extra=Extra.forbid):
6871

6972
version: int = Field(default=4, const=True)
7073
cmakeMinimumRequired: CMakeVersion = CMakeVersion() # TODO: 'version' compatability validation
71-
include: list[FilePath] = []
72-
vendor: Optional[Any] = None
74+
include: list[str] = []
75+
vendor: Optional[Any]
7376
configurePresets: list[ConfigurePreset] = []
7477
buildPresets: list[BuildPreset] = []
7578
testPresets: list[TestPreset] = []
7679

80+
@validator("include")
81+
def validate_path(cls, v):
82+
output = []
83+
for value in v:
84+
output.append(Path(value).as_posix())
85+
return output
86+
7787

7888
@dataclass
7989
class ProjectConfiguration:

cppython/utility.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
import json
66
from pathlib import Path
77

8-
from pydantic import FilePath
9-
from pydantic.types import DirectoryPath
10-
118
from cppython.schema import CMakePresets, ConfigurePreset
129

1310

@@ -26,40 +23,44 @@ def write_preset(name: str, path: Path, presets: CMakePresets) -> Path:
2623
"""
2724
file = path / f"{name}.json"
2825

29-
serialized = json.loads(presets.json())
26+
serialized = json.loads(presets.json(exclude_none=True))
3027
with open(file, "w", encoding="utf8") as json_file:
3128
json.dump(serialized, json_file, ensure_ascii=False, indent=2)
3229

3330
return file
3431

3532

36-
def write_presets(tool_path: DirectoryPath, generator_output: list[tuple[str, FilePath]]) -> None:
33+
def write_presets(tool_path: Path, generator_output: list[tuple[str, Path]]) -> None:
3734
"""
3835
Write the cppython presets
3936
"""
4037

41-
def write_generator_presets(tool_path: DirectoryPath, generator_name: str, toolchain_path: FilePath) -> FilePath:
38+
def write_generator_presets(tool_path: Path, generator_name: str, toolchain_path: Path) -> Path:
4239
"""
4340
Write a generator preset.
4441
@returns - The written json file
4542
"""
4643
generator_tool_path = tool_path / generator_name
4744
generator_tool_path.mkdir(parents=True, exist_ok=True)
4845

49-
configure_preset = ConfigurePreset(name=generator_name, hidden=True, toolchainFile=toolchain_path)
46+
configure_preset = ConfigurePreset(name=generator_name, hidden=True, toolchainFile=str(toolchain_path))
5047
presets = CMakePresets(configurePresets=[configure_preset])
5148

5249
return write_preset(generator_name, generator_tool_path, presets)
5350

5451
names = []
5552
includes = []
5653

54+
tool_path = tool_path / "cppython"
55+
5756
for generator_name, toolchain in generator_output:
5857

5958
preset_file = write_generator_presets(tool_path, generator_name, toolchain)
6059

60+
relative_file = preset_file.relative_to(tool_path)
61+
6162
names.append(generator_name)
62-
includes.append(preset_file)
63+
includes.append(str(relative_file))
6364

6465
configure_preset = ConfigurePreset(name="cppython", hidden=True, inherits=names)
6566
presets = CMakePresets(configurePresets=[configure_preset], include=includes)

tests/unit/test_utility.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ class TestBuilder:
1212
TODO
1313
"""
1414

15-
def test_preset_read_write(self, tmpdir: Path):
15+
def test_preset_read_write(self, tmpdir):
1616
"""
1717
TODO
1818
"""
1919

20+
temporary_directory = Path(tmpdir)
21+
2022
presets = CMakePresets()
21-
write_preset("test", tmpdir, presets)
22-
output = read_preset("test", tmpdir)
23+
write_preset("test", temporary_directory, presets)
24+
output = read_preset("test", temporary_directory)
2325

2426
assert presets == output
2527

@@ -37,3 +39,15 @@ def test_presets(self, tmpdir):
3739

3840
generator_output = [("test", input_toolchain)]
3941
write_presets(temporary_directory, generator_output)
42+
43+
cppython_tool = temporary_directory / "cppython"
44+
assert cppython_tool.exists()
45+
46+
cppython_file = cppython_tool / "cppython.json"
47+
assert cppython_file.exists()
48+
49+
test_tool = cppython_tool / "test"
50+
assert test_tool.exists()
51+
52+
test_file = test_tool / "test.json"
53+
assert test_file.exists()

0 commit comments

Comments
 (0)