Cleanup of some tests and base tests methods

This commit is contained in:
Maximilien Rigaut 2022-06-27 04:01:40 +02:00
parent 335d7fc967
commit c8292f498b
No known key found for this signature in database
GPG Key ID: 4F14B23709ADA5B1
7 changed files with 8931 additions and 59 deletions

8842
databases/windows_base.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@ import discordHelper
import time
# All modules must be loaded before the main program is run.
from modules import * # noqa # Set for pyinstaller
from modules import test_size, test_adler, test_sha, test_spidey, test_line_count # noqa # Set for pyinstaller
if __name__ == "__main__":
try:
@ -26,7 +26,7 @@ if __name__ == "__main__":
# Will always try to load discordHelper/discordHelper.json
run_db = os.path.join(directory_path, "test_results_found.json")
argv = rf"--all --autodetect --launch --continue --timeout 25 {db_path}--gen-data {run_db} --db discordHelper/test_results.json".split(" ") # noqa E501
argv = rf"--all --autodetect --printrate 2 --launch --continue --timeout 25 {db_path}--gen-data {run_db} --db discordHelper/test_results.json".split(" ") # noqa E501
# print(" > Starting the discordHelper with the following arguments:")
# print(" > " + " ".join(argv))

0
modules/__init__.py Normal file
View File

View File

@ -27,7 +27,7 @@ def parse(_args):
parser.add_argument("--gen-data", "--generate-data", "--gen", nargs=1, default="", help="Generate data for the analysis")
parser.add_argument("--database", "--db", type=str, help="Alternative database for the analysis")
parser.add_argument("--pollrate", "--poll", default=0.1, type=float, help="Refresh rate of the status, 0.1 is the default")
parser.add_argument("--printrate", "-refresh", default=5, type=int, help="Print the tests every X seconds")
parser.add_argument("--printrate", "--refresh", default=5, type=float, help="Print the tests every X seconds")
parser.add_argument("--size", "--window-size", default=110, type=int, help="Size of the message window")
parser.add_argument("--max-shown", default=5, type=int, help="Maximum number of errors shown")

View File

@ -197,7 +197,12 @@ class TestRunner:
self.bad_database = True
else:
for test_name in self.selected_tests:
if test_name not in self.base_data["tests"]:
found_one_value = False
for file_name, test_names in self.base_data["tests"].items():
if test_name in test_names:
found_one_value = True
break # At least one test is in the database
if not found_one_value:
print(colored(f" > The database is missing the test {test_name}", "red"))
self.bad_database = True
@ -456,11 +461,12 @@ def print_status(status, timer_start):
else:
mon, sec = divmod(ellapsed_time * 100 / status.progress, 60)
hr, mon = divmod(mon, 60)
estimated_time = "%d:%02d:%02d" % (hr, mon, sec)
sec = round(sec, 0) +1
estimated_time = f"{hr:n}:{mon:02n}:{sec:02.0n}"
print(colored(
f" > Overall progress: {status.progress}%"
f" in {time.time() - timer_start:.2f} seconds"
f" estimaded time ({estimated_time})", color
f" estimaded total time ({estimated_time})", color
))

View File

@ -48,7 +48,7 @@ class SpideyTest(test_template.TestTemplate):
"If you've recently downloaded a .exe directly sent from someone, "
"do also consider a clean windows reinstall."
)
self.add_to_new_data("global_tests", "Spidey", "passed")
self.progress = 100
return self.finish("success", "Your discord is not infected.")

View File

@ -167,6 +167,54 @@ class TestTemplate:
if self.status_code == "running":
return self.finish("skipped")
def add_to_new_data(self, entry, testname, value):
"""
Adds a new entry to the database.
:param entry: The entry to add
:type entry: str
:param testname: The name of the test
:type testname: str
:param value: The value of the entry
:type value: str
"""
if entry not in self.new_data:
self.new_data[entry] = {}
self.new_data[entry][testname] = value
return value
def get_from_new_data(self, entry, testname):
"""
Gets the value of an entry from the database.
:param entry: The entry to get
:type entry: str
:param testname: The name of the test
:type testname: str
:return: The value of the entry
:rtype: str
"""
if entry in self.new_data:
if testname in self.new_data[entry]:
return self.new_data[entry][testname]
return None
def get_from_database(self, entry, testname):
"""
Gets the value of an entry from the database.
:param entry: The entry to get
:type entry: str
:param testname: The name of the test
:type testname: str
:return: The value of the entry
:rtype: str
"""
if self.test_data:
path = self.agnostic_path.get_short_path(entry)
if "tests" in self.test_data:
if entry in self.test_data["tests"]:
if testname in self.test_data["tests"][entry]:
return self.test_data["tests"][entry][testname]
return None
class TestWalkTemplate(TestTemplate):
def __init__(self, thread_parameters):
@ -176,57 +224,6 @@ class TestWalkTemplate(TestTemplate):
super().__init__(thread_parameters)
self.to_skip = []
def add_test_result(self, path, testname, result):
"""
Adds a test result to the data dictionary.
:param path: Path to the file that was tested
:type path: str
:param testname: Name of the test
:type testname: str
:param result: Result of the test
:type result: any
"""
path = self.agnostic_path.get_short_path(path)
if path not in self.new_data:
self.new_data[path] = {}
self.new_data[path][testname] = result
return result
def get_test_data(self, path, testname):
"""
Gets the test data from the data dictionary.
:param path: Path to the file that was tested
:type path: str
:param testname: Name of the test
:type testname: str
:return: Test data
:rtype: any
"""
path = self.agnostic_path.get_short_path(path)
if path in self.new_data:
if testname in self.new_data[path]:
return self.new_data[path][testname]
return None
def get_expected_result(self, path, testname):
"""
Gets the expected result from the data dictionary.
:param path: Path to the file that was tested
:type path: str
:param testname: Name of the test
:type testname: str
:return: Expected result
:rtype: any
"""
if self.test_data:
path = self.agnostic_path.get_short_path(path)
if "tests" in self.test_data:
if path in self.test_data["tests"]:
if testname in self.test_data["tests"][path]:
return self.test_data["tests"][path][testname]
return None
def run_test(self):
"""
The main runner function, must be implemented by the child class.
@ -256,10 +253,37 @@ class TestWalkTemplate(TestTemplate):
"""
Walks the given path and returns a list of all files and directories.
:return: List of all files
:return:
"""
return self.agnostic_path.all_files
def add_test_result(self, path: str, testname: str, result: any):
"""
Adds a test result to the data dictionary.
:param path: Path to the file being tested
:param testname: Name of the test
:param result: Result of the test
"""
path = self.agnostic_path.get_short_path(path)
return self.add_to_new_data(path, testname, result)
def get_test_data(self, path, testname):
"""
Gets the test data from the data dictionary.
:param path: Path to the file being tested
:param testname: Name of the test
"""
return self.get_from_new_data(self.agnostic_path.get_short_path(path), testname)
def get_expected_result(self, path, testname):
"""
Gets the expected result from the data dictionary.
:param path: Path to the file being tested
:param testname: Name of the test
"""
if self.test_data:
return self.get_from_database(self.agnostic_path.get_short_path(path), testname)
return None
def compare(self, path, entry_name, function, args, kwargs, cmp_function=None):
"""
Use the function on the given path, with given arguments and compare it with the entry in the test data.