@@ -115,6 +115,14 @@ def test_file(*path_components):
115115 return str (Path (TEST_ROOT , * path_components ))
116116
117117
118+ def read_file (* path_components ):
119+ return Path (* path_components ).read_text ()
120+
121+
122+ def read_binary (* path_components ):
123+ return Path (* path_components ).read_bytes ()
124+
125+
118126# checks if browser testing is enabled
119127def has_browser ():
120128 return EMTEST_BROWSER != '0'
@@ -609,7 +617,7 @@ def build(self, filename, libraries=[], includes=[], force_c=False,
609617 post_build (output )
610618
611619 if js_outfile and self .uses_memory_init_file ():
612- src = open (output ). read ( )
620+ src = read_file (output )
613621 # side memory init file, or an empty one in the js
614622 assert ('/* memory initializer */' not in src ) or ('/* memory initializer */ allocate([]' in src )
615623
@@ -636,8 +644,8 @@ def count_funcs(self, javascript_file):
636644 start_off = 0
637645 end_off = 0
638646
639- with open (javascript_file , 'rt' ) as f :
640- blob = "" .join (f . readlines ())
647+ js = read_file (javascript_file )
648+ blob = "" .join (js . splitlines ())
641649
642650 start_off = blob .find (start_tok ) + len (start_tok )
643651 end_off = blob .find (end_tok )
@@ -687,8 +695,8 @@ def run_js(self, filename, engine=None, args=[], output_nicerizer=None, assert_r
687695 if not filename .endswith ('.wasm' ):
688696 self .assertEqual (line_endings .check_line_endings (filename ), 0 )
689697
690- out = open (stdout , 'r' ). read ( )
691- err = open (stderr , 'r' ). read ( )
698+ out = read_file (stdout )
699+ err = read_file (stderr )
692700 if output_nicerizer :
693701 ret = output_nicerizer (out , err )
694702 else :
@@ -790,8 +798,8 @@ def assertContainedIf(self, value, string, condition):
790798 def assertBinaryEqual (self , file1 , file2 ):
791799 self .assertEqual (os .path .getsize (file1 ),
792800 os .path .getsize (file2 ))
793- self .assertEqual (open (file1 , 'rb' ). read ( ),
794- open (file2 , 'rb' ). read ( ))
801+ self .assertEqual (read_binary (file1 ),
802+ read_binary (file2 ))
795803
796804 library_cache = {}
797805
@@ -1028,12 +1036,12 @@ def do_runf(self, filename, expected_output=None, **kwargs):
10281036
10291037 ## Just like `do_run` but with filename of expected output
10301038 def do_run_from_file (self , filename , expected_output_filename , ** kwargs ):
1031- self ._build_and_run (filename , open (expected_output_filename ). read ( ), ** kwargs )
1039+ self ._build_and_run (filename , read_file (expected_output_filename ), ** kwargs )
10321040
10331041 def do_run_in_out_file_test (self , * path , ** kwargs ):
10341042 srcfile = test_file (* path )
10351043 outfile = shared .unsuffixed (srcfile ) + '.out'
1036- expected = open (outfile ). read ( )
1044+ expected = read_file (outfile )
10371045 self ._build_and_run (srcfile , expected , ** kwargs )
10381046
10391047 ## Does a complete test - builds, runs, checks output, etc.
@@ -1180,7 +1188,7 @@ def do_GET(self):
11801188 self .send_response (200 )
11811189 self .send_header ('Content-type' , 'text/html' )
11821190 self .end_headers ()
1183- self .wfile .write (open (test_file ('browser_harness.html' ), 'rb' ). read ( ))
1191+ self .wfile .write (read_binary (test_file ('browser_harness.html' )))
11841192 elif 'report_' in self .path :
11851193 # the test is reporting its result. first change dir away from the
11861194 # test dir, as it will be deleted now that the test is finishing, and
@@ -1412,9 +1420,9 @@ def reftest(self, expected, manually_trigger=False):
14121420 # pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB infile outfile
14131421 basename = os .path .basename (expected )
14141422 shutil .copyfile (expected , os .path .join (self .get_dir (), basename ))
1423+ reporting = read_file (test_file ('browser_reporting.js' ))
14151424 with open ('reftest.js' , 'w' ) as out :
1416- with open (test_file ('browser_reporting.js' )) as reporting :
1417- out .write ('''
1425+ out .write ('''
14181426 function doReftest() {
14191427 if (doReftest.done) return;
14201428 doReftest.done = true;
@@ -1510,7 +1518,7 @@ def reftest(self, expected, manually_trigger=False):
15101518 setTimeout(realDoReftest, 1);
15111519 };
15121520 }
1513- ''' % (reporting . read () , basename , int (manually_trigger )))
1521+ ''' % (reporting , basename , int (manually_trigger )))
15141522
15151523 def compile_btest (self , args , reporting = Reporting .FULL ):
15161524 # Inject support code for reporting results. This adds an include a header so testcases can
@@ -1642,14 +1650,12 @@ def build_library(name,
16421650 shared .run_process (configure , env = env , stdout = stdout , stderr = stderr ,
16431651 cwd = project_dir )
16441652 except subprocess .CalledProcessError :
1645- with open (os .path .join (project_dir , 'configure_out' )) as f :
1646- print ('-- configure stdout --' )
1647- print (f .read ())
1648- print ('-- end configure stdout --' )
1649- with open (os .path .join (project_dir , 'configure_err' )) as f :
1650- print ('-- configure stderr --' )
1651- print (f .read ())
1652- print ('-- end configure stderr --' )
1653+ print ('-- configure stdout --' )
1654+ print (read_file (Path (project_dir , 'configure_out' )))
1655+ print ('-- end configure stdout --' )
1656+ print ('-- configure stderr --' )
1657+ print (read_file (Path (project_dir , 'configure_err' )))
1658+ print ('-- end configure stderr --' )
16531659 raise
16541660
16551661 def open_make_out (mode = 'r' ):
@@ -1683,7 +1689,7 @@ def open_make_err(mode='r'):
16831689 cache [cache_name ] = []
16841690 for f in generated_libs :
16851691 basename = os .path .basename (f )
1686- cache [cache_name ].append ((basename , open ( f , 'rb' ). read ( )))
1692+ cache [cache_name ].append ((basename , read_binary ( f )))
16871693
16881694 return generated_libs
16891695
0 commit comments