Subversion Repositories Kolibri OS

Rev

Rev 9321 | Rev 9323 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9249 Boppan 1
#!/usr/bin/python3
9312 Boppan 2
# Copyright 2021 Magomed Kostoev
9249 Boppan 3
# Published under MIT License
4
 
5
import os
6
import sys
9314 Boppan 7
import urllib
9249 Boppan 8
from importlib.machinery import SourceFileLoader
9310 Boppan 9
from shutil import which
9249 Boppan 10
import timeit
11
import urllib.request
12
import subprocess
9322 Boppan 13
from threading import Thread
9249 Boppan 14
 
15
sys.path.append('test')
16
import common
17
 
18
def log(s, end = "\n"):
19
    print(s, end = end, flush = True)
20
 
21
def execute(s, mute = False):
22
    mute = ">/dev/null" if mute else ""
23
    code = os.system(f"{s}{mute}")
24
    if code:
25
        print(f"Command returned {code}: \"{s}\"")
26
        exit(-1)
27
 
28
def stage(name, command, mute = False):
29
    print(f"{name}... ", end = "")
30
    execute(command, mute = mute)
31
    print("Done.")
32
 
9314 Boppan 33
def download(link, path):
34
    log(f"Downloading {path}... ", end = "")
9315 Boppan 35
    urllib.request.urlretrieve(link, path)
9314 Boppan 36
    log("Done.")
37
 
9310 Boppan 38
def tool_exists(name):
39
    assert(type(name) == str)
40
    return which(name) != None
41
 
42
def check_tools(tools):
43
    assert(type(tools) == tuple)
44
    for name_package_pair in tools:
45
        assert(type(name_package_pair) == tuple)
46
        assert(len(name_package_pair) == 2)
47
        assert(type(name_package_pair[0]) == str)
48
        assert(type(name_package_pair[1]) == str)
49
 
50
    not_exists = []
51
    for name, package in tools:
52
        if not tool_exists(name):
53
            not_exists.append((name, package))
54
    if len(not_exists) != 0:
55
        log("Sorry, I can't find some tools:")
9311 Boppan 56
 
57
        header_name = "Name"
58
        header_package = "Package (probably)"
59
 
60
        max_name_len = len(header_name)
61
        max_package_name_len = len(header_package)
9310 Boppan 62
        for name, package in not_exists:
63
            if len(package) > max_package_name_len:
64
                max_package_name_len = len(package)
65
            if len(name) > max_name_len:
66
                max_name_len = len(name)
67
 
68
        def draw_row(name, package):
69
            log(f" | {name.ljust(max_name_len)} | {package.ljust(max_package_name_len)} |")
70
 
71
        def draw_line():
72
            log(f" +-{'-' * max_name_len}-+-{'-' * max_package_name_len}-+")
73
 
74
        draw_line()
9311 Boppan 75
        draw_row(header_name, header_package)
9310 Boppan 76
        draw_line()
77
        for name, package in not_exists:
78
            draw_row(name, package)
79
        draw_line()
80
        exit()
81
 
9321 Boppan 82
def prepare_test_img():
83
    # TODO: Always recompile the kernel (after build system is done?)
9313 Boppan 84
    # Get IMG
85
    if not os.path.exists("kolibri_test.img"):
86
        if len(sys.argv) == 1:
9314 Boppan 87
            download("http://builds.kolibrios.org/eng/data/data/kolibri.img", "kolibri_test.img")
9313 Boppan 88
        else:
89
            builds_eng = sys.argv[1]
90
            execute(f"cp {builds_eng}/data/data/kolibri.img kolibri_test.img")
91
 
9316 Boppan 92
    # Open the IMG
93
    with open("kolibri_test.img", "rb") as img:
94
        img_data = img.read()
95
    img = common.Floppy(img_data)
9321 Boppan 96
 
9316 Boppan 97
    # Remove unuseful folders
98
    img.delete_path("GAMES")
99
    img.delete_path("DEMOS")
100
    img.delete_path("3D")
9313 Boppan 101
 
102
    # Get test kernel
103
    if not os.path.exists("kernel.mnt.pretest"):
104
        if len(sys.argv) == 1:
105
            with open("lang.inc", "w") as lang_inc:
106
                lang_inc.write("lang fix en\n")
107
            execute("fasm bootbios.asm bootbios.bin.pretest -dpretest_build=1")
108
            execute("fasm -m 65536 kernel.asm kernel.mnt.pretest -dpretest_build=1 -ddebug_com_base=0xe9")
109
        else:
110
            builds_eng = sys.argv[1]
111
            execute(f"cp {builds_eng}/data/kernel/trunk/kernel.mnt.pretest kernel.mnt.pretest", mute = True)
112
 
113
    # Put the kernel into IMG
9316 Boppan 114
    with open("kernel.mnt.pretest", "rb") as kernel_mnt_pretest:
115
        kernel_mnt_pretest_data = kernel_mnt_pretest.read()
9317 Boppan 116
    img.add_file_path("KERNEL.MNT", kernel_mnt_pretest_data)
9316 Boppan 117
    img.save("kolibri_test.img")
9321 Boppan 118
 
119
def collect_tests():
120
    tests = []
121
 
9313 Boppan 122
    # Collect tests from test folder (not recursively yet)
123
    for test_folder in os.listdir("test"):
124
        test_folder_path = f"test/{test_folder}"
125
        test_file = f"{test_folder_path}/test.py"
9321 Boppan 126
 
9313 Boppan 127
        if not os.path.isdir(test_folder_path):
128
            continue
9321 Boppan 129
 
9313 Boppan 130
        if os.path.exists(test_file):
131
            tests.append(test_folder_path)
9321 Boppan 132
    return tests
133
 
9322 Boppan 134
def run_tests_serially(tests, root_dir):
9313 Boppan 135
    test_number = 1
136
    for test in tests:
137
        test_dir = f"{root_dir}/{test}"
138
 
139
        os.chdir(test_dir)
140
        print(f"[{test_number}/{len(tests)}] {test}... ", end = "", flush=True)
141
        start = timeit.default_timer()
142
        try:
143
            SourceFileLoader("test", "test.py").load_module().run()
144
        except common.TestTimeoutException:
145
            result = "TIMEOUT"
146
        except common.TestFailureException:
147
            result = "FAILURE"
148
        else:
149
            result = "SUCCESS"
150
        finish = timeit.default_timer()
151
        print(f"{result} ({finish - start:.2f} seconds)")
152
        os.chdir(root_dir)
153
 
154
        test_number += 1
9322 Boppan 155
 
156
if __name__ == "__main__":
157
    root_dir = os.getcwd()
158
 
159
    # Check available tools
160
    tools = (("qemu-system-i386", "qemu-system-x86"),
161
             ("fasm", "fasm"))
162
    check_tools(tools)
9313 Boppan 163
 
9322 Boppan 164
    prepare_test_img()
165
    tests = collect_tests()
166
    run_tests_serially(tests, root_dir)