Dosyalar
rocm-systems/projects/rocprofiler-sdk/cmake/Modules/rocprofiler_sdk_PytestAddTests.cmake
Jonathan R. Madsen a2288eb50b [rocprofiler-sdk] Install unit tests and helper functions for integration tests (#921)
* [rocprofiler-sdk] Install unit tests and helper functions for integration tests

* Fix rocprofiler-sdk-tests-target export

* Fix handling of cmake policy CMP0174

* Remove -vv from new pytest.ini files

* add unit tests and integration tests.

* add path to ci workflow.

* misc. fixes.

* pc sampling tests.

* bug fixes.

* pc sampling tests fix.

* misc.

* Update CMakeLists.txt

* Update rocprofiler_config_install_tests.cmake, correct license name

* fix units tests install issues.

* fix counters_def file path.

* fix bug, arg shifting.

* vendor pytest-cmake.

* cmake config fix. missing endfunction()

* disable tests, 1.rocprofv3-trace-hip-libs. 2.kernel-tracing. 3.external_correlation 4.rocpd.

* disable buffered-tracing test and remove pytest-cmake from requirements.txt.

* disable hip-graph-tracing test.

* fix building standalone tests to load rocprofiler-sdk cmake package first and then find rocprofiler_sdk_pytest module.

* addressed comments: 1.add local bin path to code cov workflow. 2.add to cmake prefix path local bin. 3.use ROCPROFILER_MEMCHECK_PRELOAD_ENV_VALUE 4.misc. fix

* enabled back tests api_buffered, external_correlation_id, hip-graph, kernel-tracing, rocpd, tracing-hip-in-libraries. and misc fixes(formating, extra fixtures for agent-index tests.)

* cpack to use llvm bin for .hsaco debug symbols.

* psdb tests fixes.

* EOL.

* misc. fixes and Disable api_buffered_tracing, external_correlation_id, hip-graph-tracing, kernel-tracing, rocpd, summary, tracing-hip-libraries, tracing-plus-counter-collection.

* fix incorrect cmakelists file.

* strip smallkernel.bin

* format.

* revert disabled tests commit.

* misc. fix in counter tests.

* misc.

* search codeobj unit test assets in curr bin and install bin.

* refactor newly added rocpd tests.

* modify tests for newly added hip-host-tracing.

* add LD LIB path to units, psdb is failing due to libs not being found.

---------

Co-authored-by: Venkateshwar Reddy Kandula <venkateshwar.kandula1306@gmail.com>
Co-authored-by: Venkateshwar Reddy Kandula <Venkateshwarreddy.Kandula@amd.com>
Co-authored-by: JeniferC99 <150404595+JeniferC99@users.noreply.github.com>
2025-11-21 08:06:56 -06:00

170 satır
6.2 KiB
CMake

# Wrapper used to create individual CTest tests from Pytest tests.
cmake_minimum_required(VERSION 3.20...4.1)
if(CMAKE_SCRIPT_MODE_FILE)
# Initialize content for the CMake test file.
set(_content "")
# Convert library and Python paths to native format.
cmake_path(CONVERT "${LIBRARY_PATH}" TO_NATIVE_PATH_LIST LIBRARY_PATH)
cmake_path(CONVERT "${PYTHON_PATH}" TO_NATIVE_PATH_LIST PYTHON_PATH)
# Serialize path values separated by semicolons (required on Windows).
string(REPLACE [[;]] [[\\;]] LIBRARY_PATH "${LIBRARY_PATH}")
string(REPLACE [[;]] [[\\;]] PYTHON_PATH "${PYTHON_PATH}")
# Set up the encoded environment with required paths.
set(ENCODED_ENVIRONMENT "${LIBRARY_ENV_NAME}=${LIBRARY_PATH}"
"PYTHONPATH=${PYTHON_PATH}")
# Serialize additional environment variables if any are provided.
foreach(env ${ENVIRONMENT})
string(REPLACE [[;]] [[\\;]] env "${env}")
list(APPEND ENCODED_ENVIRONMENT "${env}")
endforeach()
# Handle EXTRA_ARGS for individual tests
if(BUNDLE_TESTS)
list(PREPEND EXTRA_ARGS ${DISCOVERY_EXTRA_ARGS})
endif()
set(EXTRA_ARGS_WRAPPED)
foreach(arg IN LISTS EXTRA_ARGS)
list(APPEND EXTRA_ARGS_WRAPPED "[==[${arg}]==]")
endforeach()
list(JOIN EXTRA_ARGS_WRAPPED " " EXTRA_ARGS_STR)
# Macro to create individual tests with optional test properties.
macro(create_test NAME IDENTIFIERS)
string(APPEND _content "add_test([==[${NAME}]==] \"${PYTEST_EXECUTABLE}\"")
foreach(identifier ${IDENTIFIERS})
string(APPEND _content " [==[${identifier}]==]")
endforeach()
string(APPEND _content " ${EXTRA_ARGS_STR} )\n")
# Prepare the properties for the test, including the environment settings.
set(args "PROPERTIES ENVIRONMENT [==[${ENCODED_ENVIRONMENT}]==]")
# Add working directory
string(APPEND args " WORKING_DIRECTORY [==[${WORKING_DIRECTORY}]==]")
# Append any additional properties, escaping complex characters if necessary.
foreach(property ${TEST_PROPERTIES})
if(property MATCHES "[^-./:a-zA-Z0-9_]")
string(APPEND args " [==[${property}]==]")
else()
string(APPEND args " ${property}")
endif()
endforeach()
# Append the test properties to the content.
string(APPEND _content "set_tests_properties([==[${NAME}]==] ${args})\n")
endmacro()
# If tests are bundled together, create a single test group.
if(BUNDLE_TESTS)
create_test("\${TEST_GROUP_NAME}" "\${TEST_PATHS}")
else()
# Set environment variables for collecting tests.
set(ENV{${LIBRARY_ENV_NAME}} "${LIBRARY_PATH}")
set(ENV{PYTHONPATH} "${PYTHON_PATH}")
set(ENV{PYTHONWARNINGS} "ignore")
set(_command "${PYTEST_EXECUTABLE}" --collect-only -q
"--rootdir=${WORKING_DIRECTORY}" ${DISCOVERY_EXTRA_ARGS})
foreach(test_path IN LISTS TEST_PATHS)
list(APPEND _command "${test_path}")
endforeach()
# Collect tests.
execute_process(
COMMAND ${_command}
OUTPUT_VARIABLE _output_lines
ERROR_VARIABLE _output_lines
OUTPUT_STRIP_TRAILING_WHITESPACE
WORKING_DIRECTORY ${WORKING_DIRECTORY})
# Check for errors during test collection.
string(REGEX MATCH "(=+ ERRORS =+|ERROR:).*" _error "${_output_lines}")
if(_error)
message(${_error})
message(
FATAL_ERROR "An error occurred during the collection of Python tests.")
endif()
# Convert the collected output into a list of lines.
string(REPLACE [[;]] [[\;]] _output_lines "${_output_lines}")
string(REPLACE "\n" ";" _output_lines "${_output_lines}")
# Regex pattern to identify pytest test identifiers.
set(test_pattern "([^:]+)\.py(::([^:]+))?::([^:]+)")
# Iterate through each line to identify and process tests.
foreach(line ${_output_lines})
string(REGEX MATCHALL ${test_pattern} matching "${line}")
# Skip lines that are not identified as tests.
if(NOT matching)
continue()
endif()
# Extract file, class, and function names from the test pattern.
set(_file ${CMAKE_MATCH_1})
set(_class ${CMAKE_MATCH_3})
set(_func ${CMAKE_MATCH_4})
# Optionally trim parts of the class or function name.
if(TRIM_FROM_NAME)
string(REGEX REPLACE "${TRIM_FROM_NAME}" "" _class "${_class}")
string(REGEX REPLACE "${TRIM_FROM_NAME}" "" _func "${_func}")
endif()
# Form the test name using class and function.
if(_class)
set(test_name "${_class}.${_func}")
else()
set(test_name "${_func}")
endif()
# Optionally strip parameter brackets from the test name.
if(STRIP_PARAM_BRACKETS)
string(REGEX REPLACE "\\[(.+)\\]$" ".\\1" test_name "${test_name}")
endif()
# Optionally include the file path in the test name.
if(INCLUDE_FILE_PATH)
cmake_path(CONVERT "${_file}" TO_CMAKE_PATH_LIST _file)
string(REGEX REPLACE "/" "." _file "${_file}")
set(test_name "${_file}.${test_name}")
endif()
# Optionally trim parts of the full test name.
if(TRIM_FROM_FULL_NAME)
string(REGEX REPLACE "${TRIM_FROM_FULL_NAME}" "" test_name "${test_name}")
endif()
# Prefix the test name with the test group name.
set(test_name "${TEST_GROUP_NAME}.${test_name}")
set(test_case "${line}")
# Create the test for CTest.
create_test("\${test_name}" "\${test_case}")
endforeach()
# Warn if no tests were discovered.
if(NOT _content)
message(WARNING "No Python tests have been discovered.")
endif()
endif()
# Write the generated test content to the specified CTest file.
file(WRITE ${CTEST_FILE} ${_content})
endif()