add_test: Regression for unquoted argument with generator expression that evaluates to empty string
Originally reported in the forums here: https://discourse.cmake.org/t/add-test-handles-empty-strings-differently-from-before/11588
Consider the following example from the forum post:
set(include_dirs)
set(include_dir_options "$<$<BOOL:${include_dirs}>:-I;$<JOIN:${include_dirs},;-I;>>")
enable_testing()
add_test(NAME test
COMMAND ${CMAKE_COMMAND} -E echo "${include_dir_options}"
COMMAND_EXPAND_LISTS
)
In earlier CMake versions (3.20.5 in the forum post), the above would result in the following being run for the test command:
/path/to/cmake "-E" "echo"
With CMake CMake 3.29.6, the test command is instead:
/path/to/cmake "-E" "echo" ""
Note the extra empty command line argument at the end. This seems to be related to !8551 (merged), which fixed #24986 (closed), but I suspect the behavior switched back and forth at least once between these two CMake versions given the history recorded in that issue.
In the above example, if the generator expression expands to an empty string, I would have expected no empty argument to be added. It doesn't seem to matter whether the whole expression given to add_test()
is quoted or not, the result is the same. The presence of the COMMAND_EXPAND_LISTS
keyword seems like a stronger hint that the intention is for the arguments to be interpreted as a list, so if there are no list items after expansion, it would see that the expectation is that no arguments are added to the command line, but the current behavior in 3.29 and later (maybe 3.27 and later) seems to contradict that.
I've labelled this as a regression, but it might be intentional given the sequence of MRs that have been applied around this. It probably needs a closer look from those involved in those changes (@marc.chevrier @brad.king).