Skip to content

Commit

Permalink
feat: modify execute process result assertion (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal authored May 28, 2024
1 parent 6b4dd05 commit c921b64
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
20 changes: 14 additions & 6 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,20 @@ function(assert_message MODE EXPECTED_MESSAGE)
endif()
endfunction()

# Asserts whether the given command successfully executes a process.
# Asserts whether the given command executes a process.
#
# This function asserts whether the given command successfully executes a
# process. If the `ERROR` argument is specified, this function asserts
# whether the given command fails to execute the process.
#
# Optional arguments:
# - COMMAND: The command to execute.
# - RESULT: If set, asserts whether the executed process exits with the given
# status.
# - OUTPUT: If set, asserts whether the output of the executed process matches
# the given regular expression.
# - ERROR: If set, asserts whether the error of the executed process matches
# the given regular expression.
function(assert_execute_process)
cmake_parse_arguments(ARG "" "RESULT;OUTPUT;ERROR" "COMMAND" ${ARGN})
cmake_parse_arguments(ARG "" "OUTPUT;ERROR" "COMMAND" ${ARGN})

execute_process(
COMMAND ${ARG_COMMAND}
Expand All @@ -150,11 +152,17 @@ function(assert_execute_process)
ERROR_VARIABLE ERR
)

if(DEFINED ARG_RESULT AND NOT RES EQUAL "${ARG_RESULT}")
if(DEFINED ARG_ERROR AND RES EQUAL 0)
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
message(
FATAL_ERROR
"expected command '${ARG_COMMAND}' to fail"
)
elseif(NOT DEFINED ARG_ERROR AND NOT RES EQUAL 0)
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
message(
FATAL_ERROR
"expected command '${ARG_COMMAND}' to exit with status ${ARG_RESULT}"
"expected command '${ARG_COMMAND}' not to fail"
)
elseif(DEFINED ARG_OUTPUT AND NOT "${OUT}" MATCHES "${ARG_OUTPUT}")
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
Expand Down
15 changes: 5 additions & 10 deletions test/cmake/AssertionTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,22 @@ endfunction()

function("Process execution assertions")
assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E true)
assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E false)

assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E true RESULT 0)
assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E false RESULT 1)
assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E false ERROR .*)

mock_message()
assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E true RESULT 1)
assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E true ERROR .*)
end_mock_message()
assert_message(
FATAL_ERROR
"expected command '${CMAKE_COMMAND} -E true' to exit with status 1"
"expected command '${CMAKE_COMMAND} -E true' to fail"
)

mock_message()
assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E false RESULT 0)
assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E false)
end_mock_message()
assert_message(
FATAL_ERROR
"expected command '${CMAKE_COMMAND} -E false' to exit with status 0"
"expected command '${CMAKE_COMMAND} -E false' not to fail"
)

assert_execute_process(
Expand All @@ -208,14 +205,12 @@ function("Process execution assertions")

assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E invalid
RESULT 1
ERROR "CMake Error:.*Available commands:"
)

mock_message()
assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E invalid
RESULT 1
ERROR "CMake Error:.*Unavailable commands:"
)
end_mock_message()
Expand Down

0 comments on commit c921b64

Please sign in to comment.