Skip to content

Commit

Permalink
feat: show error in execute process assertion (#90)
Browse files Browse the repository at this point in the history
* test: modify execute process assertion tests to call command with error message

* feat: show error if execute process failed to run during assertion
  • Loading branch information
threeal authored Jun 7, 2024
1 parent ee939cf commit 6f93489
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ function(assert_execute_process)
elseif(NOT DEFINED ARG_ERROR AND NOT RES EQUAL 0)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected command:" "${COMMAND}" "not to fail")
MESSAGE "expected command:" "${COMMAND}"
"not to fail with error:" "${ERR}")
message(FATAL_ERROR "${MESSAGE}")
elseif(DEFINED ARG_OUTPUT AND NOT "${OUT}" MATCHES "${ARG_OUTPUT}")
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
Expand Down
33 changes: 23 additions & 10 deletions test/AssertExecuteProcess.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ cmake_minimum_required(VERSION 3.17)
include(Assertion)

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

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

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

assert_fatal_error(
CALL assert_execute_process COMMAND "${CMAKE_COMMAND}" -E true ERROR .*
MESSAGE "expected command:\n ${CMAKE_COMMAND} -E true\nto fail")

string(
JOIN "\n" EXPECTED_MESSAGE
"expected command:"
" ${CMAKE_COMMAND} -E make_directory some-file"
"not to fail with error:"
" Error creating directory \"some-file\".")
assert_fatal_error(
CALL assert_execute_process COMMAND "${CMAKE_COMMAND}" -E false
MESSAGE "expected command:\n ${CMAKE_COMMAND} -E false\nnot to fail")
CALL assert_execute_process
COMMAND "${CMAKE_COMMAND}" -E make_directory some-file
MESSAGE "${EXPECTED_MESSAGE}")
endsection()

section("execute process output assertions")
Expand All @@ -36,21 +47,23 @@ section("execute process output assertions")
endsection()

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

assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E touch /
ERROR "cmake -E touch: failed to update")
COMMAND "${CMAKE_COMMAND}" -E make_directory some-file
ERROR "Error creating directory \"some-file\".")

string(
JOIN "\n" EXPECTED_MESSAGE
"expected the error:"
" cmake -E touch: failed to update \"/\"."
" Error creating directory \"some-file\"."
"of command:"
" ${CMAKE_COMMAND} -E touch /"
" ${CMAKE_COMMAND} -E make_directory some-file"
"to match:"
" cmake -E touch: not failed to update")
" Error creating directory \"some-other-file\".")
assert_fatal_error(
CALL assert_execute_process
COMMAND "${CMAKE_COMMAND}" -E touch /
ERROR "cmake -E touch: not failed to update"
COMMAND "${CMAKE_COMMAND}" -E make_directory some-file
ERROR "Error creating directory \"some-other-file\"."
MESSAGE "${EXPECTED_MESSAGE}")
endsection()

0 comments on commit 6f93489

Please sign in to comment.