Skip to content

Commit 13d6d6c

Browse files
authored
Use python3's pathlib in test code. NFC (#14175)
There are plans underway to start using pathlib more in emscripten. See: #14074 This change a designed to test the water by using `pathlib` in test code. This allows us to use unix-like paths throughout the tests that will get converted to windows-paths on windows machines automatically by the pathlib library.
1 parent a67283f commit 13d6d6c

File tree

8 files changed

+665
-658
lines changed

8 files changed

+665
-658
lines changed

tests/jsrun.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def check_engine(engine):
6262
if engine_path not in WORKING_ENGINES:
6363
logging.debug('Checking JS engine %s' % engine)
6464
try:
65-
output = run_js(shared.path_from_root('tests', 'hello_world.js'), engine, skip_check=True)
65+
output = run_js(shared.path_from_root('tests/hello_world.js'), engine, skip_check=True)
6666
if 'hello, world!' in output:
6767
WORKING_ENGINES[engine_path] = True
6868
else:

tests/runner.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import time
4444
import unittest
4545
import webbrowser
46+
from pathlib import Path
4647
from http.server import HTTPServer, SimpleHTTPRequestHandler
4748
from urllib.parse import unquote, unquote_plus
4849

@@ -65,7 +66,7 @@
6566

6667
def path_from_root(*pathelems):
6768
"""Construct a path relative to the emscripten root directory."""
68-
return os.path.join(__rootpath__, *pathelems)
69+
return str(Path(__rootpath__, *pathelems))
6970

7071

7172
sys.path.append(path_from_root('third_party/websockify'))
@@ -111,7 +112,7 @@ def delete_contents(pathname):
111112

112113
def test_file(*path_components):
113114
"""Construct a path relative to the emscripten "tests" directory."""
114-
return os.path.join(TEST_ROOT, *path_components)
115+
return str(Path(TEST_ROOT, *path_components))
115116

116117

117118
# checks if browser testing is enabled
@@ -241,8 +242,8 @@ def modified(self):
241242

242243

243244
def ensure_dir(dirname):
244-
if not os.path.isdir(dirname):
245-
os.makedirs(dirname)
245+
dirname = Path(dirname)
246+
dirname.mkdir(parents=True, exist_ok=True)
246247

247248

248249
def limit_size(string, maxbytes=800000 * 20, maxlines=100000, max_line=5000):
@@ -259,14 +260,16 @@ def limit_size(string, maxbytes=800000 * 20, maxlines=100000, max_line=5000):
259260

260261

261262
def create_file(name, contents, binary=False):
262-
assert not os.path.isabs(name)
263-
mode = 'wb' if binary else 'w'
264-
with open(name, mode) as f:
265-
f.write(contents)
263+
name = Path(name)
264+
assert not name.is_absolute()
265+
if binary:
266+
name.write_bytes(contents)
267+
else:
268+
name.write_text(contents)
266269

267270

268271
def make_executable(name):
269-
os.chmod(name, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
272+
Path(name).chmod(stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
270273

271274

272275
# The core test modes
@@ -595,7 +598,7 @@ def build(self, filename, libraries=[], includes=[], force_c=False,
595598
if shared.suffix(filename) not in ('.i', '.ii'):
596599
# Add the location of the test file to include path.
597600
cmd += ['-I.']
598-
cmd += ['-I' + include for include in includes]
601+
cmd += ['-I' + str(include) for include in includes]
599602

600603
self.run_process(cmd, stderr=self.stderr_redirect if not DEBUG else None)
601604
self.assertExists(output)
@@ -1100,8 +1103,8 @@ def get_poppler_library(self, env_init=None):
11001103
self.set_setting('ERROR_ON_UNDEFINED_SYMBOLS', 0)
11011104

11021105
self.emcc_args += [
1103-
'-I' + test_file('third_party', 'freetype', 'include'),
1104-
'-I' + test_file('third_party', 'poppler', 'include')
1106+
'-I' + test_file('third_party/freetype/include'),
1107+
'-I' + test_file('third_party/poppler/include')
11051108
]
11061109

11071110
freetype = self.get_freetype_library()
@@ -1613,10 +1616,11 @@ def build_library(name,
16131616
generated_libs = [generated_libs]
16141617
source_dir = test_file(name.replace('_native', ''))
16151618

1616-
project_dir = os.path.join(build_dir, name)
1619+
project_dir = Path(build_dir, name)
16171620
if os.path.exists(project_dir):
16181621
shutil.rmtree(project_dir)
1619-
shutil.copytree(source_dir, project_dir) # Useful in debugging sometimes to comment this out, and two lines above
1622+
# Useful in debugging sometimes to comment this out, and two lines above
1623+
shutil.copytree(source_dir, project_dir)
16201624

16211625
generated_libs = [os.path.join(project_dir, lib) for lib in generated_libs]
16221626
if native:

tests/test_browser.py

+227-228
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)