Skip to content

Commit 24e5ed5

Browse files
committed
Use python3's pathlib in test code. NFC
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 716f386 commit 24e5ed5

File tree

5 files changed

+658
-651
lines changed

5 files changed

+658
-651
lines changed

tests/runner.py

Lines changed: 18 additions & 13 deletions
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

@@ -109,9 +110,9 @@ def delete_contents(pathname):
109110
try_delete(os.path.join(pathname, entry))
110111

111112

112-
def test_file(*path_components):
113+
def test_file(*path):
113114
"""Construct a path relative to the emscripten "tests" directory."""
114-
return os.path.join(TEST_ROOT, *path_components)
115+
return os.fspath(Path(TEST_ROOT, *path))
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
@@ -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,12 @@ 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)
1620+
print(project_dir)
16171621
if os.path.exists(project_dir):
16181622
shutil.rmtree(project_dir)
1619-
shutil.copytree(source_dir, project_dir) # Useful in debugging sometimes to comment this out, and two lines above
1623+
# Useful in debugging sometimes to comment this out, and two lines above
1624+
shutil.copytree(source_dir, project_dir)
16201625

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

0 commit comments

Comments
 (0)