From a671a282828b91d363d15ab77ba4876ee2cc10fa Mon Sep 17 00:00:00 2001 From: Robert Baldyga Date: Tue, 21 May 2019 15:07:02 +0200 Subject: [PATCH] Convert UT scripts to python3 Signed-off-by: Robert Baldyga --- tests/unit/framework/add_new_test_file.py | 6 +- .../framework/prepare_sources_for_testing.py | 76 +++++++------- tests/unit/framework/run_unit_tests.py | 99 ++++++++++--------- tests/unit/framework/tests_config.py | 2 +- tests/unit/tests/add_new_test_file.py | 4 +- 5 files changed, 100 insertions(+), 87 deletions(-) diff --git a/tests/unit/framework/add_new_test_file.py b/tests/unit/framework/add_new_test_file.py index fb2d989..ac0e25e 100755 --- a/tests/unit/framework/add_new_test_file.py +++ b/tests/unit/framework/add_new_test_file.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # # Copyright(c) 2012-2018 Intel Corporation @@ -125,8 +125,8 @@ class TestGenerator(object): self.tested_file_path = path return - print self.get_main_tested_dir() + path - print "Given path not exists!" + print(os.path.join(self.get_main_tested_dir(), path)) + print("Given path not exists!") exit(1) diff --git a/tests/unit/framework/prepare_sources_for_testing.py b/tests/unit/framework/prepare_sources_for_testing.py index f823869..33f540c 100755 --- a/tests/unit/framework/prepare_sources_for_testing.py +++ b/tests/unit/framework/prepare_sources_for_testing.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # # Copyright(c) 2012-2018 Intel Corporation @@ -8,9 +8,17 @@ import shutil import sys import re -import commands import os.path from collections import defaultdict +import subprocess + +def run_command(args): + result = subprocess.run(" ".join(args), shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + result.stdout = result.stdout.decode("ASCII") + result.stderr = result.stderr.decode("ASCII") + print(result.stderr) + return result import tests_config # @@ -97,26 +105,25 @@ class UnitTestsSourcesGenerator(object): gcc_command = gcc_command_template +\ path + " > " + preprocessing_dst - status, output = commands.getstatusoutput(gcc_command) + result = run_command([gcc_command]) - if status != 0: - print "Generating preprocessing for " + self.get_main_tested_dir() + path \ - + " failed!" - print output - commands.getoutput("rm -f " + preprocessing_dst) + if result.returncode != 0: + print(f"Generating preprocessing for {self.get_main_tested_dir() + path} failed!") + print(result.output) + run_command(["rm", "-f", preprocessing_dst]) continue self.remove_hashes(preprocessing_dst) - print "Preprocessed file " + path + " saved to " + preprocessing_dst + print(f"Preprocessed file {path} saved to {preprocessing_dst}") def copy_includes(self): includes_dict = self.get_includes_to_copy_dict() - for dst, src in includes_dict.iteritems(): + for dst, src in includes_dict.items(): src_path = os.path.normpath(self.get_main_tested_dir() + src) if not os.path.isdir(src_path): - print "Directory " + src_path + " given to include does not exists!" + print(f"Directory {src_path} given to include does not exists!") continue dst_path = os.path.normpath(self.get_main_UT_dir() + dst) @@ -131,7 +138,7 @@ class UnitTestsSourcesGenerator(object): preprocessed_tested_path = self.get_preprocessing_repo() + path if not os.path.isfile(preprocessed_tested_path): - print "No preprocessed path for " + test_path + " test file." + print(f"No preprocessed path for {test_path} test file.") continue tested_src = self.get_src_to_test(test_path, preprocessed_tested_path) @@ -140,8 +147,7 @@ class UnitTestsSourcesGenerator(object): with open(self.get_sources_to_test_repo() + test_path, "w") as f: f.writelines(tested_src) - print "Sources for " + test_path + " saved in " +\ - self.get_sources_to_test_repo() + test_path + print(f"Sources for {test_path} saved in {self.get_sources_to_test_repo() + test_path}") def create_main_cmake_lists(self): buf = "cmake_minimum_required(VERSION 2.6.0)\n\n" @@ -173,7 +179,7 @@ class UnitTestsSourcesGenerator(object): with open(self.get_main_UT_dir() + "CMakeLists.txt", "w") as f: f.writelines(buf) - print "Main CMakeLists.txt generated written to " + self.get_main_UT_dir() + "CMakeLists.txt" + print(f"Main CMakeLists.txt generated written to {self.get_main_UT_dir()} CMakeLists.txt") def generate_cmakes_for_tests(self): test_files_paths = self.get_files_with_tests_list() @@ -181,7 +187,7 @@ class UnitTestsSourcesGenerator(object): for test_path in test_files_paths: tested_file_path = self.get_sources_to_test_repo() + test_path if not os.path.isfile(tested_file_path): - print "No source to test for " + test_path + " test" + print(f"No source to test for {test_path} test") continue test_file_path = self.get_main_UT_dir() + test_path @@ -192,7 +198,7 @@ class UnitTestsSourcesGenerator(object): cmake_path = os.path.splitext(cmake_path)[0] + ".cmake" with open(cmake_path, "w") as f: f.writelines(cmake_buf) - print "cmake file for " + test_path + " written to " + cmake_path + print(f"cmake file for {test_path} written to {cmake_path}") cmake_lists_path = os.path.dirname(cmake_path) + os.sep self.update_cmakelists(cmake_lists_path, cmake_path) @@ -269,11 +275,11 @@ class UnitTestsSourcesGenerator(object): # find all functions' definitions | put tabs instead of spaces | # take only columns with function name and line number | sort in descending order - status, output = commands.getstatusoutput(ctags_path + "-x --c-types=f " + file_path + " --language-force=c | \ - sed \"s/ \\+/\t/g\" | cut -f 1,3 | sort -nsr -k 2") + result = run_command([ctags_path, "-x", "--c-types=f", file_path, + "--language-force=c | sed \"s/ \\+/\t/g\" | cut -f 1,3 | sort -nsr -k 2"]) # 'output' is string, but it has to be changed to list - output = output.split("\n") + output = list(filter(None, result.stdout.split("\n"))) return output def remove_functions_from_list(self, functions_list, to_remove_list): @@ -332,14 +338,14 @@ class UnitTestsSourcesGenerator(object): function_name = self.get_tested_function_name(path) if file_path is None: - print path + " file has no tested_file tag!" + print(f"{path} file has no tested_file tag!") return None elif not os.path.isfile(self.get_main_tested_dir() + file_path): - print "Tested file given in " + path + " not exist!" + print(f"Tested file given in {path} does not exist!") return None if function_name is None: - print path + " file has no tested_function_name tag!" + print(f"{path} file has no tested_function_name tag!") return None return True @@ -472,23 +478,23 @@ class UnitTestsSourcesGenerator(object): del code_lines_list[line_id + 1: last_line_id + 1] def set_ctags_path(self): - status, output = commands.getstatusoutput("/usr/bin/ctags --version &> /dev/null") - if status == 0: + result = run_command(["/usr/bin/ctags --version &> /dev/null"]) + if result.returncode == 0: path = "/usr/bin/ctags " - status, output = commands.getstatusoutput(path + "--c-types=f") - if not re.search("unrecognized option", output, re.IGNORECASE): + result = run_command([path, "--c-types=f"]) + if not re.search("unrecognized option", result.stdout, re.IGNORECASE): self.ctags_path = path return - status, output = commands.getstatusoutput("/usr/local/bin/ctags --version &> /dev/null") - if status == 0: + result = run_command(["/usr/local/bin/ctags --version &> /dev/null"]) + if result.returncode == 0: path = "/usr/local/bin/ctags " - status, output = commands.getstatusoutput(path + "--c-types=f") - if not re.search("unrecognized option", output, re.IGNORECASE): + result = run_command(["path", "--c-types=f"]) + if not re.search("unrecognized option", result.stdout, re.IGNORECASE): self.ctags_path = path return - print "ERROR: Current ctags version don't support \"--c-types=f\" parameter!" + print("ERROR: Current ctags version don't support \"--c-types=f\" parameter!") exit(1) def get_ctags_path(self): @@ -556,7 +562,7 @@ class UnitTestsSourcesGenerator(object): main_UT_dir = os.path.normpath(os.path.normpath(self.get_script_dir_path()\ + os.sep + tests_config.MAIN_DIRECTORY_OF_UNIT_TESTS)) if not os.path.isdir(main_UT_dir): - print "Given path to main UT directory is wrong!" + print("Given path to main UT directory is wrong!") sys.exit(1) self.main_UT_dir = main_UT_dir @@ -565,7 +571,7 @@ class UnitTestsSourcesGenerator(object): main_tested_dir = os.path.normpath(os.path.normpath(self.get_script_dir_path()\ + os.sep + tests_config.MAIN_DIRECTORY_OF_TESTED_PROJECT)) if not os.path.isdir(main_tested_dir): - print "Given path to main tested directory is wrong!" + print("Given path to main tested directory is wrong!") sys.exit(1) self.main_tested_dir = main_tested_dir @@ -579,7 +585,7 @@ def __main__(): generator.create_main_cmake_lists() generator.generate_cmakes_for_tests() - print "Files for testing generated!" + print("Files for testing generated!") if __name__ == "__main__": __main__() diff --git a/tests/unit/framework/run_unit_tests.py b/tests/unit/framework/run_unit_tests.py index a394379..d07caf2 100755 --- a/tests/unit/framework/run_unit_tests.py +++ b/tests/unit/framework/run_unit_tests.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # # Copyright(c) 2012-2018 Intel Corporation @@ -7,68 +7,75 @@ import tests_config import os -import commands import sys +import subprocess + +def run_command(args): + result = subprocess.run(" ".join(args), shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + result.stdout = result.stdout.decode("ASCII") + result.stderr = result.stderr.decode("ASCII") + return result script_path = os.path.dirname(os.path.realpath(__file__)) -main_UT_dir = os.path.normpath(script_path + os.sep\ - + tests_config.MAIN_DIRECTORY_OF_UNIT_TESTS) + os.sep +main_UT_dir = os.path.join(script_path, tests_config.MAIN_DIRECTORY_OF_UNIT_TESTS) -main_tested_dir = os.path.normpath(script_path + os.sep\ - + tests_config.MAIN_DIRECTORY_OF_TESTED_PROJECT) + os.sep +main_tested_dir = os.path.join(script_path, tests_config.MAIN_DIRECTORY_OF_TESTED_PROJECT) +if not os.path.isdir(os.path.join(main_UT_dir, "ocf_env", "ocf")): + try: + os.makedirs(os.path.join(main_UT_dir, "ocf_env", "ocf")) + except Exception: + raise Exception("Cannot create ocf_env/ocf directory!") -if not os.path.isdir(main_UT_dir + "ocf_env" + os.sep + "ocf"): - try: - os.makedirs(main_UT_dir + "ocf_env" + os.sep + "ocf") - except Exception: - print "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") ]) +if result.returncode != 0: + raise Exception("Preparing sources for testing failed!") -status, output = commands.getstatusoutput("cp " + main_tested_dir +\ - "inc" + os.sep + "*" + " " + main_UT_dir + "ocf_env" + os.sep + "ocf") +result = run_command([ os.path.join(script_path, "prepare_sources_for_testing.py") ]) +if result.returncode != 0: + raise Exception("Preparing sources for testing failed!") +build_dir = os.path.join(main_UT_dir, "build") +logs_dir = os.path.join(main_UT_dir, "logs") -if os.system(script_path + os.sep + "prepare_sources_for_testing.py") != 0: - print "Preparing sources for testing failed!" - exit() +try: + if not os.path.isdir(build_dir): + os.makedirs(build_dir) + if not os.path.isdir(logs_dir): + os.makedirs(logs_dir) +except Exception: + raise Exception("Cannot create logs directory!") +os.chdir(build_dir) -build_dir = main_UT_dir + "build" + os.sep -logs_dir = main_UT_dir + "logs" + os.sep +cmake_result = run_command([ "cmake", ".." ]) -if not os.path.isdir(build_dir): - try: - os.makedirs(build_dir) - except Exception: - print "Cannot create build directory!" -if not os.path.isdir(logs_dir): - try: - os.makedirs(logs_dir) - except Exception: - print "Cannot create logs directory!" +print(cmake_result.stdout) +with open(os.path.join(logs_dir, "cmake.output"), "w") as f: + f.write(cmake_result.stdout) -cmake_status, cmake_output = commands.getstatusoutput("cd " + build_dir + " && cmake ..") -print cmake_output -with open(logs_dir + 'cmake.output', 'w') as f: - f.write(cmake_output) +if cmake_result.returncode != 0: + with open(os.path.join(logs_dir, "tests.output"), "w") as f: + f.write("Cmake step failed! More details in cmake.output.") + sys.exit(1) -if cmake_status != 0: - with open(logs_dir + 'tests.output', 'w') as f: - f.write("Cmake step failed! More details in cmake.output.") - sys.exit(1) +make_result = run_command([ "make", "-j" ]) -make_status, make_output = commands.getstatusoutput("cd " + build_dir + " && make") -print make_output -with open(logs_dir + 'make.output', 'w') as f: - f.write(make_output) +print(make_result.stdout) +with open(os.path.join(logs_dir, "make.output"), "w") as f: + f.write(make_result.stdout) -if make_status != 0: - with open(logs_dir + 'tests.output', 'w') as f: +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) -test_status, test_output = commands.getstatusoutput("cd " + build_dir + " && make test") -print test_output -with open(logs_dir + 'tests.output', 'w') as f: - f.write(test_output) +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) diff --git a/tests/unit/framework/tests_config.py b/tests/unit/framework/tests_config.py index da0fe13..19a1761 100644 --- a/tests/unit/framework/tests_config.py +++ b/tests/unit/framework/tests_config.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # # Copyright(c) 2012-2018 Intel Corporation diff --git a/tests/unit/tests/add_new_test_file.py b/tests/unit/tests/add_new_test_file.py index 5eaf133..1e79136 100755 --- a/tests/unit/tests/add_new_test_file.py +++ b/tests/unit/tests/add_new_test_file.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # # Copyright(c) 2012-2018 Intel Corporation @@ -11,7 +11,7 @@ import os args = ' '.join(sys.argv[1:]) script_path = os.path.dirname(os.path.realpath(__file__)) -framework_script_path = script_path + os.sep + "../framework/add_new_test_file.py" +framework_script_path = os.path.join(script_path, "../framework/add_new_test_file.py") framework_script_path = os.path.normpath(framework_script_path) status, output = commands.getstatusoutput(framework_script_path + " " + args)