Skip to content

Commit b489ea0

Browse files
authored
Add newline to news files created with create-news-file (#57)
* Add newline to news files created with create-news-file * Update create-news-file tests * Add test for skipping adding newline * Optimise tests by removing pyfakefs
1 parent eef162a commit b489ea0

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

mbed_tools_ci_scripts/create_news_file.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def determine_news_file_path(news_type: NewsType) -> pathlib.Path:
5555

5656
def _write_file(file_path: pathlib.Path, text: str) -> None:
5757
file_path.parent.mkdir(parents=True, exist_ok=True)
58+
if not text.endswith("\n"):
59+
text = f"{text}\n"
5860
file_path.write_text(text)
5961

6062

news/20200707.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add newline to news file created with create-news-file

tests/test_create_news_file.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
# SPDX-License-Identifier: Apache-2.0
44
#
55
import pathlib
6-
from pyfakefs.fake_filesystem_unittest import Patcher
76
from unittest import TestCase, mock
87
from datetime import datetime
9-
8+
from tempfile import TemporaryDirectory
109
from mbed_tools_ci_scripts.utils.configuration import configuration, ConfigurationVariable
1110
from mbed_tools_ci_scripts.create_news_file import NewsType, create_news_file, determine_news_file_path, _write_file
1211

@@ -35,9 +34,9 @@ def test_finds_first_available_file_path_in_news_dir(self):
3534

3635
for news_type in NewsType:
3736
with self.subTest(f"It determines available file path for {news_type}."):
38-
with Patcher() as patcher:
39-
patcher.fs.create_file(f"{news_file_path_today}.{news_type.name}")
40-
patcher.fs.create_file(f"{news_file_path_today}01.{news_type.name}")
37+
with TemporaryDirectory() as tmp_dir:
38+
pathlib.Path(tmp_dir, f"{news_file_path_today}.{news_type.name}").touch()
39+
pathlib.Path(tmp_dir, f"{news_file_path_today}01.{news_type.name}").touch()
4140

4241
file_path = determine_news_file_path(news_type)
4342

@@ -46,10 +45,20 @@ def test_finds_first_available_file_path_in_news_dir(self):
4645

4746
class TestWriteFile(TestCase):
4847
def test_writes_files_in_nested_directories(self):
49-
with Patcher():
50-
file_path = "/some/directory/file.txt"
51-
path = pathlib.Path(file_path)
48+
with TemporaryDirectory() as tmp_dir:
49+
dir_path = pathlib.Path(tmp_dir, "some", "dir")
50+
dir_path.mkdir(parents=True)
51+
file_path = dir_path / "file.txt"
52+
file_path.touch()
5253
contents = "woohoo"
53-
_write_file(path, contents)
54+
_write_file(file_path, contents)
55+
56+
self.assertEqual(file_path.read_text(), f"{contents}\n")
57+
58+
def test_skips_adding_newline_if_already_exists(self):
59+
with TemporaryDirectory() as tmp_dir:
60+
file_path = pathlib.Path(tmp_dir, "file.txt")
61+
contents = "woohoo\n"
62+
_write_file(file_path, contents)
5463

55-
self.assertEqual(path.read_text(), contents)
64+
self.assertEqual(file_path.read_text(), contents)

0 commit comments

Comments
 (0)