Skip to content

Commit 7687557

Browse files
committed
config: Simplify write_file interface
1 parent d611c0c commit 7687557

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

src/mbed_tools/build/_internal/write_files.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from mbed_tools.build.exceptions import InvalidExportOutputDirectory
88

99

10-
def write_file(output_directory: pathlib.Path, file_name: str, file_contents: str) -> None:
11-
"""Writes out a file to a directory.
10+
def write_file(file_path: pathlib.Path, file_contents: str) -> None:
11+
"""Writes out a string to a file.
1212
1313
If the intermediate directories to the output directory don't exist,
1414
this function will create them.
@@ -19,9 +19,9 @@ def write_file(output_directory: pathlib.Path, file_name: str, file_contents: st
1919
Raises:
2020
InvalidExportOutputDirectory: it's not possible to export to the output directory provided
2121
"""
22+
output_directory = file_path.parent
2223
if output_directory.is_file():
2324
raise InvalidExportOutputDirectory("Output directory cannot be a path to a file.")
2425

2526
output_directory.mkdir(parents=True, exist_ok=True)
26-
output_file = output_directory.joinpath(file_name)
27-
output_file.write_text(file_contents)
27+
file_path.write_text(file_contents)

src/mbed_tools/build/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ def generate_config(target_name: str, toolchain: str, program: MbedProgram) -> p
3333
toolchain_name=toolchain,
3434
)
3535
cmake_config_file_path = program.files.cmake_config_file
36-
write_file(cmake_config_file_path.parent, cmake_config_file_path.name, cmake_file_contents)
36+
write_file(cmake_config_file_path, cmake_file_contents)
3737
return cmake_config_file_path

tests/build/_internal/test_write_files.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@ class TestWriteFile(TestCase):
1414
def test_writes_content_to_file(self):
1515
with tempfile.TemporaryDirectory() as directory:
1616
content = "Some rendered content"
17-
export_path = pathlib.Path(directory, "output")
18-
file_name = "some_file.txt"
17+
export_path = pathlib.Path(directory, "output", "some_file.txt")
1918

20-
write_file(export_path, file_name, content)
19+
write_file(export_path, content)
2120

22-
created_file = pathlib.Path(export_path, pathlib.Path(export_path, file_name))
21+
created_file = pathlib.Path(export_path)
2322
self.assertEqual(created_file.read_text(), content)
2423

2524
def test_output_dir_is_file(self):
2625
with tempfile.TemporaryDirectory() as directory:
27-
bad_export_dir = pathlib.Path(directory, "some_file.txt")
28-
bad_export_dir.touch()
26+
bad_export_dir = pathlib.Path(directory, "some_file.txt", ".txt")
27+
bad_export_dir.parent.touch()
2928
with self.assertRaises(InvalidExportOutputDirectory):
30-
write_file(bad_export_dir, "any_file.txt", "some contents")
29+
write_file(bad_export_dir, "some contents")

0 commit comments

Comments
 (0)