Skip to content

Commit

Permalink
test: add test runner and gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
imnotjames committed Nov 12, 2023
1 parent 350637b commit d899635
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 3 deletions.
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.out
libs/
16 changes: 14 additions & 2 deletions tests/asyncio/asyncio_gather_notimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async def main():
loop.set_exception_handler(custom_handler)

# Test case where can't wait on a task being gathered.
print("=" * 10)
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))]
gt = asyncio.create_task(gather_task(tasks[0], tasks[1]))
await asyncio.sleep(0) # let the gather start
Expand All @@ -42,13 +43,24 @@ async def main():
print(repr(er))
await gt

print("====")

# Test case where can't gather on a task being waited.
print("=" * 10)
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))]
asyncio.create_task(gather_task(tasks[0], tasks[1]))
await tasks[0] # wait on this task before the gather starts
await tasks[1]

# Can't gather after a task has completed
print("=" * 10)
task_1 = asyncio.create_task(task(1))

try:
# Wait for task_1 to complete
await task_1

await asyncio.gather(task_1)
except RuntimeError as er:
print(repr(er))


asyncio.run(main())
7 changes: 6 additions & 1 deletion tests/asyncio/asyncio_gather_notimpl.py.exp
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
==========
task start 1
task start 2
gather_task start
RuntimeError("can't wait",)
task end 1
task end 2
gather_task end
====
==========
task start 1
task start 2
gather_task start
RuntimeError("can't gather",)
task end 1
task end 2
==========
task start 1
task end 1
RuntimeError("can't gather",)
125 changes: 125 additions & 0 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#
# This is minimal MicroPython variant of run-tests.py script, which uses
# .exp files as generated by run-tests.py --write-exp. It is useful to run
# testsuite on systems which have neither CPython3 nor unix shell.
# This script is intended to be run by the same interpreter executable
# which is to be tested, so should use minimal language functionality.
#
import sys
import os

try:
from typing import List, Tuple
except ImportError:
pass

AVAILABLE_SUITES = ["asyncio"]


def get_interpreter():
interpreter = os.getenv("MICROPY_MICROPYTHON")

if interpreter:
return interpreter

if sys.platform == "win32":
return "micropython.exe"

return "micropython"


def get_testcases(suite: str) -> List[str]:
if sys.platform == "win32":
# dir /b prints only contained filenames, one on a line
# http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/dir.mspx
result = os.system("dir /b %s/*.py >tests.lst" % suite)
else:
result = os.system("ls %s/*.py | xargs -n1 basename >tests.lst" % suite)

assert result == 0

with open("tests.lst") as test_list_file:
testcases = test_list_file.readlines()
testcases = [l[:-1] for l in testcases]

os.system("rm tests.lst")
assert testcases, "No tests found in dir '%s', which is implausible" % suite

return testcases


def run_testcase(suite: str, testcase: str):
qtest = "%s/%s" % (suite, testcase)

try:
with open("%s.exp" % qtest) as expected_output_file:
expected_output = expected_output_file.read()
except OSError as exc:
raise RuntimeError("SKIP") from exc

result = os.system("{0} {1} 2> {1}.out > {1}.out".format(get_interpreter(), qtest))

with open("%s.out" % qtest) as actual_output_file:
actual_output = actual_output_file.read()

if result != 0:
actual_output += "\n\nCRASH\n"

if actual_output == "SKIP\n":
print("skip %s" % qtest)
raise RuntimeError("SKIP")

if actual_output != expected_output:
print("FAIL %s" % qtest)
return False

print("pass %s" % qtest)
return True


def run_suite(suite: str) -> Tuple[int, int, int]:
test_count = 0
passed_count = 0
skip_count = 0

testcases = get_testcases(suite)

for testcase in testcases:
try:
if run_testcase(suite, testcase):
passed_count += 1

test_count += 1
except RuntimeError as exc:
if str(exc) == "SKIP":
skip_count += 1

return test_count, passed_count, skip_count


def main():
test_count = 0
passed_count = 0
skip_count = 0

for suite in AVAILABLE_SUITES:
suite_test_count, suite_passed_count, suite_skip_count = run_suite(suite)

test_count += suite_test_count
passed_count += suite_passed_count
skip_count += suite_skip_count

print("-" * 20)
print("%s tests performed" % test_count)
print("%s tests passed" % passed_count)
if test_count != passed_count:
print("%s tests failed" % (test_count - passed_count))
if skip_count:
print("%s tests skipped" % skip_count)

if test_count - passed_count > 0:
sys.exit(1)


if __name__ == "__main__":
main()

0 comments on commit d899635

Please sign in to comment.