33# SPDX-License-Identifier: Apache-2.0
44#
55import pathlib
6- from pyfakefs .fake_filesystem_unittest import Patcher
76from unittest import TestCase , mock
87from datetime import datetime
9-
8+ from tempfile import TemporaryDirectory
109from mbed_tools_ci_scripts .utils .configuration import configuration , ConfigurationVariable
1110from 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
4746class 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