Skip to content

Commit

Permalink
feat: modify OUTPUT and ERROR options in assert_execute_process
Browse files Browse the repository at this point in the history
… to accept multi vals (#119)
  • Loading branch information
threeal authored Jun 30, 2024
1 parent 45a5936 commit 91aa977
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
20 changes: 14 additions & 6 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ endfunction()
# Asserts whether the given command correctly executes a process.
#
# assert_execute_process(
# [COMMAND] <command> [<arg>...] [OUTPUT <output>] [ERROR <error>])
# [COMMAND] <command> [<arg>...] [OUTPUT <output>...] [ERROR <error>...])
#
# This function asserts whether the given command and arguments successfully
# execute a process.
Expand All @@ -211,12 +211,20 @@ endfunction()
# arguments fail to execute a process. It also asserts whether the error of the
# executed process matches the expected `<error>`.
function(assert_execute_process)
cmake_parse_arguments(PARSE_ARGV 0 ARG "" "OUTPUT;ERROR" "COMMAND")
cmake_parse_arguments(PARSE_ARGV 0 ARG "" "" "COMMAND;OUTPUT;ERROR")

if(NOT DEFINED ARG_COMMAND)
set(ARG_COMMAND ${ARG_UNPARSED_ARGUMENTS})
endif()

if(DEFINED ARG_OUTPUT)
string(JOIN "" EXPECTED_OUTPUT ${ARG_OUTPUT})
endif()

if(DEFINED ARG_ERROR)
string(JOIN "" EXPECTED_ERROR ${ARG_ERROR})
endif()

execute_process(
COMMAND ${ARG_COMMAND}
RESULT_VARIABLE RES
Expand All @@ -234,19 +242,19 @@ function(assert_execute_process)
MESSAGE "expected command:" "${COMMAND}"
"not to fail with error:" "${ERR}")
message(FATAL_ERROR "${MESSAGE}")
elseif(DEFINED ARG_OUTPUT AND NOT "${OUT}" MATCHES "${ARG_OUTPUT}")
elseif(DEFINED EXPECTED_OUTPUT AND NOT "${OUT}" MATCHES "${EXPECTED_OUTPUT}")
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected the output:" "${OUT}"
"of command:" "${COMMAND}"
"to match:" "${ARG_OUTPUT}")
"to match:" "${EXPECTED_OUTPUT}")
message(FATAL_ERROR "${MESSAGE}")
elseif(DEFINED ARG_ERROR AND NOT "${ERR}" MATCHES "${ARG_ERROR}")
elseif(DEFINED EXPECTED_ERROR AND NOT "${ERR}" MATCHES "${EXPECTED_ERROR}")
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected the error:" "${ERR}"
"of command:" "${COMMAND}"
"to match:" "${ARG_ERROR}")
"to match:" "${EXPECTED_ERROR}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endfunction()
Expand Down
16 changes: 8 additions & 8 deletions test/AssertExecuteProcess.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,35 @@ endsection()
section("execute process output assertions")
assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E echo "Hello world!"
OUTPUT "Hello.*!")
OUTPUT "Hello" ".*!")

assert_fatal_error(
CALL assert_execute_process
COMMAND "${CMAKE_COMMAND}" -E echo "Hello world!"
OUTPUT "Hi.*!"
OUTPUT "Hello" ".*earth!"
MESSAGE "expected the output:\n"
" Hello world!\n"
".*\n"
"of command:\n"
" ${CMAKE_COMMAND} -E echo Hello world!\n"
"to match:\n"
" Hi.*!")
" Hello.*earth!")
endsection()

section("execute process error assertions")
file(TOUCH some-file)

assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E make_directory some-file
ERROR "Error creating directory \"some-file\".")
ERROR "Error creating directory" ".*some-file")

assert_fatal_error(
CALL assert_execute_process
COMMAND "${CMAKE_COMMAND}" -E make_directory some-file
ERROR "Error creating directory \"some-other-file\"."
ERROR "Error creating directory" ".*some-other-file"
MESSAGE "expected the error:\n"
" Error creating directory \"some-file\".\n"
".*\n"
"of command:\n"
" ${CMAKE_COMMAND} -E make_directory some-file\n"
"to match:\n"
" Error creating directory \"some-other-file\".")
" Error creating directory.*some-other-file")
endsection()

0 comments on commit 91aa977

Please sign in to comment.