From ba1324fb3e44e804c8187aaef2ff8c33d775ebcc Mon Sep 17 00:00:00 2001 From: Yash Chauhan Date: Wed, 17 Jan 2024 15:48:41 +0530 Subject: [PATCH] Renamed some variables and replaced os.path with pathlib --- .github/workflows/windows_x86_build.yml | 2 +- CMakeLists.txt | 14 ++-- tests/CMakeTests/run_test.py | 8 +-- .../tests/Utils/get_exported_function_list.py | 66 ++++++++++--------- .../tests/exported_functions_addition_test.py | 6 +- .../tests/exported_functions_test.py | 18 +++-- .../tests/message_structure_test.py | 8 +-- 7 files changed, 61 insertions(+), 61 deletions(-) diff --git a/.github/workflows/windows_x86_build.yml b/.github/workflows/windows_x86_build.yml index 13c16c0b..c3981c75 100644 --- a/.github/workflows/windows_x86_build.yml +++ b/.github/workflows/windows_x86_build.yml @@ -24,7 +24,7 @@ jobs: - name: Create Build Environment run: mkdir ${{runner.workspace}}\grpc-labview\build - + - name: Configure CMake working-directory: ${{runner.workspace}}\grpc-labview\build run: cmake -G "Visual Studio 16 2019" -A Win32 .. diff --git a/CMakeLists.txt b/CMakeLists.txt index dae8f7c8..1a2fa267 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,8 +55,8 @@ include_directories("${CMAKE_CURRENT_BINARY_DIR}" "./src" "./third_party/grpc" " # LabVIEW support for grpc and protobuf #---------------------------------------------------------------------- -add_custom_target(Detect_MessageMetadata_Structural_Changes - COMMAND ${CMAKE_COMMAND} -E echo "Running MessageMetadata structure test.py ..." +add_custom_target(Detect_Compatibility_Breaks + COMMAND ${CMAKE_COMMAND} -E echo "Detecting backward compatibility breakage ..." COMMAND python -m pip install -r ${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeTests/requirements.txt COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeTests/run_test.py RESULT_VARIABLE shell_command_result @@ -190,8 +190,8 @@ target_link_libraries(test_server ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF}) -add_dependencies(labview_grpc_server Detect_MessageMetadata_Structural_Changes) -add_dependencies(labview_grpc_generator Detect_MessageMetadata_Structural_Changes) -add_dependencies(test_client Detect_MessageMetadata_Structural_Changes) -add_dependencies(test_server Detect_MessageMetadata_Structural_Changes) -add_dependencies(example_client Detect_MessageMetadata_Structural_Changes) \ No newline at end of file +add_dependencies(labview_grpc_server Detect_Compatibility_Breaks) +add_dependencies(labview_grpc_generator Detect_Compatibility_Breaks) +add_dependencies(test_client Detect_Compatibility_Breaks) +add_dependencies(test_server Detect_Compatibility_Breaks) +add_dependencies(example_client Detect_Compatibility_Breaks) \ No newline at end of file diff --git a/tests/CMakeTests/run_test.py b/tests/CMakeTests/run_test.py index 492f30ca..9b259423 100644 --- a/tests/CMakeTests/run_test.py +++ b/tests/CMakeTests/run_test.py @@ -1,11 +1,11 @@ import subprocess import sys -import os +from pathlib import Path def run_tests(): - message_structures_test_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tests/message_structure_test.py") - exported_functions_test_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tests/exported_functions_test.py") - exported_functions_addition_test_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tests/exported_functions_addition_test.py") + message_structures_test_path = Path(__file__) / "tests" / "message_structure_test.py" + exported_functions_test_path = Path(__file__) / "tests" / "exported_functions_test.py" + exported_functions_addition_test_path = Path(__file__) / "tests" / "exported_functions_addition_test.py" # Check the exit code result_message_structure = subprocess.run(["python", "-m", "pytest", message_structures_test_path, "-vv"]) diff --git a/tests/CMakeTests/tests/Utils/get_exported_function_list.py b/tests/CMakeTests/tests/Utils/get_exported_function_list.py index c57f3eac..8da5f750 100644 --- a/tests/CMakeTests/tests/Utils/get_exported_function_list.py +++ b/tests/CMakeTests/tests/Utils/get_exported_function_list.py @@ -1,8 +1,8 @@ -import os import re import json +from pathlib import Path -folder_path = os.path.abspath(os.path.join(__file__, '../../../../../src')) +folder_path = Path(__file__).parent.parent.parent.parent.parent / "src" # Regex pattern to match exported function signatures pattern = re.compile(r'\bLIBRARY_EXPORT\b\s+(.*?)\s*{', re.DOTALL) @@ -10,40 +10,42 @@ def getFunctionSignatureList(): sorted_signature_list = {"size": 0, "signatures": []} unsorted_signature_list = [] - for root, dirs, files in os.walk(folder_path): - for file in files: - if file.endswith('.cc'): - with open(os.path.join(root, file), 'r') as f: - content = f.read() - matches = pattern.findall(content) - for match in matches: - # Replace new line with space - match = match.replace('\n', ' ') - # Extract Parameters - match_list = match.split('(') - return_type = match_list[0].split()[0].strip() - function_name = match_list[0].split()[-1].strip() - parameter_list = match_list[1].split(')')[0].split(',') + # Traversing all the files in the src folder + for file_path in folder_path.rglob("*"): + if str(file_path).endswith('.cc'): + with open(file_path, 'r') as f: + content = f.read() + matches = pattern.findall(content) + for match in matches: + # Replace new line with space + match = match.replace('\n', ' ') - # Remove the parameter name - for i in range(len(parameter_list)): - parameter = parameter_list[i] - parameter_name = parameter.split()[-1] - cur = ' '.join(parameter.split()[:-1]) - if parameter_name[0] == '*': - cur += '*' - if len(parameter_name) > 1 and parameter_name[1] == '*': - cur += '*' - cur += ' '+parameter.split()[-1] - parameter_list[i] = cur - parameter_list = [' '.join(parameter.split()[:-1]) for parameter in parameter_list] + # Extract Parameters + match_list = match.split('(') + return_type = match_list[0].split()[0].strip() + function_name = match_list[0].split()[-1].strip() + parameter_list = match_list[1].split(')')[0].split(',') - # Add parameter list and function name - # match = return_type + ' ' + function_name + '(' + ', '.join(parameter_list) + ')' + # Remove the parameter name + for i in range(len(parameter_list)): + parameter = parameter_list[i] + parameter_name = parameter.split()[-1] + cur = ' '.join(parameter.split()[:-1]) + if parameter_name[0] == '*': + cur += '*' + if len(parameter_name) > 1 and parameter_name[1] == '*': + cur += '*' + cur += ' '+parameter.split()[-1] + parameter_list[i] = cur + parameter_list = [' '.join(parameter.split()[:-1]) for parameter in parameter_list] + + # Add parameter list and function name + # match = return_type + ' ' + function_name + '(' + ', '.join(parameter_list) + ')' + + # Add each signature into unsorted list + unsorted_signature_list.append({"id": 404, "function_name": function_name, "return_type": return_type, "parameter_list": parameter_list}) - # Add each signature into unsorted list - unsorted_signature_list.append({"id": 404, "function_name": function_name, "return_type": return_type, "parameter_list": parameter_list}) sorted_signature_list["size"] = len(unsorted_signature_list) sorted_signature_list["signatures"] = sorted(unsorted_signature_list, key=lambda k: k['function_name']) diff --git a/tests/CMakeTests/tests/exported_functions_addition_test.py b/tests/CMakeTests/tests/exported_functions_addition_test.py index a1cae7dd..dd5dccc2 100644 --- a/tests/CMakeTests/tests/exported_functions_addition_test.py +++ b/tests/CMakeTests/tests/exported_functions_addition_test.py @@ -1,8 +1,8 @@ import Utils.get_exported_function_list as get_exported_functions import json -import os +from pathlib import Path -struct_json_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../testcases/ExportedFunctionList.json") +exported_functions_json_file_path = Path(__file__).parent.parent / "testcases" / "ExportedFunctionList.json" def read_json(filepath): with open(filepath, 'r') as file: @@ -10,7 +10,7 @@ def read_json(filepath): return data new_function_list, new_function_map = get_exported_functions.getFunctionSignatureList() -old_function_list = read_json(struct_json_file_path) +old_function_list = read_json(exported_functions_json_file_path) old_function_map = get_exported_functions.getFunctionMap(old_function_list) if new_function_list["size"] != old_function_list["size"]: diff --git a/tests/CMakeTests/tests/exported_functions_test.py b/tests/CMakeTests/tests/exported_functions_test.py index c2949ddb..6990ec39 100644 --- a/tests/CMakeTests/tests/exported_functions_test.py +++ b/tests/CMakeTests/tests/exported_functions_test.py @@ -1,21 +1,19 @@ -import re import json import pytest -import os +from pathlib import Path import Utils.get_exported_function_list as get_exported_function_list -struct_json_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../testcases/ExportedFunctionList.json") +exported_functions_json_file_path = Path(__file__).parent.parent / "testcases" / "ExportedFunctionList.json" def read_json(filepath): with open(filepath, 'r') as file: test_data = json.load(file) return test_data -exported_function_list, function_map = get_exported_function_list.getFunctionSignatureList() -# function_map = get_exported_function_list.get_function_map(exported_function_list) +parsed_exported_function_list, parsed_function_map = get_exported_function_list.getFunctionSignatureList() -@pytest.mark.parametrize('function', read_json(struct_json_file_path)['signatures']) -def test_functions(function): - test_input = function_map.get(function['function_name']) - expected_output = {"function_name": function["function_name"], "return_type": function["return_type"], "parameter_list": function["parameter_list"]} - assert test_input == expected_output +@pytest.mark.parametrize('function_signature', read_json(exported_functions_json_file_path)['signatures']) +def test_function_compatibility(function_signature): + test_input = parsed_function_map.get(function_signature['function_name']) + expected_output = {"function_name": function_signature["function_name"], "return_type": function_signature["return_type"], "parameter_list": function_signature["parameter_list"]} + assert test_input == expected_output \ No newline at end of file diff --git a/tests/CMakeTests/tests/message_structure_test.py b/tests/CMakeTests/tests/message_structure_test.py index 30496c8c..7b2f8a6e 100644 --- a/tests/CMakeTests/tests/message_structure_test.py +++ b/tests/CMakeTests/tests/message_structure_test.py @@ -1,9 +1,9 @@ import re import json import pytest -import os +from pathlib import Path -struct_json_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../testcases/MessageStructures.json") +struct_json_file_path = Path(__file__).parent.parent / "testcases" / "MessageStructures.json" def read_json(filepath): with open(filepath, 'r') as file: @@ -28,10 +28,10 @@ def extract_struct_by_name(file_path, target_struct_name): return {'name': '', 'fields': []} -cpp_file_path = os.path.abspath(os.path.join(__file__, '../../../../src/message_metadata.h')) +cpp_file_path = Path(__file__).parent.parent.parent.parent / "src" / "message_metadata.h" @pytest.mark.parametrize('struct', read_json(struct_json_file_path)) -def test_structs(struct): +def test_struct_compatibility(struct): test_input = extract_struct_by_name(cpp_file_path, struct['name'])['fields'] expected_output = struct['fields'] assert test_input == expected_output