Skip to content

Commit 523f512

Browse files
committed
test: add test runner and gitignore
1 parent da943a7 commit 523f512

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

tests/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
*.out

tests/run_tests.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#! /usr/bin/env python3
2+
3+
# SPDX-FileCopyrightText: 2019 Damien P. George
4+
#
5+
# SPDX-License-Identifier: MIT
6+
#
7+
# MicroPython uasyncio module
8+
# MIT license; Copyright (c) 2019 Damien P. George
9+
#
10+
11+
import sys
12+
import os
13+
14+
try:
15+
from typing import List, Tuple
16+
except ImportError:
17+
pass
18+
19+
AVAILABLE_SUITES = ["asyncio"]
20+
21+
22+
LICENSE_PREFIX = """# SPDX-FileCopyrightText: 2019 Damien P. George
23+
#
24+
# SPDX-License-Identifier: MIT
25+
"""
26+
27+
28+
def get_interpreter():
29+
interpreter = os.getenv("MICROPY_MICROPYTHON")
30+
31+
if interpreter:
32+
return interpreter
33+
34+
if sys.platform == "win32":
35+
return "micropython.exe"
36+
37+
return "micropython"
38+
39+
40+
def get_testcases(suite: str) -> List[str]:
41+
if sys.platform == "win32":
42+
# dir /b prints only contained filenames, one on a line
43+
# http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/dir.mspx
44+
result = os.system("dir /b %s/*.py >tests.lst" % suite)
45+
else:
46+
result = os.system("ls %s/*.py | xargs -n1 basename >tests.lst" % suite)
47+
48+
assert result == 0
49+
50+
with open("tests.lst") as test_list_file:
51+
testcases = test_list_file.readlines()
52+
testcases = [l[:-1] for l in testcases]
53+
54+
os.system("rm tests.lst")
55+
assert testcases, "No tests found in dir '%s', which is implausible" % suite
56+
57+
return testcases
58+
59+
60+
def run_testcase(suite: str, testcase: str):
61+
qtest = "%s/%s" % (suite, testcase)
62+
63+
try:
64+
with open("%s.exp" % qtest) as expected_output_file:
65+
expected_output = expected_output_file.read()
66+
except OSError as exc:
67+
raise RuntimeError("SKIP") from exc
68+
69+
with open("{0}.out".format(qtest), "w") as actual_output_file:
70+
actual_output_file.write(LICENSE_PREFIX)
71+
72+
result = os.system(
73+
"{0} {1} 2>> {1}.out >> {1}.out".format(get_interpreter(), qtest)
74+
)
75+
76+
with open("%s.out" % qtest) as actual_output_file:
77+
actual_output = actual_output_file.read()
78+
79+
if result != 0:
80+
actual_output += "\n\nCRASH\n"
81+
82+
if actual_output == LICENSE_PREFIX + "SKIP\n":
83+
print("skip %s" % qtest)
84+
raise RuntimeError("SKIP")
85+
86+
if actual_output != expected_output:
87+
print("FAIL %s" % qtest)
88+
os.system("diff -u {0}.exp {0}.out".format(qtest))
89+
return False
90+
91+
print("pass %s" % qtest)
92+
return True
93+
94+
95+
def run_suite(suite: str) -> Tuple[int, int, int]:
96+
test_count = 0
97+
passed_count = 0
98+
skip_count = 0
99+
100+
testcases = get_testcases(suite)
101+
102+
for testcase in testcases:
103+
try:
104+
if run_testcase(suite, testcase):
105+
passed_count += 1
106+
107+
test_count += 1
108+
except RuntimeError as exc:
109+
if str(exc) == "SKIP":
110+
skip_count += 1
111+
112+
return test_count, passed_count, skip_count
113+
114+
115+
def main():
116+
test_count = 0
117+
passed_count = 0
118+
skip_count = 0
119+
120+
for suite in AVAILABLE_SUITES:
121+
suite_test_count, suite_passed_count, suite_skip_count = run_suite(suite)
122+
123+
test_count += suite_test_count
124+
passed_count += suite_passed_count
125+
skip_count += suite_skip_count
126+
127+
print("-" * 20)
128+
print("%s tests performed" % test_count)
129+
print("%s tests passed" % passed_count)
130+
if test_count != passed_count:
131+
print("%s tests failed" % (test_count - passed_count))
132+
if skip_count:
133+
print("%s tests skipped" % skip_count)
134+
135+
if test_count - passed_count > 0:
136+
sys.exit(1)
137+
138+
139+
if __name__ == "__main__":
140+
main()

0 commit comments

Comments
 (0)