Adapt all python code to PEP8 style standards

Signed-off-by: Kamil Lepek <kamil.lepek94@gmail.com>
This commit is contained in:
Kamil Lepek
2019-06-10 15:49:15 +02:00
parent 1e2b8f1980
commit e52d34c1c8
15 changed files with 822 additions and 765 deletions

View File

@@ -11,167 +11,169 @@ import os
import sys
import textwrap
class TestGenerator(object):
main_UT_dir = ""
main_tested_dir = ""
tested_file_path = ""
tested_function_name = ""
main_UT_dir = ""
main_tested_dir = ""
tested_file_path = ""
tested_function_name = ""
def __init__(self, main_UT_dir, main_tested_dir, file_path, func_name):
self.set_main_UT_dir(main_UT_dir)
self.set_main_tested_dir(main_tested_dir)
self.set_tested_file_path(file_path)
self.tested_function_name = func_name
def __init__(self, main_UT_dir, main_tested_dir, file_path, func_name):
self.set_main_UT_dir(main_UT_dir)
self.set_main_tested_dir(main_tested_dir)
self.set_tested_file_path(file_path)
self.tested_function_name = func_name
def create_empty_test_file(self):
dst_dir = os.path.dirname(self.get_tested_file_path()[::-1])[::-1]
def create_empty_test_file(self):
dst_dir = os.path.dirname(self.get_tested_file_path()[::-1])[::-1]
self.create_dir_if_not_exist(self.get_main_UT_dir() + dst_dir)
test_file_name = os.path.basename(self.get_tested_file_path())
self.create_dir_if_not_exist(self.get_main_UT_dir() + dst_dir)
test_file_name = os.path.basename(self.get_tested_file_path())
dst_path = self.get_main_UT_dir() + dst_dir + "/" + test_file_name
dst_path = self.get_main_UT_dir() + dst_dir + "/" + test_file_name
no_str = ""
no = 0
while True:
if not os.path.isfile(dst_path.rsplit(".", 1)[0] + no_str + "." + dst_path.rsplit(".", 1)[1]):
break
no += 1
no_str = str(no)
no_str = ""
no = 0
while True:
if not os.path.isfile("{0}{1}.{2}".format(dst_path.rsplit(".", 1)[0], no_str,
dst_path.rsplit(".", 1)[1])):
break
no += 1
no_str = str(no)
dst_path = dst_path.rsplit(".", 1)[0] + no_str + "." + dst_path.rsplit(".", 1)[1]
buf = self.get_markups()
buf += "#undef static\n\n"
buf += "#undef inline\n\n"
buf += self.get_UT_includes()
buf += self.get_includes(self.get_main_tested_dir() + self.get_tested_file_path())
buf += self.get_autowrap_file_include(dst_path)
buf += self.get_empty_test_function()
buf += self.get_test_main()
dst_path = dst_path.rsplit(".", 1)[0] + no_str + "." + dst_path.rsplit(".", 1)[1]
buf = self.get_markups()
buf += "#undef static\n\n"
buf += "#undef inline\n\n"
buf += self.get_UT_includes()
buf += self.get_includes(self.get_main_tested_dir() + self.get_tested_file_path())
buf += self.get_autowrap_file_include(dst_path)
buf += self.get_empty_test_function()
buf += self.get_test_main()
with open(dst_path, "w") as f:
f.writelines(buf)
with open(dst_path, "w") as f:
f.writelines(buf)
print(f"{dst_path} generated successfully!")
print(f"{dst_path} generated successfully!")
def get_markups(self):
ret = "/*\n"
ret += " * <tested_file_path>" + self.get_tested_file_path() + "</tested_file_path>\n"
ret += " * <tested_function>" + self.get_tested_function_name() + "</tested_function>\n"
ret += " * <functions_to_leave>\n"
ret += " *\tINSERT HERE LIST OF FUNCTIONS YOU WANT TO LEAVE\n"
ret += " *\tONE FUNCTION PER LINE\n"
ret += " * </functions_to_leave>\n"
ret += " */\n\n"
def get_markups(self):
ret = "/*\n"
ret += " * <tested_file_path>" + self.get_tested_file_path() + "</tested_file_path>\n"
ret += " * <tested_function>" + self.get_tested_function_name() + "</tested_function>\n"
ret += " * <functions_to_leave>\n"
ret += " *\tINSERT HERE LIST OF FUNCTIONS YOU WANT TO LEAVE\n"
ret += " *\tONE FUNCTION PER LINE\n"
ret += " * </functions_to_leave>\n"
ret += " */\n\n"
return ret
return ret
def create_dir_if_not_exist(self, path):
if not os.path.isdir(path):
try:
os.makedirs(path)
except Exception:
pass
return True
return None
def create_dir_if_not_exist(self, path):
if not os.path.isdir(path):
try:
os.makedirs(path)
except Exception:
pass
return True
return None
def get_UT_includes(self):
ret = '''
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "print_desc.h"\n\n'''
def get_UT_includes(self):
ret = '''
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "print_desc.h"\n\n'''
return textwrap.dedent(ret)
return textwrap.dedent(ret)
def get_autowrap_file_include(self, test_file_path):
autowrap_file = test_file_path.rsplit(".", 1)[0]
autowrap_file = autowrap_file.replace(self.main_UT_dir, "")
autowrap_file += "_generated_warps.c"
return "#include \"" + autowrap_file + "\"\n\n"
def get_autowrap_file_include(self, test_file_path):
autowrap_file = test_file_path.rsplit(".", 1)[0]
autowrap_file = autowrap_file.replace(self.main_UT_dir, "")
autowrap_file += "_generated_warps.c"
return "#include \"" + autowrap_file + "\"\n\n"
def get_includes(self, abs_path_to_tested_file):
with open(abs_path_to_tested_file, "r") as f:
code = f.readlines()
def get_includes(self, abs_path_to_tested_file):
with open(abs_path_to_tested_file, "r") as f:
code = f.readlines()
ret = [line for line in code if re.search(r'#include', line)]
ret = [line for line in code if re.search(r'#include', line)]
return "".join(ret) + "\n"
return "".join(ret) + "\n"
def get_empty_test_function(self):
ret = "static void " + self.get_tested_function_name() + "_test01(void **state)\n"
ret += "{\n"
ret += "\tprint_test_description(\"Put test description here\");\n"
ret += "\tassert_int_equal(1,1);\n"
ret += "}\n\n"
def get_empty_test_function(self):
ret = "static void " + self.get_tested_function_name() + "_test01(void **state)\n"
ret += "{\n"
ret += "\tprint_test_description(\"Put test description here\");\n"
ret += "\tassert_int_equal(1,1);\n"
ret += "}\n\n"
return ret
return ret
def get_test_main(self):
ret = "int main(void)\n"
ret += "{\n"
ret += "\tconst struct CMUnitTest tests[] = {\n"
ret += "\t\tcmocka_unit_test(" + self.get_tested_function_name() + "_test01)\n"
ret += "\t};\n\n"
ret += "\tprint_message(\"Unit test of " + self.get_tested_file_path() + "\");\n\n"
ret += "\treturn cmocka_run_group_tests(tests, NULL, NULL);\n"
ret += "}"
def get_test_main(self):
ret = "int main(void)\n"
ret += "{\n"
ret += "\tconst struct CMUnitTest tests[] = {\n"
ret += "\t\tcmocka_unit_test(" + self.get_tested_function_name() + "_test01)\n"
ret += "\t};\n\n"
ret += "\tprint_message(\"Unit test of " + self.get_tested_file_path() + "\");\n\n"
ret += "\treturn cmocka_run_group_tests(tests, NULL, NULL);\n"
ret += "}"
return ret
return ret
def set_tested_file_path(self, path):
call_dir = os.getcwd() + os.sep
p = os.path.normpath(call_dir + path)
def set_tested_file_path(self, path):
call_dir = os.getcwd() + os.sep
p = os.path.normpath(call_dir + path)
if os.path.isfile(p):
self.tested_file_path = p.split(self.get_main_tested_dir(), 1)[1]
return
elif os.path.isfile(self.get_main_tested_dir() + path):
self.tested_file_path = path
return
if os.path.isfile(p):
self.tested_file_path = p.split(self.get_main_tested_dir(), 1)[1]
return
elif os.path.isfile(self.get_main_tested_dir() + path):
self.tested_file_path = path
return
print(f"{os.path.join(self.get_main_tested_dir(), path)}")
print("Given path not exists!")
exit(1)
print(f"{os.path.join(self.get_main_tested_dir(), path)}")
print("Given path not exists!")
exit(1)
def set_main_UT_dir(self, path):
p = os.path.dirname(os.path.realpath(__file__)) + os.sep + path
p = os.path.normpath(os.path.dirname(p)) + os.sep
self.main_UT_dir = p
def get_main_UT_dir(self):
return self.main_UT_dir
def set_main_UT_dir(self, path):
p = os.path.dirname(os.path.realpath(__file__)) + os.sep + path
p = os.path.normpath(os.path.dirname(p)) + os.sep
self.main_UT_dir = p
def set_main_tested_dir(self, path):
p = os.path.dirname(os.path.realpath(__file__)) + os.sep + path
p = os.path.normpath(os.path.dirname(p)) + os.sep
self.main_tested_dir = p
def get_main_UT_dir(self):
return self.main_UT_dir
def get_main_tested_dir(self):
return self.main_tested_dir
def set_main_tested_dir(self, path):
p = os.path.dirname(os.path.realpath(__file__)) + os.sep + path
p = os.path.normpath(os.path.dirname(p)) + os.sep
self.main_tested_dir = p
def get_tested_file_path(self):
return self.tested_file_path
def get_main_tested_dir(self):
return self.main_tested_dir
def get_tested_function_name(self):
return self.tested_function_name
def get_tested_file_path(self):
return self.tested_file_path
def get_tested_function_name(self):
return self.tested_function_name
def __main__():
if len(sys.argv) < 3:
print("No path to tested file or tested function name given !")
sys.exit(1)
if len(sys.argv) < 3:
print("No path to tested file or tested function name given !")
sys.exit(1)
tested_file_path = sys.argv[1]
tested_function_name = sys.argv[2]
tested_file_path = sys.argv[1]
tested_function_name = sys.argv[2]
generator = TestGenerator(tests_config.MAIN_DIRECTORY_OF_UNIT_TESTS,\
tests_config.MAIN_DIRECTORY_OF_TESTED_PROJECT,\
tested_file_path, tested_function_name)
generator = TestGenerator(tests_config.MAIN_DIRECTORY_OF_UNIT_TESTS,
tests_config.MAIN_DIRECTORY_OF_TESTED_PROJECT,
tested_file_path, tested_function_name)
generator.create_empty_test_file()
generator.create_empty_test_file()
if __name__ == "__main__":
__main__()
__main__()

