Skip to content

Commit 1a9afc7

Browse files
committed
fix(runtest.py): Improve error handling in Runner class and cleanup process
1 parent ac646ab commit 1a9afc7

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,16 @@ def _read_stdout_byte(self) -> Tuple[bool, Optional[bytes]]:
181181
# queue, so we keep it for non-windows platforms.
182182
[outs,_,_] = select([self.stdout], [], [], 1)
183183
if self.stdout in outs:
184-
return True, self.stdout.read(1)
184+
try:
185+
stdout_byte = self.stdout.read(1)
186+
except ValueError:
187+
return True, None
188+
except OSError:
189+
return True, None
190+
except Exception as e:
191+
print("Exception: ", e)
192+
return False, None
193+
return True, stdout_byte
185194
else:
186195
return False, None
187196

@@ -1302,6 +1311,20 @@ def test_assert_with_exception(form, wast_tempfile, wasm_tempfile, aot_tempfile,
13021311
raise Exception("Failed:\n expected: '%s'\n got: '%s'" % \
13031312
(expected, r.buf))
13041313

1314+
def recently_added_wasm(temp_file_repo):
1315+
for f in reversed(temp_file_repo):
1316+
if not f:
1317+
continue
1318+
1319+
assert os.path.exists(f), f"temp file {f} should exist"
1320+
1321+
if os.path.getsize(f) == 0:
1322+
continue
1323+
1324+
if f.endswith(".wasm"):
1325+
return f
1326+
1327+
13051328
if __name__ == "__main__":
13061329
opts = parser.parse_args(sys.argv[1:])
13071330
# print('Input param :',opts)
@@ -1501,10 +1524,12 @@ def test_assert_with_exception(form, wast_tempfile, wasm_tempfile, aot_tempfile,
15011524
# assumption
15021525
# - There exists a module in the form of (module $name).
15031526
# - The nearest module in the form of (module), without $name, is the candidate for registration.
1527+
recently_wasm = recently_added_wasm(temp_file_repo)
15041528
if not name_new in temp_module_table:
1505-
print(f"Module {name_new} is not found in temp_module_table. use the nearest module {wasm_tempfile}")
1529+
print(temp_file_repo)
1530+
print(f"Module {name_new} is not found in temp_module_table. use the nearest module {recently_wasm}")
15061531

1507-
for_registration = temp_module_table.get(name_new, wasm_tempfile)
1532+
for_registration = temp_module_table.get(name_new, recently_wasm)
15081533
assert os.path.exists(for_registration), f"module {for_registration} is not found"
15091534

15101535
new_module = os.path.join(tempfile.gettempdir(), name_new + ".wasm")
@@ -1547,25 +1572,25 @@ def test_assert_with_exception(form, wast_tempfile, wasm_tempfile, aot_tempfile,
15471572
else:
15481573
ret_code = 0
15491574
finally:
1550-
if not opts.no_cleanup:
1551-
log(f"Removing tempfiles {wast_tempfile} and {wasm_tempfile}")
1552-
try:
1575+
try:
1576+
if not opts.no_cleanup:
15531577
# remove the files under /tempfiles/ and copy of .wasm files
1578+
log(f"Removing {temp_file_repo}")
1579+
15541580
for t in temp_file_repo:
15551581
# None and empty
15561582
if not t:
15571583
continue
15581584

15591585
if os.path.exists(t):
1560-
log(f"Removing {t}")
15611586
os.remove(t)
1562-
1563-
except Exception as e:
1564-
print("Failed to remove tempfiles: %s" % e)
1565-
# ignore the exception
1566-
ret_code = 0
1567-
else:
1568-
log("Leaving tempfiles: %s" % ([wast_tempfile, wasm_tempfile]))
1587+
else:
1588+
log(f"Leaving {temp_file_repo}")
1589+
1590+
except Exception as e:
1591+
print("Failed to remove tempfiles: %s" % e)
1592+
# ignore the exception
1593+
ret_code = 0
15691594

15701595
log(f"### End testing {opts.test_file.name} with {ret_code}")
15711596
sys.exit(ret_code)

0 commit comments

Comments
 (0)