@@ -157,10 +157,8 @@ def write_markdown(path: Path, content: dict[str, str]) -> None:
157
157
to_print += "# Cleanup\n " + content ['cleanup' ] + '\n '
158
158
159
159
try :
160
- with open (path , 'w' , encoding = 'utf-8' ) as md_file :
161
- md_file .write (to_print )
162
- echo (style (
163
- f"Test case successfully stored into '{ path } '." , fg = 'magenta' ))
160
+ path .write_text (to_print , encoding = 'utf-8' )
161
+ echo (style (f"Test case successfully stored into '{ path } '." , fg = 'magenta' ))
164
162
except OSError :
165
163
raise ConvertError (f"Unable to write '{ path } '." )
166
164
@@ -269,22 +267,21 @@ def read_datafile(
269
267
# As 'make' command was specified for test, ensure Makefile present.
270
268
makefile_path = path / 'Makefile'
271
269
try :
272
- with open ( makefile_path , encoding = 'utf-8' ) as makefile_file :
273
- makefile = makefile_file . read ()
274
- search_result = \
275
- re . search ( makefile_regex_test , makefile , re .MULTILINE )
270
+ search_result = re . search (
271
+ makefile_regex_test ,
272
+ makefile_path . read_text ( encoding = 'utf-8' ),
273
+ re .MULTILINE )
276
274
except OSError :
277
275
raise ConvertError ("Makefile is missing." )
278
276
# Retrieve the path to the test file from the Makefile
279
277
if search_result is not None :
280
278
test_path = path / search_result .group (1 ).split ()[- 1 ]
281
279
# Read the test file and determine the framework used.
282
280
if test_path :
283
- with open (test_path , encoding = "utf-8" ) as test_file :
284
- if re .search ("beakerlib" , test_file .read (), re .MULTILINE ):
285
- data ["framework" ] = "beakerlib"
286
- else :
287
- data ["framework" ] = "shell"
281
+ if re .search ("beakerlib" , test_path .read_text (encoding = "utf-8" ), re .MULTILINE ):
282
+ data ["framework" ] = "beakerlib"
283
+ else :
284
+ data ["framework" ] = "shell"
288
285
else :
289
286
data ["framework" ] = "shell"
290
287
echo (style ("framework: " , fg = "green" ) + data ["framework" ])
@@ -452,8 +449,7 @@ def read(
452
449
assert filename is not None # type check
453
450
datafile_path = path / filename
454
451
try :
455
- with open (datafile_path , encoding = 'utf-8' ) as datafile_file :
456
- datafile = datafile_file .read ()
452
+ datafile = datafile_path .read_text (encoding = 'utf-8' )
457
453
except OSError :
458
454
raise ConvertError (f"Unable to open '{ datafile_path } '." )
459
455
echo (f"found in '{ datafile_path } '." )
@@ -462,8 +458,7 @@ def read(
462
458
testinfo_path = path / 'testinfo.desc'
463
459
if testinfo_path .is_file ():
464
460
try :
465
- with open (testinfo_path , encoding = 'utf-8' ) as testinfo_file :
466
- old_testinfo = testinfo_file .read ()
461
+ old_testinfo = testinfo_path .read_text (encoding = 'utf-8' )
467
462
testinfo_path .unlink ()
468
463
except OSError :
469
464
raise ConvertError (
@@ -495,8 +490,7 @@ def read(
495
490
496
491
# Read testinfo.desc
497
492
try :
498
- with open (testinfo_path , encoding = 'utf-8' ) as testinfo_file :
499
- testinfo = testinfo_file .read ()
493
+ testinfo = testinfo_path .read_text (encoding = 'utf-8' )
500
494
except OSError :
501
495
raise ConvertError (f"Unable to open '{ testinfo_path } '." )
502
496
@@ -561,8 +555,8 @@ def target_content_build() -> list[str]:
561
555
# Restore the original testinfo.desc content (if existed)
562
556
if old_testinfo :
563
557
try :
564
- with open ( testinfo_path , 'w' , encoding = 'utf-8' ) as testinfo_file :
565
- testinfo_file . write ( old_testinfo )
558
+ testinfo_path . write_text ( old_testinfo , encoding = 'utf-8' )
559
+
566
560
except OSError :
567
561
raise ConvertError (
568
562
f"Unable to write '{ testinfo_path } '." )
@@ -578,8 +572,7 @@ def target_content_build() -> list[str]:
578
572
echo (style ('Purpose ' , fg = 'blue' ), nl = False )
579
573
purpose_path = path / 'PURPOSE'
580
574
try :
581
- with open (purpose_path , encoding = 'utf-8' ) as purpose_file :
582
- content = purpose_file .read ()
575
+ content = purpose_path .read_text (encoding = 'utf-8' )
583
576
echo (f"found in '{ purpose_path } '." )
584
577
for header in ['PURPOSE' , 'Description' , 'Author' ]:
585
578
content = re .sub (f'^{ header } .*\n ' , '' , content )
@@ -1171,8 +1164,8 @@ def write(path: Path, data: NitrateDataType, quiet: bool = False) -> None:
1171
1164
1172
1165
# Store metadata into a fmf file
1173
1166
try :
1174
- with open ( path , 'w' , encoding = 'utf-8' ) as fmf_file :
1175
- fmf_file . write ( tmt . utils . dict_to_yaml ( sorted_data ))
1167
+ path . write_text ( tmt . utils . dict_to_yaml ( sorted_data ), encoding = 'utf-8' )
1168
+
1176
1169
except OSError :
1177
1170
raise ConvertError (f"Unable to write '{ path } '" )
1178
1171
if not quiet :
0 commit comments