-
Notifications
You must be signed in to change notification settings - Fork 0
/
selftest.py
212 lines (143 loc) · 7.05 KB
/
selftest.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#---------------------------------------------------------------------------------------------------#
# File name: selftest.py #
# Autor: Chrissi2802 #
# Created on: 22.04.2021 #
#---------------------------------------------------------------------------------------------------#
# This file provides a class to check python files.
# Exact description in the class and methods.
import os
import platform
class Selftest():
"""This class is used to check python files.
"""
def __init__(self, skip_file_list = []):
"""Initialisation of the class (constructor). Executes the checks.
Args:
skip_file_list (list, optional): Files to be skipped. Defaults to [].
"""
self.return_value_io = 0
self.return_value_nio = 1
self.skip_file_list = skip_file_list
self.skip_file_list_big = self.skip_file_list + ["main.py", str(__file__).split("\\")[-1]]
self.__identify_operation_system()
self.run()
def __identify_operation_system(self):
"""This method identifies the operating system in use.
"""
if (platform.system().find("Windows") < 0):
self.python_command_name = "python3" # Linux or Mac
else:
self.python_command_name = "python" # Windows
def __create_file_list(self):
"""This method creates a list of all Python files in this folder.
"""
self.file_list = [file for file in os.listdir() if file.endswith(".py")]
i = 0
# Run through all python files
while (i < len(self.file_list)):
delete_file = False
# Run through all files that should be skipped
for j in range(len(self.skip_file_list_temp)):
if (self.skip_file_list_temp[j] == self.file_list[i]):
delete_file = True
if (delete_file == True):
del(self.file_list[i])
else:
i += 1
def __run_through_file_list(self, function, skip_file_list_temp):
"""This method traverses the file list and executes the passed function on these files. Exceptions can be passed.
Args:
function (method): Check method to be executed
skip_file_list_temp (list): Files to be skipped
"""
self.skip_file_list_temp = skip_file_list_temp
self.__create_file_list()
# Run through all files
for file in self.file_list:
print(function.__name__, "checks", file)
return_value = function(file)
assert (return_value == self.return_value_io), "Error in: " + file
def __check_pyflakes(self, file):
"""This method checks Python source files for errors.
Args:
file (string): File that will be checked
Returns:
return_value (integer): Return value, is 0 if everything is ok
"""
return_value = os.system("pyflakes " + file)
return return_value
def __check_vulture(self, file):
"""This method finds unused code.
Args:
file (string): File that will be checked
Returns:
return_value (integer): Return value, is 0 if everything is ok
"""
return_value = os.system("vulture " + file + " --min-confidence 80")
return return_value
def __check_file_imported_somewhere(self, file):
"""This method checks if the files were imported into one of the other files.
Args:
file (string): File that will be checked
Returns:
return_value (integer): Return value, is 0 if everything is ok
"""
file_name, file_extension = os.path.splitext(file)
return_value = self.return_value_nio
# Run through all files
for file_temp in self.file_list:
current_file = open(file_temp, "r") # Read file
# Run through all rows
for row in current_file:
if (("import" in row) and (file_name in row)):
return_value = self.return_value_io
current_file.close()
if (return_value == self.return_value_nio):
print(file, "was not imported anywhere")
return return_value
def __check_asserts(self, file):
"""This method checks if asserts are present in the main block "if (__name__ == "__main__"):".
Args:
file (string): File that will be checked
Returns:
return_value (integer): Return value, is 0 if everything is ok
"""
current_file = open(file, "r") # Read file
test_code = False
return_value = self.return_value_nio
# Run through all rows
for row in current_file:
if (("if" in row) and ("__name__" in row) and ("__main__" in row)):
test_code = True
if ((test_code == True) and ("assert" in row)):
return_value = self.return_value_io
current_file.close()
if (return_value == self.return_value_nio):
if (test_code == False):
print("No test code found in", file)
else:
print("No asserts found in", file)
return return_value
def __check_main_block(self, file):
"""This method executes from the called file, the code that is written at "if (__name__ == "__main__"):".
Args:
file (string): File that will be checked
Returns:
return_value (integer): Return value, is 0 if everything is ok
"""
return_value = os.system(self.python_command_name + " " + file)
return return_value
def run(self):
"""This method executes the checks.
"""
print("The check starts ...")
self.__run_through_file_list(self.__check_pyflakes, self.skip_file_list)
self.__run_through_file_list(self.__check_vulture, self.skip_file_list)
self.__run_through_file_list(self.__check_file_imported_somewhere , self.skip_file_list_big)
self.__run_through_file_list(self.__check_asserts , self.skip_file_list_big)
self.__run_through_file_list(self.__check_main_block, self.skip_file_list_big)
print("---------------------------------------------------")
print(" All checks successfully executed! ")
print("---------------------------------------------------")
if (__name__ == "__main__"):
CSelftest = Selftest()