Skip to content

Commit ed3f6de

Browse files
authored
test_browser.py: Use create_file API. NFC (#17069)
1 parent 5b63376 commit ed3f6de

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

site/source/docs/porting/files/Synchronous-Virtual-XHR-Backed-File-System-Usage.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Instructions
3333

3434
.. include:: ../../../../../tests/test_browser.py
3535
:literal:
36-
:start-after: prejs_file.write(r"""
36+
:start-after: create_file('worker_prejs.js'
3737
:end-before: var doTrace = true;
3838
:code: javascript
3939

@@ -63,7 +63,7 @@ Instructions
6363

6464
.. include:: ../../../../../tests/test_browser.py
6565
:literal:
66-
:start-after: html_file.write(r"""
66+
:start-after: create_file('main.html',
6767
:end-before: """ % self.port)
6868
:code: html
6969

tests/test_browser.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ def test_zzz_html_source_map(self):
214214
# multiple mapped lines. in other words, if the program consists of a
215215
# single 'throw' statement, browsers may just map any thrown exception to
216216
# 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'''
219218
#include <cstdio>
220219
221220
int main() {
@@ -1380,18 +1379,18 @@ def test_fs_workerfs_read(self):
13801379
def test_fs_workerfs_package(self):
13811380
create_file('file1.txt', 'first')
13821381
ensure_dir('sub')
1383-
open(Path('sub/file2.txt'), 'w').write('second')
1382+
create_file('sub/file2.txt', 'second')
13841383
self.run_process([FILE_PACKAGER, 'files.data', '--preload', 'file1.txt', Path('sub/file2.txt'), '--separate-metadata', '--js-output=files.js'])
13851384
self.btest(Path('fs/test_workerfs_package.cpp'), '1', args=['-lworkerfs.js', '--proxy-to-worker', '-lworkerfs.js'])
13861385

13871386
def test_fs_lz4fs_package(self):
13881387
# generate data
13891388
ensure_dir('subdir')
13901389
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))
13921391
random_data = bytearray(random.randint(0, 255) for x in range(1024 * 128 * 10 + 1))
13931392
random_data[17] = ord('X')
1394-
open('file3.txt', 'wb').write(random_data)
1393+
create_file('file3.txt', random_data, binary=True)
13951394

13961395
# compress in emcc, -sLZ4 tells it to tell the file packager
13971396
print('emcc-normal')
@@ -1404,7 +1403,7 @@ def test_fs_lz4fs_package(self):
14041403
# compress in the file packager, on the server. the client receives compressed data and can just use it. this is typical usage
14051404
print('normal')
14061405
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)
14081407
self.btest(Path('fs/test_lz4fs.cpp'), '2', args=['--pre-js', 'files.js', '-sLZ4=1', '-sFORCE_FILESYSTEM'])
14091408
print(' opts')
14101409
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):
14361435
shutil.copyfile('file2.txt', Path('files/file2.txt'))
14371436
shutil.copyfile('file3.txt', Path('files/file3.txt'))
14381437
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)
14401439
self.btest(Path('fs/test_lz4fs.cpp'), '2', args=['--pre-js', 'files.js'])'''
14411440

14421441
def test_separate_metadata_later(self):
@@ -1603,8 +1602,7 @@ def test_egl_createcontext_error(self):
16031602
def test_worker(self):
16041603
# Test running in a web worker
16051604
create_file('file.dat', 'data for worker')
1606-
html_file = open('main.html', 'w')
1607-
html_file.write('''
1605+
create_file('main.html', '''
16081606
<html>
16091607
<body>
16101608
Worker Test
@@ -1620,7 +1618,6 @@ def test_worker(self):
16201618
</body>
16211619
</html>
16221620
''' % self.port)
1623-
html_file.close()
16241621

16251622
for file_data in [1, 0]:
16261623
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):
16361633
main = 'chunked_sync_xhr.html'
16371634
worker_filename = "download_and_checksum_worker.js"
16381635

1639-
html_file = open(main, 'w')
1640-
html_file.write(r"""
1636+
create_file(main, r"""
16411637
<!doctype html>
16421638
<html>
16431639
<head><meta charset="utf-8"><title>Chunked XHR</title></head>
@@ -1671,13 +1667,8 @@ def test_chunked_synchronous_xhr(self):
16711667
</body>
16721668
</html>
16731669
""" % self.port)
1674-
html_file.close()
16751670

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"""
16811672
if (typeof(Module) === "undefined") Module = {};
16821673
Module["arguments"] = ["/bigfile"];
16831674
Module["preInit"] = function() {
@@ -1687,11 +1678,10 @@ def test_chunked_synchronous_xhr(self):
16871678
Module["print"] = function(s) { self.postMessage({channel: "stdout", line: s}); };
16881679
Module["printErr"] = function(s) { self.postMessage({channel: "stderr", char: s, trace: ((doTrace && s === 10) ? new Error().stack : null)}); doTrace = false; };
16891680
""")
1690-
prejs_file.close()
16911681
# vs. os.path.join(self.get_dir(), filename)
16921682
# 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'])
16951685
chunkSize = 1024
16961686
data = os.urandom(10 * chunkSize + 1) # 10 full chunks and one 1 byte chunk
16971687
checksum = zlib.adler32(data) & 0xffffffff # Python 2 compatibility: force bigint
@@ -5077,7 +5067,7 @@ def test_wasm2js_fallback(self):
50775067
# Then disable WebAssembly support in VM, and try again.. Should still work with Wasm2JS fallback.
50785068
html = read_file('test.html')
50795069
html = html.replace('<body>', '<body><script>delete WebAssembly;</script>')
5080-
open('test.html', 'w').write(html)
5070+
create_file('test.html', html)
50815071
os.remove('test.wasm') # Also delete the Wasm file to test that it is not attempted to be loaded.
50825072
self.run_browser('test.html', 'hello!', '/report_result?exit:0')
50835073

0 commit comments

Comments
 (0)