File diff suppressed because it is too large Load Diff

View File

@@ -10,13 +10,15 @@ import os
import sys
import subprocess
def run_command(args):
result = subprocess.run(" ".join(args), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result.stdout = result.stdout.decode("ASCII", errors='ignore')
result.stderr = result.stderr.decode("ASCII", errors='ignore')
return result
script_path = os.path.dirname(os.path.realpath(__file__))
main_UT_dir = os.path.join(script_path, tests_config.MAIN_DIRECTORY_OF_UNIT_TESTS)
@@ -29,13 +31,13 @@ if not os.path.isdir(os.path.join(main_UT_dir, "ocf_env", "ocf")):
except Exception:
raise Exception("Cannot create ocf_env/ocf directory!")
result = run_command([ "cp", "-r",
os.path.join(main_tested_dir, "inc", "*"),
os.path.join(main_UT_dir, "ocf_env", "ocf") ])
result = run_command(["cp", "-r",
os.path.join(main_tested_dir, "inc", "*"),
os.path.join(main_UT_dir, "ocf_env", "ocf")])
if result.returncode != 0:
raise Exception("Preparing sources for testing failed!")
result = run_command([ os.path.join(script_path, "prepare_sources_for_testing.py") ])
result = run_command([os.path.join(script_path, "prepare_sources_for_testing.py")])
if result.returncode != 0:
raise Exception("Preparing sources for testing failed!")
@@ -52,7 +54,7 @@ except Exception:
os.chdir(build_dir)
cmake_result = run_command([ "cmake", ".." ])
cmake_result = run_command(["cmake", ".."])
print(cmake_result.stdout)
with open(os.path.join(logs_dir, "cmake.output"), "w") as f:
@@ -64,20 +66,20 @@ if cmake_result.returncode != 0:
f.write("Cmake step failed! More details in cmake.output.")
sys.exit(1)
make_result = run_command([ "make", "-j" ])
make_result = run_command(["make", "-j"])
print(make_result.stdout)
with open(os.path.join(logs_dir, "make.output"), "w") as f:
f.write(make_result.stdout)
f.write(make_result.stderr)
f.write(make_result.stdout)
f.write(make_result.stderr)
if make_result.returncode != 0:
with open(os.path.join(logs_dir, "tests.output"), "w") as f:
f.write("Make step failed! More details in make.output.")
sys.exit(1)
with open(os.path.join(logs_dir, "tests.output"), "w") as f:
f.write("Make step failed! More details in make.output.")
sys.exit(1)
test_result = run_command([ "make", "test" ])
test_result = run_command(["make", "test"])
print(test_result.stdout)
with open(os.path.join(logs_dir , "tests.output"), "w") as f:
f.write(test_result.stdout)
with open(os.path.join(logs_dir, "tests.output"), "w") as f:
f.write(test_result.stdout)

View File

@@ -11,25 +11,34 @@ MAIN_DIRECTORY_OF_TESTED_PROJECT = "../../../"
MAIN_DIRECTORY_OF_UNIT_TESTS = "../tests/"
# Paths to all directories, in which tests are stored. All paths should be relative to MAIN_DIRECTORY_OF_UNIT_TESTS
DIRECTORIES_WITH_TESTS_LIST = ["cleaning/", "metadata/", "mngt/", "concurrency/", "engine/", "eviction/", "utils/"]
# Paths to all directories, in which tests are stored. All paths should be relative to
# MAIN_DIRECTORY_OF_UNIT_TESTS
DIRECTORIES_WITH_TESTS_LIST = ["cleaning/", "metadata/", "mngt/", "concurrency/", "engine/",
"eviction/", "utils/"]
# Paths to all directories containing files with sources. All paths should be relative to MAIN_DIRECTORY_OF_TESTED_PROJECT
DIRECTORIES_TO_INCLUDE_FROM_PROJECT_LIST = ["src/", "src/cleaning/", "src/engine/", "src/metadata/", "src/eviction/", "src/mngt/", "src/concurrency/", "src/utils/", "inc/"]
# Paths to all directories containing files with sources. All paths should be relative to
# MAIN_DIRECTORY_OF_TESTED_PROJECT
DIRECTORIES_TO_INCLUDE_FROM_PROJECT_LIST = ["src/", "src/cleaning/", "src/engine/", "src/metadata/",
"src/eviction/", "src/mngt/", "src/concurrency/",
"src/utils/", "inc/"]
# Paths to all directories from directory with tests, which should also be included
DIRECTORIES_TO_INCLUDE_FROM_UT_LIST = ["ocf_env/"]
# Paths to include, required by cmake, cmocka, cunit
FRAMEWORK_DIRECTORIES_TO_INCLUDE_LIST = ["${CMOCKA_PUBLIC_INCLUDE_DIRS}" ,"${CMAKE_BINARY_DIR}", "${CMAKE_CURRENT_SOURCE_DIR}"]
FRAMEWORK_DIRECTORIES_TO_INCLUDE_LIST = ["${CMOCKA_PUBLIC_INCLUDE_DIRS}", "${CMAKE_BINARY_DIR}",
"${CMAKE_CURRENT_SOURCE_DIR}"]
# Path to directory containing all sources after preprocessing. Should be relative to MAIN_DIRECTORY_OF_UNIT_TESTS
# Path to directory containing all sources after preprocessing. Should be relative to
# MAIN_DIRECTORY_OF_UNIT_TESTS
PREPROCESSED_SOURCES_REPOSITORY = "preprocessed_sources_repository/"
# Path to directory containing all sources after removing unneeded functions and cmake files for tests
# Path to directory containing all sources after removing unneeded functions and cmake files for
# tests
SOURCES_TO_TEST_REPOSITORY = "sources_to_test_repository/"
# List of includes. Directories will be recursively copied to given destinations in directory with tests.
# List of includes.
# Directories will be recursively copied to given destinations in directory with tests.
# key - destination in dir with tests
# value - path in tested project to dir which should be copied
INCLUDES_TO_COPY_DICT = { 'ocf_env/ocf/' : "inc/" }
INCLUDES_TO_COPY_DICT = {'ocf_env/ocf/': "inc/"}