Skip to content

Commit

Permalink
feat: add EXPECT_FAIL option to assert_execute_process function
Browse files Browse the repository at this point in the history
Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
  • Loading branch information
threeal committed Oct 17, 2024
1 parent d95ee1d commit 02a34a6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,12 @@ Asserts whether the given command correctly executes a process.
```cmake
assert_execute_process(
[COMMAND] <command> [<arguments>...]
[EXPECT_FAIL]
[EXPECT_OUTPUT <output>...]
[EXPECT_ERROR <error>...])
```

This function asserts whether the given `<command>` and `<arguments>` successfully execute a process. If `EXPECT_ERROR` is specified, it instead asserts whether it fails to execute the process.
This function asserts whether the given `<command>` and `<arguments>` successfully execute a process. If `EXPECT_FAIL` or `EXPECT_ERROR` is specified, it instead asserts whether it fails to execute the process.

If `EXPECT_OUTPUT` is specified, it also asserts whether the output of the executed process matches the expected `<output>`. If more than one `<output>` string is given, they are concatenated into a single output with no separator between the strings.

Expand Down
9 changes: 5 additions & 4 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,13 @@ endfunction()
#
# assert_execute_process(
# [COMMAND] <command> [<arguments>...]
# [EXPECT_FAIL]
# [EXPECT_OUTPUT <output>...]
# [EXPECT_ERROR <error>...])
#
# This function asserts whether the given `<command>` and `<arguments>`
# successfully execute a process. If `EXPECT_ERROR` is specified, it instead
# asserts whether it fails to execute the process.
# successfully execute a process. If `EXPECT_FAIL` or `EXPECT_ERROR` is
# specified, it instead asserts whether it fails to execute the process.
#
# If `EXPECT_OUTPUT` is specified, it also asserts whether the output of the
# executed process matches the expected `<output>`. If more than one `<output>`
Expand All @@ -447,7 +448,7 @@ endfunction()
# between the strings.
function(assert_execute_process)
cmake_parse_arguments(
PARSE_ARGV 0 ARG "" "" "COMMAND;EXPECT_OUTPUT;EXPECT_ERROR")
PARSE_ARGV 0 ARG EXPECT_FAIL "" "COMMAND;EXPECT_OUTPUT;EXPECT_ERROR")

if(NOT DEFINED ARG_COMMAND)
set(ARG_COMMAND ${ARG_UNPARSED_ARGUMENTS})
Expand All @@ -459,7 +460,7 @@ function(assert_execute_process)
OUTPUT_VARIABLE OUT
ERROR_VARIABLE ERR)

if(DEFINED ARG_EXPECT_ERROR)
if(ARG_EXPECT_FAIL OR DEFINED ARG_EXPECT_ERROR)
if(RES EQUAL 0)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected command" COMMAND "to fail")
Expand Down
4 changes: 2 additions & 2 deletions test/assert_execute_process.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ section("process execution assertions")

section("it should fail to assert a process execution")
assert_fatal_error(
CALL assert_execute_process "${CMAKE_COMMAND}" -E true EXPECT_ERROR .*
CALL assert_execute_process "${CMAKE_COMMAND}" -E true EXPECT_FAIL
EXPECT_MESSAGE "expected command:\n ${CMAKE_COMMAND} -E true\nto fail")
endsection()
endsection()
Expand All @@ -19,7 +19,7 @@ section("failed process execution assertions")

section("it should assert a failed process execution")
assert_execute_process(
"${CMAKE_COMMAND}" -E make_directory some-file EXPECT_ERROR .*)
"${CMAKE_COMMAND}" -E make_directory some-file EXPECT_FAIL)
endsection()

section("it should fail to assert a failed process execution")
Expand Down

0 comments on commit 02a34a6

Please sign in to comment.