@@ -214,8 +214,7 @@ def test_zzz_html_source_map(self):
214
214
# multiple mapped lines. in other words, if the program consists of a
215
215
# single 'throw' statement, browsers may just map any thrown exception to
216
216
# that line, because it will be the only mapped line.
217
- with open (cpp_file , 'w' ) as f :
218
- f .write (r'''
217
+ create_file (cpp_file , r'''
219
218
#include <cstdio>
220
219
221
220
int main() {
@@ -1380,18 +1379,18 @@ def test_fs_workerfs_read(self):
1380
1379
def test_fs_workerfs_package (self ):
1381
1380
create_file ('file1.txt' , 'first' )
1382
1381
ensure_dir ('sub' )
1383
- open ( Path ( 'sub/file2.txt' ), 'w' ). write ( 'second' )
1382
+ create_file ( 'sub/file2.txt' , 'second' )
1384
1383
self .run_process ([FILE_PACKAGER , 'files.data' , '--preload' , 'file1.txt' , Path ('sub/file2.txt' ), '--separate-metadata' , '--js-output=files.js' ])
1385
1384
self .btest (Path ('fs/test_workerfs_package.cpp' ), '1' , args = ['-lworkerfs.js' , '--proxy-to-worker' , '-lworkerfs.js' ])
1386
1385
1387
1386
def test_fs_lz4fs_package (self ):
1388
1387
# generate data
1389
1388
ensure_dir ('subdir' )
1390
1389
create_file ('file1.txt' , '0123456789' * (1024 * 128 ))
1391
- open ( Path ( 'subdir/file2.txt' ), 'w' ). write ( '1234567890' * (1024 * 128 ))
1390
+ create_file ( 'subdir/file2.txt' , '1234567890' * (1024 * 128 ))
1392
1391
random_data = bytearray (random .randint (0 , 255 ) for x in range (1024 * 128 * 10 + 1 ))
1393
1392
random_data [17 ] = ord ('X' )
1394
- open ('file3.txt' , 'wb' ). write ( random_data )
1393
+ create_file ('file3.txt' , random_data , binary = True )
1395
1394
1396
1395
# compress in emcc, -sLZ4 tells it to tell the file packager
1397
1396
print ('emcc-normal' )
@@ -1404,7 +1403,7 @@ def test_fs_lz4fs_package(self):
1404
1403
# compress in the file packager, on the server. the client receives compressed data and can just use it. this is typical usage
1405
1404
print ('normal' )
1406
1405
out = subprocess .check_output ([FILE_PACKAGER , 'files.data' , '--preload' , 'file1.txt' , 'subdir/file2.txt' , 'file3.txt' , '--lz4' ])
1407
- open ('files.js' , 'wb' ). write ( out )
1406
+ create_file ('files.js' , out , binary = True )
1408
1407
self .btest (Path ('fs/test_lz4fs.cpp' ), '2' , args = ['--pre-js' , 'files.js' , '-sLZ4=1' , '-sFORCE_FILESYSTEM' ])
1409
1408
print (' opts' )
1410
1409
self .btest (Path ('fs/test_lz4fs.cpp' ), '2' , args = ['--pre-js' , 'files.js' , '-sLZ4=1' , '-sFORCE_FILESYSTEM' , '-O2' ])
@@ -1436,7 +1435,7 @@ def test_fs_lz4fs_package(self):
1436
1435
shutil.copyfile('file2.txt', Path('files/file2.txt'))
1437
1436
shutil.copyfile('file3.txt', Path('files/file3.txt'))
1438
1437
out = subprocess.check_output([FILE_PACKAGER, 'files.data', '--preload', 'files/file1.txt', 'files/file2.txt', 'files/file3.txt'])
1439
- open ('files.js', 'wb').write( out)
1438
+ create_file ('files.js', out, binary=True )
1440
1439
self.btest(Path('fs/test_lz4fs.cpp'), '2', args=['--pre-js', 'files.js'])'''
1441
1440
1442
1441
def test_separate_metadata_later (self ):
@@ -1603,8 +1602,7 @@ def test_egl_createcontext_error(self):
1603
1602
def test_worker (self ):
1604
1603
# Test running in a web worker
1605
1604
create_file ('file.dat' , 'data for worker' )
1606
- html_file = open ('main.html' , 'w' )
1607
- html_file .write ('''
1605
+ create_file ('main.html' , '''
1608
1606
<html>
1609
1607
<body>
1610
1608
Worker Test
@@ -1620,7 +1618,6 @@ def test_worker(self):
1620
1618
</body>
1621
1619
</html>
1622
1620
''' % self .port )
1623
- html_file .close ()
1624
1621
1625
1622
for file_data in [1 , 0 ]:
1626
1623
cmd = [EMCC , test_file ('hello_world_worker.cpp' ), '-o' , 'worker.js' ] + (['--preload-file' , 'file.dat' ] if file_data else [])
@@ -1636,8 +1633,7 @@ def test_chunked_synchronous_xhr(self):
1636
1633
main = 'chunked_sync_xhr.html'
1637
1634
worker_filename = "download_and_checksum_worker.js"
1638
1635
1639
- html_file = open (main , 'w' )
1640
- html_file .write (r"""
1636
+ create_file (main , r"""
1641
1637
<!doctype html>
1642
1638
<html>
1643
1639
<head><meta charset="utf-8"><title>Chunked XHR</title></head>
@@ -1671,13 +1667,8 @@ def test_chunked_synchronous_xhr(self):
1671
1667
</body>
1672
1668
</html>
1673
1669
""" % self .port )
1674
- html_file .close ()
1675
1670
1676
- c_source_filename = "checksummer.c"
1677
-
1678
- prejs_filename = "worker_prejs.js"
1679
- prejs_file = open (prejs_filename , 'w' )
1680
- prejs_file .write (r"""
1671
+ create_file ('worker_prejs.js' , r"""
1681
1672
if (typeof(Module) === "undefined") Module = {};
1682
1673
Module["arguments"] = ["/bigfile"];
1683
1674
Module["preInit"] = function() {
@@ -1687,11 +1678,10 @@ def test_chunked_synchronous_xhr(self):
1687
1678
Module["print"] = function(s) { self.postMessage({channel: "stdout", line: s}); };
1688
1679
Module["printErr"] = function(s) { self.postMessage({channel: "stderr", char: s, trace: ((doTrace && s === 10) ? new Error().stack : null)}); doTrace = false; };
1689
1680
""" )
1690
- prejs_file .close ()
1691
1681
# vs. os.path.join(self.get_dir(), filename)
1692
1682
# vs. test_file('hello_world_gles.c')
1693
- self .compile_btest ([test_file (c_source_filename ), '-g' , '-sSMALL_XHR_CHUNKS' , '-o' , worker_filename ,
1694
- '--pre-js' , prejs_filename ])
1683
+ self .compile_btest ([test_file ('checksummer.c' ), '-g' , '-sSMALL_XHR_CHUNKS' , '-o' , worker_filename ,
1684
+ '--pre-js' , 'worker_prejs.js' ])
1695
1685
chunkSize = 1024
1696
1686
data = os .urandom (10 * chunkSize + 1 ) # 10 full chunks and one 1 byte chunk
1697
1687
checksum = zlib .adler32 (data ) & 0xffffffff # Python 2 compatibility: force bigint
@@ -5077,7 +5067,7 @@ def test_wasm2js_fallback(self):
5077
5067
# Then disable WebAssembly support in VM, and try again.. Should still work with Wasm2JS fallback.
5078
5068
html = read_file ('test.html' )
5079
5069
html = html .replace ('<body>' , '<body><script>delete WebAssembly;</script>' )
5080
- open ('test.html' , 'w' ). write ( html )
5070
+ create_file ('test.html' , html )
5081
5071
os .remove ('test.wasm' ) # Also delete the Wasm file to test that it is not attempted to be loaded.
5082
5072
self .run_browser ('test.html' , 'hello!' , '/report_result?exit:0' )
5083
5073
0 commit comments