Skip to content

Commit 8baa797

Browse files
committed
Add BinaryDir Stubbing
1 parent 7274239 commit 8baa797

File tree

5 files changed

+52
-23
lines changed

5 files changed

+52
-23
lines changed

cppython/plugins/cmake/builder.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,24 @@ def write_cppython_preset(
114114
return cppython_preset_file
115115

116116
@staticmethod
117-
def generate_root_preset(preset_file: Path, cppython_preset_file: Path, cmake_data: CMakeData) -> CMakePresets:
117+
def generate_root_preset(
118+
preset_file: Path, cppython_preset_file: Path, cmake_data: CMakeData, build_directory: Path
119+
) -> CMakePresets:
118120
"""Generates the top level root preset with the include reference.
119121
120122
Args:
121123
preset_file: Preset file to modify
122124
cppython_preset_file: Path to the cppython preset file to include
123125
cmake_data: The CMake data to use
126+
build_directory: The build directory to use
124127
125128
Returns:
126129
A CMakePresets object
127130
"""
128131
default_configure_preset = ConfigurePreset(
129132
name=cmake_data.configuration_name,
130133
inherits='cppython',
134+
binaryDir=build_directory.as_posix(),
131135
)
132136

133137
if preset_file.exists():
@@ -170,7 +174,9 @@ def generate_root_preset(preset_file: Path, cppython_preset_file: Path, cmake_da
170174
return root_preset
171175

172176
@staticmethod
173-
def write_root_presets(preset_file: Path, cppython_preset_file: Path, cmake_data: CMakeData) -> None:
177+
def write_root_presets(
178+
preset_file: Path, cppython_preset_file: Path, cmake_data: CMakeData, build_directory: Path
179+
) -> None:
174180
"""Read the top level json file and insert the include reference.
175181
176182
Receives a relative path to the tool cmake json file
@@ -182,6 +188,7 @@ def write_root_presets(preset_file: Path, cppython_preset_file: Path, cmake_data
182188
preset_file: Preset file to modify
183189
cppython_preset_file: Path to the cppython preset file to include
184190
cmake_data: The CMake data to use
191+
build_directory: The build directory to use
185192
"""
186193
initial_root_preset = None
187194

@@ -190,7 +197,7 @@ def write_root_presets(preset_file: Path, cppython_preset_file: Path, cmake_data
190197
initial_json = file.read()
191198
initial_root_preset = CMakePresets.model_validate_json(initial_json)
192199

193-
root_preset = Builder.generate_root_preset(preset_file, cppython_preset_file, cmake_data)
200+
root_preset = Builder.generate_root_preset(preset_file, cppython_preset_file, cmake_data, build_directory)
194201

195202
# Only write the file if the data has changed
196203
if root_preset != initial_root_preset:

cppython/plugins/cmake/plugin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def sync(self, sync_data: SyncData) -> None:
7171
self._cppython_preset_directory, self._provider_directory, sync_data
7272
)
7373

74-
self.builder.write_root_presets(self.data.preset_file, cppython_preset_file, self.data)
74+
self.builder.write_root_presets(
75+
self.data.preset_file, cppython_preset_file, self.data, self.core_data.cppython_data.build_path
76+
)
7577
case _:
7678
raise ValueError('Unsupported sync data type')

cppython/plugins/cmake/schema.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class ConfigurePreset(CPPythonModel, extra='allow'):
5454
inherits: Annotated[
5555
str | list[str] | None, Field(description='The inherits field allows inheriting from other presets.')
5656
] = None
57+
binaryDir: Annotated[
58+
str | None,
59+
Field(description='The path to the output binary directory.'),
60+
] = None
5761
cacheVariables: dict[str, None | bool | str | CacheVariable] | None = None
5862

5963

tests/integration/examples/test_conan_cmake.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ def test_simple(example_runner: CliRunner) -> None:
2828

2929
assert result.returncode == 0, f'Cmake failed: {result.stderr}'
3030

31+
path = Path('build').absolute()
32+
3133
# Verify that the build directory contains the expected files
32-
assert (Path('build') / 'CMakeCache.txt').exists(), 'build/CMakeCache.txt not found'
34+
assert (path / 'CMakeCache.txt').exists(), f'{path / "CMakeCache.txt"} not found'

tests/unit/plugins/cmake/test_presets.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pathlib import Path
44

5+
from cppython.core.schema import ProjectData
56
from cppython.plugins.cmake.builder import Builder
67
from cppython.plugins.cmake.schema import CMakeData, CMakePresets, CMakeSyncData
78
from cppython.utility.utility import TypeName
@@ -11,36 +12,40 @@ class TestBuilder:
1112
"""Tests for the CMakePresets class"""
1213

1314
@staticmethod
14-
def test_generate_root_preset_new(tmp_path: Path) -> None:
15+
def test_generate_root_preset_new(project_data: ProjectData) -> None:
1516
"""Test generate_root_preset when the preset file does not exist"""
1617
builder = Builder()
17-
preset_file = tmp_path / 'CMakePresets.json'
18-
cppython_preset_file = tmp_path / 'cppython.json'
18+
preset_file = project_data.project_root / 'CMakePresets.json'
19+
cppython_preset_file = project_data.project_root / 'cppython.json'
1920
cmake_data = CMakeData(preset_file=preset_file, configuration_name='test-configuration')
2021

22+
build_directory = project_data.project_root / 'build'
23+
2124
# The function should create a new preset with the correct name and inheritance
22-
result = builder.generate_root_preset(preset_file, cppython_preset_file, cmake_data)
25+
result = builder.generate_root_preset(preset_file, cppython_preset_file, cmake_data, build_directory)
2326
assert result.configurePresets is not None
2427
assert any(p.name == 'test-configuration' for p in result.configurePresets)
2528

2629
preset = next(p for p in result.configurePresets if p.name == 'test-configuration')
2730
assert preset.inherits == 'cppython'
2831

2932
@staticmethod
30-
def test_generate_root_preset_existing(tmp_path: Path) -> None:
33+
def test_generate_root_preset_existing(project_data: ProjectData) -> None:
3134
"""Test generate_root_preset when the preset file already exists"""
3235
builder = Builder()
33-
preset_file = tmp_path / 'CMakePresets.json'
34-
cppython_preset_file = tmp_path / 'cppython.json'
36+
preset_file = project_data.project_root / 'CMakePresets.json'
37+
cppython_preset_file = project_data.project_root / 'cppython.json'
3538
cmake_data = CMakeData(preset_file=preset_file, configuration_name='test-configuration')
3639

3740
# Create an initial preset file with a different preset
3841
initial_presets = CMakePresets(configurePresets=[])
3942
with open(preset_file, 'w', encoding='utf-8') as f:
4043
f.write(initial_presets.model_dump_json(exclude_none=True, by_alias=False, indent=4))
4144

45+
build_directory = project_data.project_root / 'build'
46+
4247
# Should add the new preset and include
43-
result = builder.generate_root_preset(preset_file, cppython_preset_file, cmake_data)
48+
result = builder.generate_root_preset(preset_file, cppython_preset_file, cmake_data, build_directory)
4449
assert result.configurePresets is not None
4550
assert any(p.name == 'test-configuration' for p in result.configurePresets)
4651

@@ -86,15 +91,15 @@ def test_cppython_write(tmp_path: Path) -> None:
8691
builder.write_cppython_preset(tmp_path, provider_directory, data)
8792

8893
@staticmethod
89-
def test_root_write(tmp_path: Path) -> None:
94+
def test_root_write(project_data: ProjectData) -> None:
9095
"""Verifies that the root preset writing works as intended
9196
9297
Args:
93-
tmp_path: The input path the use
98+
project_data: The project data with a temporary workspace
9499
"""
95100
builder = Builder()
96101

97-
cppython_preset_directory = tmp_path / 'cppython'
102+
cppython_preset_directory = project_data.project_root / 'cppython'
98103
cppython_preset_directory.mkdir(parents=True, exist_ok=True)
99104

100105
provider_directory = cppython_preset_directory / 'providers'
@@ -104,7 +109,7 @@ def test_root_write(tmp_path: Path) -> None:
104109
with includes_file.open('w', encoding='utf-8') as file:
105110
file.write('example contents')
106111

107-
root_file = tmp_path / 'CMakePresets.json'
112+
root_file = project_data.project_root / 'CMakePresets.json'
108113
presets = CMakePresets()
109114

110115
serialized = presets.model_dump_json(exclude_none=True, by_alias=False, indent=4)
@@ -116,20 +121,24 @@ def test_root_write(tmp_path: Path) -> None:
116121

117122
cppython_preset_file = builder.write_cppython_preset(cppython_preset_directory, provider_directory, data)
118123

124+
build_directory = project_data.project_root / 'build'
119125
builder.write_root_presets(
120-
root_file, cppython_preset_file, CMakeData(preset_file=root_file, configuration_name='default')
126+
root_file,
127+
cppython_preset_file,
128+
CMakeData(preset_file=root_file, configuration_name='default'),
129+
build_directory,
121130
)
122131

123132
@staticmethod
124-
def test_relative_root_write(tmp_path: Path) -> None:
133+
def test_relative_root_write(project_data: ProjectData) -> None:
125134
"""Verifies that the root preset writing works as intended
126135
127136
Args:
128-
tmp_path: The input path the use
137+
project_data: The project data with a temporary workspace
129138
"""
130139
builder = Builder()
131140

132-
cppython_preset_directory = tmp_path / 'tool' / 'cppython'
141+
cppython_preset_directory = project_data.project_root / 'tool' / 'cppython'
133142
cppython_preset_directory.mkdir(parents=True, exist_ok=True)
134143

135144
provider_directory = cppython_preset_directory / 'providers'
@@ -139,7 +148,7 @@ def test_relative_root_write(tmp_path: Path) -> None:
139148
with includes_file.open('w', encoding='utf-8') as file:
140149
file.write('example contents')
141150

142-
relative_indirection = tmp_path / 'nested'
151+
relative_indirection = project_data.project_root / 'nested'
143152
relative_indirection.mkdir(parents=True, exist_ok=True)
144153

145154
root_file = relative_indirection / 'CMakePresets.json'
@@ -152,6 +161,11 @@ def test_relative_root_write(tmp_path: Path) -> None:
152161
builder.write_provider_preset(provider_directory, data)
153162

154163
cppython_preset_file = builder.write_cppython_preset(cppython_preset_directory, provider_directory, data)
164+
165+
build_directory = project_data.project_root / 'build'
155166
builder.write_root_presets(
156-
root_file, cppython_preset_file, CMakeData(preset_file=root_file, configuration_name='default')
167+
root_file,
168+
cppython_preset_file,
169+
CMakeData(preset_file=root_file, configuration_name='default'),
170+
build_directory,
157171
)

0 commit comments

Comments
 (0)