Skip to content

Commit d899635

Browse files
committed
test: add test runner and gitignore
1 parent 350637b commit d899635

File tree

4 files changed

+147
-3
lines changed

4 files changed

+147
-3
lines changed

tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.out
2+
libs/

tests/asyncio/asyncio_gather_notimpl.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ async def main():
3333
loop.set_exception_handler(custom_handler)
3434

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

45-
print("====")
46-
4746
# Test case where can't gather on a task being waited.
47+
print("=" * 10)
4848
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))]
4949
asyncio.create_task(gather_task(tasks[0], tasks[1]))
5050
await tasks[0] # wait on this task before the gather starts
5151
await tasks[1]
5252

53+
# Can't gather after a task has completed
54+
print("=" * 10)
55+
task_1 = asyncio.create_task(task(1))
56+
57+
try:
58+
# Wait for task_1 to complete
59+
await task_1
60+
61+
await asyncio.gather(task_1)
62+
except RuntimeError as er:
63+
print(repr(er))
64+
5365

5466
asyncio.run(main())
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
==========
12
task start 1
23
task start 2
34
gather_task start
45
RuntimeError("can't wait",)
56
task end 1
67
task end 2
78
gather_task end
8-
====
9+
==========
910
task start 1
1011
task start 2
1112
gather_task start
1213
RuntimeError("can't gather",)
1314
task end 1
1415
task end 2
16+
==========
17+
task start 1
18+
task end 1
19+
RuntimeError("can't gather",)

tests/run_tests.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#
2+
# This is minimal MicroPython variant of run-tests.py script, which uses
3+
# .exp files as generated by run-tests.py --write-exp. It is useful to run
4+
# testsuite on systems which have neither CPython3 nor unix shell.
5+
# This script is intended to be run by the same interpreter executable
6+
# which is to be tested, so should use minimal language functionality.
7+
#
8+
import sys
9+
import os
10+
11+
try:
12+
from typing import List, Tuple
13+
except ImportError:
14+
pass
15+
16+
AVAILABLE_SUITES = ["asyncio"]
17+
18+
19+
def get_interpreter():
20+
interpreter = os.getenv("MICROPY_MICROPYTHON")
21+
22+
if interpreter:
23+
return interpreter
24+
25+
if sys.platform == "win32":
26+
return "micropython.exe"
27+
28+
return "micropython"
29+
30+
31+
def get_testcases(suite: str) -> List[str]:
32+
if sys.platform == "win32":
33+
# dir /b prints only contained filenames, one on a line
34+
# http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/dir.mspx
35+
result = os.system("dir /b %s/*.py >tests.lst" % suite)
36+
else:
37+
result = os.system("ls %s/*.py | xargs -n1 basename >tests.lst" % suite)
38+
39+
assert result == 0
40+
41+
with open("tests.lst") as test_list_file:
42+
testcases = test_list_file.readlines()
43+
testcases = [l[:-1] for l in testcases]
44+
45+
os.system("rm tests.lst")
46+
assert testcases, "No tests found in dir '%s', which is implausible" % suite
47+
48+
return testcases
49+
50+
51+
def run_testcase(suite: str, testcase: str):
52+
qtest = "%s/%s" % (suite, testcase)
53+
54+
try:
55+
with open("%s.exp" % qtest) as expected_output_file:
56+
expected_output = expected_output_file.read()
57+
except OSError as exc:
58+
raise RuntimeError("SKIP") from exc
59+
60+
result = os.system("{0} {1} 2> {1}.out > {1}.out".format(get_interpreter(), qtest))
61+
62+
with open("%s.out" % qtest) as actual_output_file:
63+
actual_output = actual_output_file.read()
64+
65+
if result != 0:
66+
actual_output += "\n\nCRASH\n"
67+
68+
if actual_output == "SKIP\n":
69+
print("skip %s" % qtest)
70+
raise RuntimeError("SKIP")
71+
72+
if actual_output != expected_output:
73+
print("FAIL %s" % qtest)
74+
return False
75+
76+
print("pass %s" % qtest)
77+
return True
78+
79+
80+
def run_suite(suite: str) -> Tuple[int, int, int]:
81+
test_count = 0
82+
passed_count = 0
83+
skip_count = 0
84+
85+
testcases = get_testcases(suite)
86+
87+
for testcase in testcases:
88+
try:
89+
if run_testcase(suite, testcase):
90+
passed_count += 1
91+
92+
test_count += 1
93+
except RuntimeError as exc:
94+
if str(exc) == "SKIP":
95+
skip_count += 1
96+
97+
return test_count, passed_count, skip_count
98+
99+
100+
def main():
101+
test_count = 0
102+
passed_count = 0
103+
skip_count = 0
104+
105+
for suite in AVAILABLE_SUITES:
106+
suite_test_count, suite_passed_count, suite_skip_count = run_suite(suite)
107+
108+
test_count += suite_test_count
109+
passed_count += suite_passed_count
110+
skip_count += suite_skip_count
111+
112+
print("-" * 20)
113+
print("%s tests performed" % test_count)
114+
print("%s tests passed" % passed_count)
115+
if test_count != passed_count:
116+
print("%s tests failed" % (test_count - passed_count))
117+
if skip_count:
118+
print("%s tests skipped" % skip_count)
119+
120+
if test_count - passed_count > 0:
121+
sys.exit(1)
122+
123+
124+
if __name__ == "__main__":
125+
main()

0 commit comments

Comments
 (0)