Skip to content

Commit 6abcd6e

Browse files
committed
Moved JSON Read/Write To Utility + Test
1 parent 926ebff commit 6abcd6e

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

cppython/project.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from pydantic import create_model
2121

2222
from cppython.schema import API, CMakePresets, ConfigurePreset, ProjectConfiguration
23+
from cppython.utility import write_preset
2324

2425

2526
class ProjectBuilder:
@@ -187,14 +188,6 @@ def download(self):
187188
else:
188189
cppython_logger.info(f"The {generator.name()} generator is already downloaded")
189190

190-
def _write_presets_file(self, path: Path, presets: CMakePresets) -> None:
191-
"""
192-
Writing routing
193-
"""
194-
195-
with open(path / "CMakePresets.json", "w", encoding="utf8") as json_file:
196-
presets.json(json_file) # type: ignore
197-
198191
def _write_generator_presets(self, tool_path: Path, generator: Generator, toolchain_path: Path) -> Path:
199192
"""
200193
Write a generator preset.
@@ -206,7 +199,7 @@ def _write_generator_presets(self, tool_path: Path, generator: Generator, toolch
206199
configure_preset = ConfigurePreset(name=generator.name(), hidden=True, toolchainFile=toolchain_path)
207200
presets = CMakePresets(configurePresets=[configure_preset])
208201

209-
self._write_presets_file(generator_tool_path, presets)
202+
write_preset(generator_tool_path, presets)
210203

211204
return generator_tool_path
212205

@@ -217,7 +210,8 @@ def _write_presets(self, tool_path: Path, names: list[str], includes: list[Path]
217210

218211
configure_preset = ConfigurePreset(name="cppython", hidden=True, inherits=names)
219212
presets = CMakePresets(configurePresets=[configure_preset], include=includes)
220-
self._write_presets_file(tool_path, presets)
213+
214+
write_preset(tool_path, presets)
221215

222216
# API Contract
223217
def install(self) -> None:

cppython/utility.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
TODO
3+
"""
4+
5+
import json
6+
from pathlib import Path
7+
8+
from cppython.schema import CMakePresets
9+
10+
11+
def read_preset(path: Path) -> CMakePresets:
12+
"""
13+
Reading routing
14+
"""
15+
16+
preset_path = path / "CMakePresets.json"
17+
return CMakePresets.parse_file(path=preset_path)
18+
19+
20+
def write_preset(path: Path, presets: CMakePresets) -> None:
21+
"""
22+
Writing routine
23+
"""
24+
with open(path / "CMakePresets.json", "w", encoding="utf8") as json_file:
25+
json.dump(presets.dict(), json_file, ensure_ascii=False, indent=2)

tests/unit/test_utility.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
TODO
3+
"""
4+
from pathlib import Path
5+
6+
from cppython.schema import CMakePresets
7+
from cppython.utility import read_preset, write_preset
8+
9+
10+
class TestBuilder:
11+
"""
12+
TODO
13+
"""
14+
15+
def test_preset_read_write(self, tmpdir: Path):
16+
"""
17+
TODO
18+
"""
19+
20+
presets = CMakePresets()
21+
write_preset(tmpdir, presets)
22+
output = read_preset(tmpdir)
23+
24+
assert presets == output

0 commit comments

Comments
 (0)