Skip to content

Commit

Permalink
[test] generate JS/HTML wrappers for tests in subdirectories (#1808)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajklein authored Nov 1, 2024
1 parent cda4012 commit 268a03d
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions test/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
WASM_EXEC = os.path.join(INTERPRETER_DIR, 'wasm')

WAST_TESTS_DIR = os.path.join(SCRIPT_DIR, 'core')
WAST_TEST_SUBDIRS = [os.path.basename(d) for d in
filter(os.path.isdir,
glob.glob(os.path.join(WAST_TESTS_DIR, '*')))]
HARNESS_DIR = os.path.join(SCRIPT_DIR, 'harness')

HARNESS_FILES = ['testharness.js', 'testharnessreport.js', 'testharness.css']
Expand Down Expand Up @@ -67,13 +70,17 @@ def convert_wast_to_js(out_js_dir):

inputs = []

for wast_file in glob.glob(os.path.join(WAST_TESTS_DIR, '*.wast')):
for wast_file in glob.glob(os.path.join(WAST_TESTS_DIR, '**/*.wast'),
recursive = True):
# Don't try to compile tests that are supposed to fail.
if '.fail.' in wast_file:
continue

js_subdir = os.path.basename(os.path.dirname(wast_file))
if js_subdir == 'core':
js_subdir = ''
js_filename = os.path.basename(wast_file) + '.js'
js_file = os.path.join(out_js_dir, js_filename)
js_file = os.path.join(out_js_dir, js_subdir, js_filename)
inputs.append((wast_file, js_file))

pool = mp.Pool(processes=8)
Expand Down Expand Up @@ -112,9 +119,9 @@ def build_js(out_js_dir):
</head>
<body>
<script src={WPT_PREFIX}/testharness.js></script>
<script src={WPT_PREFIX}/testharnessreport.js></script>
<script src={PREFIX}/{JS_HARNESS}></script>
<script src="{WPT_PREFIX}/testharness.js"></script>
<script src="{WPT_PREFIX}/testharnessreport.js"></script>
<script src="{PREFIX}/{JS_HARNESS}"></script>
<div id=log></div>
"""
Expand All @@ -137,6 +144,8 @@ def wrap_single_test(js_file):

def build_html_js(out_dir):
ensure_empty_dir(out_dir)
for d in WAST_TEST_SUBDIRS:
ensure_empty_dir(os.path.join(out_dir, d))
copy_harness_files(out_dir, True)

tests = convert_wast_to_js(out_dir)
Expand All @@ -146,19 +155,25 @@ def build_html_js(out_dir):

def build_html_from_js(tests, html_dir, use_sync):
for js_file in tests:
js_filename = os.path.basename(js_file)
html_filename = js_filename + '.html'
html_file = os.path.join(html_dir, html_filename)
subdir = os.path.basename(os.path.dirname(js_file))
js_prefix = '../js'
if subdir == 'js':
subdir = ''
js_prefix = './js'
js_filename = os.path.join(js_prefix, subdir, os.path.basename(js_file))
html_filename = os.path.basename(js_file) + '.html'
html_file = os.path.join(html_dir, subdir, html_filename)
js_harness = "sync_index.js" if use_sync else "async_index.js"
harness_dir = os.path.join(js_prefix, 'harness')
with open(html_file, 'w+') as f:
content = HTML_HEADER.replace('{PREFIX}', './js/harness') \
.replace('{WPT_PREFIX}', './js/harness') \
content = HTML_HEADER.replace('{PREFIX}', harness_dir) \
.replace('{WPT_PREFIX}', harness_dir) \
.replace('{JS_HARNESS}', js_harness)
content += " <script src=./js/{SCRIPT}></script>".replace('{SCRIPT}', js_filename)
content += ' <script src="' + js_filename + '"></script>'
content += HTML_BOTTOM
f.write(content)

def build_html(html_dir, js_dir, use_sync):
def build_html(html_dir, use_sync):
print("Building HTML tests...")

js_html_dir = os.path.join(html_dir, 'js')
Expand Down Expand Up @@ -248,11 +263,15 @@ def process_args():

if js_dir is not None:
ensure_empty_dir(js_dir)
for d in WAST_TEST_SUBDIRS:
ensure_empty_dir(os.path.join(js_dir, d))
build_js(js_dir)

if html_dir is not None:
ensure_empty_dir(html_dir)
build_html(html_dir, js_dir, args.use_sync)
for d in WAST_TEST_SUBDIRS:
ensure_empty_dir(os.path.join(html_dir, d))
build_html(html_dir, args.use_sync)

if front_dir is not None:
ensure_empty_dir(front_dir)
Expand Down

0 comments on commit 268a03d

Please sign in to comment.