Skip to content

Commit

Permalink
feat: add execute process assertion functions (#37)
Browse files Browse the repository at this point in the history
* feat: add execute process assertion functions

* test: add test for execute process assertion functions
  • Loading branch information
threeal authored May 17, 2024
1 parent a48a8a7 commit 23a71f4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,27 @@ function(assert_message MODE EXPECTED_MESSAGE)
set(${MODE}_MESSAGES "${${MODE}_MESSAGES}" PARENT_SCOPE)
endif()
endfunction()

# Asserts whether the given command successfully executes a process.
#
# Arguments:
# - ARGN: The command to execute.
function(assert_execute_process)
execute_process(COMMAND ${ARGN} RESULT_VARIABLE RES)
if(NOT RES EQUAL 0)
string(REPLACE ";" " " ARGUMENTS "${ARGN}")
message(FATAL_ERROR "expected command '${ARGUMENTS}' not to fail (exit code: ${RES})")
endif()
endfunction()

# Asserts whether the given command fails to execute a process.
#
# Arguments:
# - ARGN: The command to execute.
function(assert_not_execute_process)
execute_process(COMMAND ${ARGN} RESULT_VARIABLE RES)
if(RES EQUAL 0)
string(REPLACE ";" " " ARGUMENTS "${ARGN}")
message(FATAL_ERROR "expected command '${ARGUMENTS}' to fail (exit code: ${RES})")
endif()
endfunction()
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ add_cmake_test(
"Assert unequal strings"
"Mock message"
"Assert messages"
"Assert successful process execution"
"Assert failed process execution"
)
18 changes: 18 additions & 0 deletions test/cmake/AssertionTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,22 @@ function("Assert messages")
assert_message(FATAL_ERROR "expected error message '' to be equal to 'some other error message'")
endfunction()

function("Assert successful process execution")
assert_execute_process("${CMAKE_COMMAND}" -E true)

mock_message()
assert_not_execute_process("${CMAKE_COMMAND}" -E true)
end_mock_message()
assert_message(FATAL_ERROR "expected command '${CMAKE_COMMAND} -E true' to fail (exit code: 0)")
endfunction()

function("Assert failed process execution")
mock_message()
assert_execute_process("${CMAKE_COMMAND}" -E false)
end_mock_message()
assert_message(FATAL_ERROR "expected command '${CMAKE_COMMAND} -E false' not to fail (exit code: 1)")

assert_not_execute_process("${CMAKE_COMMAND}" -E false)
endfunction()

cmake_language(CALL "${TEST_COMMAND}")

0 comments on commit 23a71f4

Please sign in to comment.