-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.py
132 lines (105 loc) · 3.79 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
import os
import sys
import argparse
import subprocess
import datetime
from sys import platform
# Global variables
build_dir = "build/"
install_dir = "_install/"
build_commands = ["cmake", "--build", ".", "--config", "Release", "--parallel"]
test_arguments = ["-test-frames", "100"]
def header(name):
"""Print a header with a given name."""
print("")
print("***************************************************************************")
print(f" {name}")
print("***************************************************************************")
def create_directory_if_not_exists(directory):
"""Create a directory if it does not exist."""
if not os.path.exists(directory):
os.makedirs(directory)
def run_cmake(commands):
"""Run CMake commands."""
try:
subprocess.run(commands, check=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred while running CMake: {e}")
def build():
"""Build the project."""
header("BUILDING PROJECT")
# Create build and install directories if they do not exist
create_directory_if_not_exists(build_dir)
create_directory_if_not_exists(install_dir)
# Change to the build directory
os.chdir(build_dir)
# Run CMake to generate build files
run_cmake(["cmake", ".."])
# Call cmake to build the release config in parallel
run_cmake(build_commands)
# Change back to the original directory
os.chdir("..")
# Call cmake to install
run_cmake(["cmake", "--install", "build", "--prefix", install_dir])
def print_log_result(log_file):
# Print last 3 lines of log file
if os.path.exists(log_file):
with open(log_file, "r") as file:
lines = file.readlines()
print("Results:")
for line in lines[-3:]:
print(line.strip())
def test():
"""Run tests on the built executables."""
print("Executing test function")
test_dir = os.path.join(install_dir, "bin_x64/")
# Check if test directory exists
if not os.path.exists(test_dir):
print(f"Test directory '{test_dir}' does not exist.")
return
current_dir = os.getcwd()
os.chdir(test_dir)
# Find all executables in the test directory
executables = [
f
for f in os.listdir(".")
if os.path.isfile(os.path.join(".", f)) and (f.endswith(".exe") or f.endswith("_app"))
]
names_to_avoid = []
# Call each executable with options --test and --screenshot
returncode = 0
for executable in executables:
executable_path = os.path.join(".", executable)
args = [executable_path]
if not any(name in executable for name in names_to_avoid):
args += ["-test"]
args += test_arguments
args += ["-screenshot", "snap_" + executable[:-4] + ".jpg"]
try:
header(f"Testing '{executable}'")
subprocess.run(args, check=True)
log_file = "log_" + executable[:-4] + ".txt"
print_log_result(log_file)
except subprocess.CalledProcessError as e:
print(f"Error occurred while testing: {e}")
returncode = e.returncode
os.chdir(current_dir)
if returncode != 0:
sys.exit(1)
if __name__ == "__main__":
# Create the parser
parser = argparse.ArgumentParser()
# Add the command line options
parser.add_argument("--build", action="store_true", help="Execute build function")
parser.add_argument("--test", action="store_true", help="Execute test function")
# Parse the command line arguments
args = parser.parse_args()
if args.build:
build()
if args.test:
test()
# If no arguments provided, call all functions
if not any(vars(args).values()):
build()
test()