Skip to content

Commit 0511321

Browse files
committed
fix(runtest.py): Refactor create_tmp_file function and improve error handling for module registration
1 parent 9813e4f commit 0511321

1 file changed

Lines changed: 42 additions & 37 deletions

File tree

tests/wamr-test-suites/spec-test-script/runtest.py

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import atexit
88
import math
99
import os
10+
import pathlib
1011
import re
1112
import shutil
1213
import struct
@@ -81,9 +82,8 @@ def log(data, end='\n'):
8182
print(data, end=end)
8283
sys.stdout.flush()
8384

84-
def create_tmp_file(suffix: str) -> str:
85-
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp_file:
86-
return tmp_file.name
85+
def create_tmp_file(prefix: str, suffix: str) -> str:
86+
return tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix, delete=False).name
8787

8888
# TODO: do we need to support '\n' too
8989
import platform
@@ -1241,10 +1241,10 @@ def run_wasm_with_repl(wasm_tempfile, aot_tempfile, opts, r):
12411241
def create_tmpfiles(wast_name):
12421242
tempfiles = []
12431243

1244-
tempfiles.append(create_tmp_file(".wast"))
1245-
tempfiles.append(create_tmp_file(".wasm"))
1244+
tempfiles.append(create_tmp_file(wast_name, ".wast"))
1245+
tempfiles.append(create_tmp_file(wast_name, ".wasm"))
12461246
if test_aot:
1247-
tempfiles.append(create_tmp_file(".aot"))
1247+
tempfiles.append(create_tmp_file(wast_name, ".aot"))
12481248

12491249
# add these temp file to temporal repo, will be deleted when finishing the test
12501250
temp_file_repo.extend(tempfiles)
@@ -1314,10 +1314,15 @@ def test_assert_with_exception(form, wast_tempfile, wasm_tempfile, aot_tempfile,
13141314
else:
13151315
SKIP_TESTS = C_SKIP_TESTS
13161316

1317-
wast_tempfile = create_tmp_file(".wast")
1318-
wasm_tempfile = create_tmp_file(".wasm")
1317+
case_file = pathlib.Path(opts.test_file.name)
1318+
assert(case_file.exists()), f"Test file {case_file} doesn't exist"
1319+
1320+
case_name = case_file.stem + "_"
1321+
wast_tempfile = create_tmp_file(case_name, ".wast")
1322+
wasm_tempfile = create_tmp_file(case_name, ".wasm")
1323+
13191324
if test_aot:
1320-
aot_tempfile = create_tmp_file(".aot")
1325+
aot_tempfile = create_tmp_file(case_name, ".aot")
13211326
# could be potientially compiled to aot
13221327
# with the future following call test_assert_xxx,
13231328
# add them to temp_file_repo now even if no actual following file,
@@ -1498,35 +1503,35 @@ def test_assert_with_exception(form, wast_tempfile, wasm_tempfile, aot_tempfile,
14981503
elif re.match("^\(register\\b.*", form):
14991504
# get module's new name from the register cmd
15001505
name_new =re.split('\"',re.search('\".*\"',form).group(0))[1]
1501-
if name_new:
1502-
if not name_new in temp_module_table:
1503-
print(f"can not find module name {name_new} from the register, use {wasm_tempfile} instead")
1504-
1505-
just_generated = temp_module_table.get(name_new, wasm_tempfile)
1506-
if not os.path.exists(just_generated):
1507-
raise Exception("can not find file %s" % just_generated)
1508-
1509-
print("f{just_generated} is copied to {new_module}")
1510-
1511-
# copy the just generated module into a file with specific name
1512-
new_module = os.path.join(tempfile.gettempdir(), name_new + ".wasm")
1513-
shutil.copyfile(just_generated, new_module)
1514-
1515-
# add new_module copied from the old into temp_file_repo[]
1516-
temp_file_repo.append(new_module)
1517-
1518-
if test_aot:
1519-
new_module_aot = os.path.join(tempfile.gettempdir(), name_new + ".aot")
1520-
r = compile_wasm_to_aot(new_module, new_module_aot, True, opts, r)
1521-
try:
1522-
assert_prompt(r, ['Compile success'], opts.start_timeout, True)
1523-
except:
1524-
raise Exception(f"compile wasm to aot failed {r.buf}")
1525-
# add aot module into temp_file_repo[]
1526-
temp_file_repo.append(new_module_aot)
1527-
else:
1506+
if not name_new:
15281507
# there is no name defined in register cmd
1529-
raise Exception("can not find module name from the register")
1508+
raise Exception(f"Not following register cmd pattern {form}")
1509+
1510+
# assumption
1511+
# - There exists a module in the form of (module $name).
1512+
# - The nearest module in the form of (module), without $name, is the candidate for registration.
1513+
if not name_new in temp_module_table:
1514+
print(f"Module {name_new} is not found in temp_module_table. use the nearest module")
1515+
1516+
for_registration = temp_module_table.get(name_new, wasm_tempfile)
1517+
assert os.path.exists(for_registration), f"module {for_registration} is not found"
1518+
1519+
new_module = os.path.join(tempfile.gettempdir(), name_new + ".wasm")
1520+
# for_registration(tmpfile) --copy-> name_new.wasm
1521+
shutil.copyfile(for_registration, new_module)
1522+
1523+
# add new_module copied from the old into temp_file_repo[]
1524+
temp_file_repo.append(new_module)
1525+
1526+
if test_aot:
1527+
new_module_aot = os.path.join(tempfile.gettempdir(), name_new + ".aot")
1528+
r = compile_wasm_to_aot(new_module, new_module_aot, True, opts, r)
1529+
try:
1530+
assert_prompt(r, ['Compile success'], opts.start_timeout, True)
1531+
except:
1532+
raise Exception("compile wasm to aot failed")
1533+
# add aot module into temp_file_repo[]
1534+
temp_file_repo.append(new_module_aot)
15301535
else:
15311536
raise Exception("unrecognized form '%s...'" % form[0:40])
15321537
except Exception as e:

0 commit comments

Comments
 (0)