Skip to content

Commit

Permalink
feat: improve assertion message in assert_execute_process function
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed May 30, 2024
1 parent 830c048 commit f484f36
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
40 changes: 20 additions & 20 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -429,28 +429,28 @@ function(assert_execute_process)
)

if(DEFINED ARG_ERROR AND RES EQUAL 0)
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
message(
FATAL_ERROR
"expected command '${ARG_COMMAND}' to fail"
)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected command:" "${COMMAND}" "to fail")
message(FATAL_ERROR "${MESSAGE}")
elseif(NOT DEFINED ARG_ERROR AND NOT RES EQUAL 0)
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
message(
FATAL_ERROR
"expected command '${ARG_COMMAND}' not to fail"
)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected command:" "${COMMAND}" "not to fail")
message(FATAL_ERROR "${MESSAGE}")
elseif(DEFINED ARG_OUTPUT AND NOT "${OUT}" MATCHES "${ARG_OUTPUT}")
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
message(
FATAL_ERROR
"expected the output of command '${ARG_COMMAND}' to match '${ARG_OUTPUT}'"
)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected the output:" "${OUT}"
"of command:" "${COMMAND}"
"to match:" "${ARG_OUTPUT}")
message(FATAL_ERROR "${MESSAGE}")
elseif(DEFINED ARG_ERROR AND NOT "${ERR}" MATCHES "${ARG_ERROR}")
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
message(
FATAL_ERROR
"expected the error of command '${ARG_COMMAND}' to match '${ARG_ERROR}'"
)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected the error:" "${ERR}"
"of command:" "${COMMAND}"
"to match:" "${ARG_ERROR}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endfunction()
50 changes: 28 additions & 22 deletions test/cmake/AssertionTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -213,48 +213,54 @@ function("Process execution assertions")
end_mock_message()
assert_message(
FATAL_ERROR
"expected command '${CMAKE_COMMAND} -E true' to fail"
)
"expected command:\n ${CMAKE_COMMAND} -E true\nto fail")

mock_message()
assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E false)
end_mock_message()
assert_message(
FATAL_ERROR
"expected command '${CMAKE_COMMAND} -E false' not to fail"
)
"expected command:\n ${CMAKE_COMMAND} -E false\nnot to fail")

assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E echo "Hello world!"
OUTPUT "Hello.*!"
)
OUTPUT "Hello.*!")

mock_message()
assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E echo "Hello world!"
OUTPUT "Hi.*!"
)
OUTPUT "Hi.*!")
end_mock_message()
assert_message(
FATAL_ERROR
"expected the output of command '${CMAKE_COMMAND} -E echo Hello world!' to match 'Hi.*!'"
)
string(
JOIN "\n" EXPECTED_MESSAGE
"expected the output:"
" Hello world!"
" "
"of command:"
" ${CMAKE_COMMAND} -E echo Hello world!"
"to match:"
" Hi.*!")
assert_message(FATAL_ERROR "${EXPECTED_MESSAGE}")

assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E invalid
ERROR "CMake Error:.*Available commands:"
)
COMMAND "${CMAKE_COMMAND}" -E touch /
ERROR "cmake -E touch: failed to update")

mock_message()
assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E invalid
ERROR "CMake Error:.*Unavailable commands:"
)
COMMAND "${CMAKE_COMMAND}" -E touch /
ERROR "cmake -E touch: not failed to update")
end_mock_message()
assert_message(
FATAL_ERROR
"expected the error of command '${CMAKE_COMMAND} -E invalid' to match 'CMake Error:.*Unavailable commands:'"
)
string(
JOIN "\n" EXPECTED_MESSAGE
"expected the error:"
" cmake -E touch: failed to update \"/\"."
" "
"of command:"
" ${CMAKE_COMMAND} -E touch /"
"to match:"
" cmake -E touch: not failed to update")
assert_message(FATAL_ERROR "${EXPECTED_MESSAGE}")
endfunction()

function("Mock message")
Expand Down

0 comments on commit f484f36

Please sign in to comment.