Skip to content

Commit 6f40964

Browse files
cmake: Add python unit testing
1 parent 8c07919 commit 6f40964

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN "YES")
3838
add_subdirectory(scripts)
3939
set(MERGE_SCRIPT ${PROJECT_SOURCE_DIR}/scripts/gen_profiles_file.py)
4040
set(SOLUTION_SCRIPT ${PROJECT_SOURCE_DIR}/scripts/gen_profiles_solution.py)
41+
set(PROFILES_SCRIPT ${PROJECT_SOURCE_DIR}/scripts/profiles.py)
4142

4243
find_package(VulkanHeaders REQUIRED CONFIG QUIET)
4344

profiles/test/CMakeLists.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,71 @@ add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME} --gtest_catch_exceptions=0)
3939
set_target_properties(${TEST_NAME} PROPERTIES FOLDER "Profiles schema")
4040

4141
add_dependencies(VpProfile_test_schema_validation VpGenerated)
42+
43+
function(add_python_unit_test test_base_name)
44+
# Check and install 'vulkan-object' if missing
45+
get_property(vulkan_object_checked GLOBAL PROPERTY VULKAN_OBJECT_CHECKED)
46+
if(NOT vulkan_object_checked)
47+
# Fetch the path of the discovered Python interpreter
48+
get_target_property(PYTHON_EXE Python3::Interpreter IMPORTED_LOCATION)
49+
if(NOT PYTHON_EXE)
50+
set(PYTHON_EXE "${Python3_EXECUTABLE}")
51+
endif()
52+
53+
message(STATUS "Checking for Python module 'vulkan_object'...")
54+
55+
# See if it's already installed to avoid unnecessary pip calls
56+
execute_process(
57+
COMMAND "${PYTHON_EXE}" -c "import vulkan_object"
58+
RESULT_VARIABLE ERROR_CODE
59+
OUTPUT_QUIET
60+
ERROR_QUIET
61+
)
62+
63+
# If not installed (ERROR_CODE is non-zero), install it
64+
if(NOT ERROR_CODE EQUAL 0)
65+
message(STATUS "Module 'vulkan_object' not found. Installing via pip...")
66+
execute_process(
67+
COMMAND "${PYTHON_EXE}" -m pip install vulkan-object
68+
RESULT_VARIABLE pip_status
69+
)
70+
71+
if(NOT pip_status EQUAL 0)
72+
message(FATAL_ERROR "Failed to install 'vulkan-object'. Please install it manually.")
73+
endif()
74+
else()
75+
message(STATUS "Module 'vulkan_object' is already installed.")
76+
endif()
77+
78+
# Cache the check globally so it only runs once per CMake configuration
79+
set_property(GLOBAL PROPERTY VULKAN_OBJECT_CHECKED TRUE)
80+
endif()
81+
82+
# Construct the target name and the path to the script
83+
set(target_name "python_unit_test_${test_base_name}")
84+
set(script_path "${PROJECT_SOURCE_DIR}/scripts/tests/test_${test_base_name}.py")
85+
86+
# Change the command from a direct file path to a module invocation (-m)
87+
add_custom_target(${target_name} ALL
88+
COMMAND Python3::Interpreter -m scripts.tests.test_${test_base_name} -v
89+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
90+
VERBATIM
91+
SOURCES "${script_path}"
92+
DEPENDS "${script_path}"
93+
)
94+
95+
set_target_properties(${target_name} PROPERTIES FOLDER "Profiles schema")
96+
# add_dependencies(${target_name} VpGenerated)
97+
98+
# Apply the same module invocation fix to the CTest definition
99+
add_test(NAME ${target_name}
100+
COMMAND Python3::Interpreter -m scripts.tests.test_${test_base_name} -v
101+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
102+
)
103+
endfunction()
104+
105+
add_python_unit_test(util)
106+
add_python_unit_test(json)
107+
add_python_unit_test(expression_tree)
108+
add_python_unit_test(vk_xml_parsing)
109+

0 commit comments

Comments
 (0)