99import tempfile
1010from os .path import exists , join
1111from pathlib import Path
12+ from typing import Union
1213from unittest import main
1314
1415from lxml .html import fromstring
1920from tests .sound_swallower_stub import SoundSwallowerStub
2021
2122
22- def write_file (filename : str , file_contents : str ) -> str :
23+ def write_file (filename : Union [ str , Path ] , file_contents : str ) -> str :
2324 """Write file_contents to file filename, and return its name (filename)"""
2425 with open (filename , mode = "w" , encoding = "utf8" ) as f :
2526 f .write (file_contents )
26- return filename
27+ return str ( filename )
2728
2829
2930class TestAlignCli (BasicTestCase ):
@@ -245,7 +246,7 @@ def test_align_english(self):
245246 """Validates that the lexicon-based g2p works for English language alignment"""
246247
247248 input_filename = write_file (
248- join ( self .tempdir , "input" ) ,
249+ self .tempdir / "input" ,
249250 "This is some text that we will run through the English lexicon "
250251 "grapheme to morpheme approach." ,
251252 )
@@ -405,7 +406,7 @@ def test_infer_plain_text_or_xml(self):
405406 """align -i is obsolete, now we infer plain text vs XML; test that!"""
406407
407408 # plain text with guess by contents
408- infile1 = write_file (join ( self .tempdir , "infile1" ) , "some plain text" )
409+ infile1 = write_file (self .tempdir / "infile1" , "some plain text" )
409410 with SoundSwallowerStub ("word:0:1" ):
410411 results = self .runner .invoke (
411412 align ,
@@ -420,7 +421,7 @@ def test_infer_plain_text_or_xml(self):
420421 self .assertIn ("No input language specified for plain text" , results .output )
421422
422423 # plain text by extension
423- infile2 = write_file (join ( self .tempdir , "infile2.txt" ) , "<?xml but .txt" )
424+ infile2 = write_file (self .tempdir / "infile2.txt" , "<?xml but .txt" )
424425 with SoundSwallowerStub ("word:0:1" ):
425426 results = self .runner .invoke (
426427 align ,
@@ -437,7 +438,7 @@ def test_infer_plain_text_or_xml(self):
437438 # XML with guess by contents
438439 infile3 = self .add_bom (
439440 write_file (
440- join ( self .tempdir , "infile3" ) ,
441+ self .tempdir / "infile3" ,
441442 "<?xml version='1.0' encoding='utf-8'?><text>blah blah</text>" ,
442443 )
443444 )
@@ -454,7 +455,7 @@ def test_infer_plain_text_or_xml(self):
454455
455456 # XML with guess by contents, but with content error
456457 infile4 = write_file (
457- join ( self .tempdir , "infile4" ) ,
458+ self .tempdir / "infile4" ,
458459 "<?xml version='1.0' encoding='utf-8'?><text>blah blah</bad_tag>" ,
459460 )
460461 with SoundSwallowerStub ("word:0:1" ):
@@ -470,7 +471,7 @@ def test_infer_plain_text_or_xml(self):
470471 self .assertIn ("Error parsing XML" , results .output )
471472
472473 # XML by file extension
473- infile5 = write_file (join ( self .tempdir , "infile5.readalong" ) , "Not XML!" )
474+ infile5 = write_file (self .tempdir / "infile5.readalong" , "Not XML!" )
474475 with SoundSwallowerStub ("word:0:1" ):
475476 results = self .runner .invoke (
476477 align ,
@@ -617,9 +618,7 @@ def slurp_text(filename, encoding):
617618 with open (filename , "r" , encoding = encoding ) as f :
618619 return f .read ()
619620
620- base_file = write_file (
621- str (self .tempdir / "add-bom-input.txt" ), "Random Text été"
622- )
621+ base_file = write_file (self .tempdir / "add-bom-input.txt" , "Random Text été" )
623622 bom_file = self .add_bom (base_file )
624623 self .assertEqual (
625624 slurp_text (base_file , "utf-8" ), slurp_text (bom_file , "utf-8-sig" )
0 commit comments