Skip to content

Commit

Permalink
[cmake] 新增 runm_utest 系列目标, 仅在 utest 未执行过或修改后执行测试; 解决了执行 run_utest 目标…
Browse files Browse the repository at this point in the history
…时, utest 运行失败而无法显示输出的问题
  • Loading branch information
WentsingNee committed Apr 2, 2024
1 parent bc04a14 commit 9f45003
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,4 @@ jobs:
run: cmake --build ${{github.workspace}}/KerbalTest-build --config ${{env.BUILD_TYPE}} --parallel

- name: Run Unit Test
run: cmake --build ${{github.workspace}}/KerbalTest-build --config ${{env.BUILD_TYPE}} --target run_utest
run: cmake --build ${{github.workspace}}/KerbalTest-build --config ${{env.BUILD_TYPE}} --target runm_utest
72 changes: 72 additions & 0 deletions cmake/run_utest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#
# @file run_utest.cmake
# @brief
# @date 2024-04-01
# @author Peter
# @copyright
# Peter of [ThinkSpirit Laboratory](http://thinkspirit.org/)
# of [Nanjing University of Information Science & Technology](http://www.nuist.edu.cn/)
# all rights reserved
#

cmake_minimum_required(VERSION 3.10)

include(Kerbal/list_ext)


##
# CACHED_HASH_FILE_PATH one, optional
# RUN_UTEST_ARGS multi
function(run_utest target path)
kerbal_sublist("${ARGV}" 2 -1 ARGV)
cmake_parse_arguments(
""
""
"CACHED_HASH_FILE_PATH"
"RUN_UTEST_ARGS"
${ARGV}
)

set(cached_hash_file_path ${_CACHED_HASH_FILE_PATH})
set(run_utest_args ${_RUN_UTEST_ARGS})

if (DEFINED cached_hash_file_path)
execute_process(
COMMAND
${CMAKE_COMMAND} -E sha512sum ${path}
OUTPUT_VARIABLE binary_hash
)
message(STATUS "binary_hash: ${binary_hash}")
if (EXISTS "${cached_hash_file_path}")
file(READ "${cached_hash_file_path}" cached_hash)
message(STATUS "cached_hash: ${cached_hash}")
if (cached_hash STREQUAL binary_hash)
message(STATUS "target: ${target} is skipped because it has run")
return()
endif ()
endif ()
endif ()
execute_process(
COMMAND ${KTEST_RUN_UTEST_INVOKER} ${path} ${run_utest_args}
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
RESULT_VARIABLE run_utest_exit_value
OUTPUT_FILE "test_output/${target}.out.txt"
COMMAND_ECHO STDOUT
)
execute_process(
COMMAND ${CMAKE_COMMAND} -E cat test_output/${target}.out.txt
)
if (NOT run_utest_exit_value EQUAL 0)
message(FATAL_ERROR "target: ${target} exit with value: ${run_utest_exit_value}")
endif ()
if (DEFINED cached_hash_file_path)
file(WRITE "${cached_hash_file_path}" "${binary_hash}")
endif ()
endfunction()


run_utest(
"${TARGET}" "${PATH}"
CACHED_HASH_FILE_PATH "${HASH_FILE}"
RUN_UTEST_ARGS "${RUN_UTEST_ARGS}"
)
68 changes: 46 additions & 22 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,57 @@ function(add_ktest_utest_target target target_src_file)
endif ()

if (NOT ${KTEST_CI} STREQUAL "")
set (run_utest_args "--run_in_ci")
list(APPEND run_utest_args "--run_in_ci")
endif ()

set(run_utest_target_name "run_${target}")
add_custom_target(${run_utest_target_name}
COMMAND ${KTEST_RUN_UTEST_INVOKER} ${path} ${run_utest_args} > test_output/${target}.out.txt
COMMAND ${CMAKE_COMMAND} -E cat test_output/${target}.out.txt
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
COMMENT "Running utest target ${target}"
DEPENDS ${target} run_utest_make_test_output_dir
)
set_target_properties("${run_utest_target_name}"
PROPERTIES ktest_category "run_utest"
)
set(run_utest_args_with_escape "")
foreach (arg IN LISTS run_utest_args)
set(run_utest_args_with_escape "${run_utest_args_with_escape}${arg}\;")
endforeach ()

foreach (run_modified IN ITEMS True False)
if (run_modified)
set(run_utest_target_name "runm_${target}")
set(hash_file "${PROJECT_BINARY_DIR}/runm_utest_hash/${target}")
set(ktest_category "runm_utest")
else ()
set(run_utest_target_name "run_${target}")
set(hash_file "")
set(ktest_category "run_utest")
endif ()

if (${KTEST_ENABLE_SANITIZE} AND ${CMAKE_CXX_COMPILER} MATCHES "MSVC")
if (NOT TARGET copy_sanitize_dll_before_run_utest)
add_custom_target(
copy_sanitize_dll_before_run_utest
COMMAND ${CMAKE_COMMAND} -E copy ${KTEST_LIB_ASAN_DLL} ${PROJECT_BINARY_DIR}/targets
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
)
add_custom_target(
${run_utest_target_name}
COMMAND
"${CMAKE_COMMAND}"
-DCMAKE_MODULE_PATH="${CMAKE_MODULE_PATH}"
-DKTEST_RUN_UTEST_INVOKER="${KTEST_RUN_UTEST_INVOKER}"
-DTARGET="${target}"
-DPATH="${path}"
-DHASH_FILE="${hash_file}"
-DRUN_UTEST_ARGS="${run_utest_args_with_escape}"
-DPROJECT_BINARY_DIR="${PROJECT_BINARY_DIR}"
-P "${PROJECT_SOURCE_DIR}/cmake/run_utest.cmake"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
DEPENDS ${target} run_utest_make_test_output_dir
)
set_target_properties("${run_utest_target_name}"
PROPERTIES ktest_category "${ktest_category}"
)

if (${KTEST_ENABLE_SANITIZE} AND ${CMAKE_CXX_COMPILER} MATCHES "MSVC")
if (NOT TARGET copy_sanitize_dll_before_run_utest)
add_custom_target(
copy_sanitize_dll_before_run_utest
COMMAND ${CMAKE_COMMAND} -E copy ${KTEST_LIB_ASAN_DLL} ${PROJECT_BINARY_DIR}/targets
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
)
endif ()
add_dependencies(${run_utest_target_name} copy_sanitize_dll_before_run_utest)
endif ()
add_dependencies(${run_utest_target_name} copy_sanitize_dll_before_run_utest)
endif ()

add_kerbal_parent_target(${run_utest_target_name})
add_kerbal_parent_target(${run_utest_target_name})
endforeach ()
endfunction()


Expand Down

0 comments on commit 9f45003

Please sign in to comment.