Subversion Repositories Kolibri OS

Rev

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