instrumentation: Test shell redirection in instrumented custom commands

Issue: #27610
This commit is contained in:
Martin Duffy authored and Brad King committed 2026-02-20 08:59:59 -05:00
1 parent b75e6413c0
commit b6b946f7ec
5 files changed
+29 -3

No files matched your search

@@ -109,7 +109,7 @@ function(instrument test)
"${RunCMake_TEST_BINARY_DIR}/cmake-command-workflow.cmake"
@ONLY
)
foreach(f IN ITEMS CMakeLists.txt main.cxx lib.cxx lib.h)
foreach(f IN ITEMS CMakeLists.txt main.cxx lib.cxx lib.h shell_redirect.txt)
configure_file(
"${RunCMake_TEST_SOURCE_DIR}/${f}"
"${RunCMake_TEST_BINARY_DIR}/${f}"
@@ -88,7 +88,7 @@ foreach(snippet IN LISTS snippets)
endif()
json_missing_key("${snippet}" "${contents}" target)
# unrecognized outputs
else()
elseif (NOT outputs MATCHES "shell_redirect\\.out")
json_error("${snippet}" "Custom command has unexpected outputs\n${outputs}")
endif()
endif()
@@ -188,6 +188,10 @@ endif()
if (ARGS_TEST AND NOT EXISTS ${RunCMake_TEST_BINARY_DIR}/Testing)
add_error("ctest --instrument launcher failed to test the project")
endif()
# Ensure shell_redirect command ran successfully
if (ARGS_BUILD AND NOT EXISTS ${RunCMake_TEST_BINARY_DIR}/shell_redirect.out)
add_error("custom command with shell redirection did not run")
endif()
# Look for build snippet, which may not appear immediately
if (ARGS_BUILD_MAKE_PROGRAM)
@@ -0,0 +1 @@
Redirect from shell in custom command
@@ -15,10 +15,30 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E true
OUTPUT output1 output2
)
file(COPY_FILE shell_redirect.txt ${CMAKE_CURRENT_BINARY_DIR}/shell_redirect.in)
if(CMAKE_GENERATOR STREQUAL "Watcom WMake")
# Watcom WMake does not seem to understand shell redirection.
add_custom_command(
OUTPUT shell_redirect.out
VERBATIM
COMMAND ${CMAKE_COMMAND} -E cat shell_redirect.in
COMMAND ${CMAKE_COMMAND} -E touch shell_redirect.out
)
else()
add_custom_command(
OUTPUT shell_redirect.out
VERBATIM
COMMAND ${CMAKE_COMMAND} -E cat - < shell_redirect.in
COMMAND ${CMAKE_COMMAND} -E echo "> operator" > shell_redirect.out
COMMAND ${CMAKE_COMMAND} -E echo ">> operator" >> shell_redirect.out
)
endif()
set_property(SOURCE output1 output2 PROPERTY SYMBOLIC 1)
add_custom_target(customTarget ALL
COMMAND ${CMAKE_COMMAND} -E true
DEPENDS output1
DEPENDS output1 shell_redirect.out
)
add_test(NAME test COMMAND $<TARGET_FILE:main>)
install(TARGETS main)
@@ -0,0 +1 @@
Redirect from shell in custom command