Skip to content

Commit

Permalink
Renamed some variables and replaced os.path with pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-ni committed Jan 17, 2024
1 parent a50fc6e commit ba1324f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/windows_x86_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ..
Expand Down
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
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)
8 changes: 4 additions & 4 deletions tests/CMakeTests/run_test.py
Original file line number Diff line number Diff line change
@@ -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"])
Expand Down
66 changes: 34 additions & 32 deletions tests/CMakeTests/tests/Utils/get_exported_function_list.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
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)

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'])

Expand Down
6 changes: 3 additions & 3 deletions tests/CMakeTests/tests/exported_functions_addition_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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:
data = json.load(file)
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"]:
Expand Down
18 changes: 8 additions & 10 deletions tests/CMakeTests/tests/exported_functions_test.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions tests/CMakeTests/tests/message_structure_test.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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

0 comments on commit ba1324f

Please sign in to comment.