1#
2# This is minimal MicroPython variant of run-tests script, which uses
3# .exp files as generated by run-tests --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#
8import usys as sys
9import uos as os
10
11
12tests = ["basics", "micropython", "float", "import", "io", " misc", "unicode", "extmod", "unix"]
13
14if sys.platform == "win32":
15    MICROPYTHON = "micropython.exe"
16else:
17    MICROPYTHON = "micropython"
18
19
20def should_skip(test):
21    if test.startswith("native"):
22        return True
23    if test.startswith("viper"):
24        return True
25
26
27test_count = 0
28passed_count = 0
29skip_count = 0
30
31for suite in tests:
32    # print("Running in: %s" % suite)
33    if sys.platform == "win32":
34        # dir /b prints only contained filenames, one on a line
35        # http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/dir.mspx
36        r = os.system("dir /b %s/*.py >tests.lst" % suite)
37    else:
38        r = os.system("ls %s/*.py | xargs -n1 basename >tests.lst" % suite)
39    assert r == 0
40
41    with open("tests.lst") as f:
42        testcases = f.readlines()
43        testcases = [l[:-1] for l in testcases]
44    assert testcases, "No tests found in dir '%s', which is implausible" % suite
45    # print(testcases)
46    for t in testcases:
47        if t == "native_check.py":
48            continue
49
50        qtest = "%s/%s" % (suite, t)
51
52        if should_skip(t):
53            print("skip " + qtest)
54            skip_count += 1
55            continue
56
57        exp = None
58        try:
59            f = open(qtest + ".exp")
60            exp = f.read()
61            f.close()
62        except OSError:
63            pass
64
65        if exp is not None:
66            # print("run " + qtest)
67            r = os.system(MICROPYTHON + " %s >.tst.out" % qtest)
68            if r == 0:
69                f = open(".tst.out")
70                out = f.read()
71                f.close()
72            else:
73                out = "CRASH"
74
75            if out == "SKIP\n":
76                print("skip " + qtest)
77                skip_count += 1
78            else:
79                if out == exp:
80                    print("pass " + qtest)
81                    passed_count += 1
82                else:
83                    print("FAIL " + qtest)
84
85                test_count += 1
86        else:
87            skip_count += 1
88
89print("%s tests performed" % test_count)
90print("%s tests passed" % passed_count)
91if test_count != passed_count:
92    print("%s tests failed" % (test_count - passed_count))
93if skip_count:
94    print("%s tests skipped" % skip_count)
95