Skip to content

Commit

Permalink
feat: add support to specify operator in assert_execute_process fun…
Browse files Browse the repository at this point in the history
…ction

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
  • Loading branch information
threeal committed Oct 17, 2024
1 parent f30b8e6 commit 701632d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 23 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,17 @@ Asserts whether the given command correctly executes a process.
assert_execute_process(
[COMMAND] <command> [<arguments>...]
[EXPECT_FAIL]
[EXPECT_OUTPUT <output>...]
[EXPECT_ERROR <error>...])
[EXPECT_OUTPUT [MATCHES|STREQUAL] <output>...]
[EXPECT_ERROR [MATCHES|STREQUAL] <error>...])
```

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.
This function asserts whether the given `<command>` and `<arguments>` successfully execute a process. If `EXPECT_FAIL` or `EXPECT_ERROR` is specified, it asserts that the process fails to execute.

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.
If `EXPECT_OUTPUT` or `EXPECT_ERROR` is specified, it also asserts whether the output or error of the executed process matches the expected output or error.

If `EXPECT_ERROR` is specified, it also asserts whether the error of the executed process matches the expected `<error>`. If more than one `<error>` string is given, they are concatenated into a single error with no separator between the strings.
If `MATCHES` is specified, it asserts whether the output or error matches the `<output>` or `<error>`. If `STREQUAL` is specified, it asserts whether the output or error is equal to `<output>` or `<error>`. If neither is specified, it defaults to `MATCHES`.

If more than one `<output>` or `<error>` string is given, they are concatenated into a single output or error with no separator between the strings.

#### Example

Expand All @@ -225,7 +227,7 @@ assert_execute_process(
EXPECT_OUTPUT hello)
```

The above example asserts whether the call to `${CMAKE_COMMAND} -E echo hello` successfully executes a process whose output matches `hello`. If it somehow fails to execute the process, it will throw the following fatal error message:
The above example asserts that the call to `${CMAKE_COMMAND} -E echo hello` successfully executes a process whose output is equal to `hello`. If the process fails to execute, it will throw the following fatal error message:

```
expected command:
Expand Down
58 changes: 41 additions & 17 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -447,22 +447,24 @@ endfunction()
# assert_execute_process(
# [COMMAND] <command> [<arguments>...]
# [EXPECT_FAIL]
# [EXPECT_OUTPUT <output>...]
# [EXPECT_ERROR <error>...])
# [EXPECT_OUTPUT [MATCHES|STREQUAL] <output>...]
# [EXPECT_ERROR [MATCHES|STREQUAL] <error>...])
#
# 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.
# specified, it asserts that the process fails to execute.
#
# 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.
# If `EXPECT_OUTPUT` or `EXPECT_ERROR` is specified, it also asserts whether the
# output or error of the executed process matches the expected output or error.
#
# If `EXPECT_ERROR` is specified, it also asserts whether the error of the
# executed process matches the expected `<error>`. If more than one `<error>`
# string is given, they are concatenated into a single error with no separator
# between the strings.
# If `MATCHES` is specified, it asserts whether the output or error matches the
# `<output>` or `<error>`. If `STREQUAL` is specified, it asserts whether the
# output or error is equal to `<output>` or `<error>`. If neither is specified,
# it defaults to `MATCHES`.
#
# If more than one `<output>` or `<error>` string is given, they are
# concatenated into a single output or error with no separator between the
# strings.
function(assert_execute_process)
cmake_parse_arguments(
PARSE_ARGV 0 ARG EXPECT_FAIL "" "COMMAND;EXPECT_OUTPUT;EXPECT_ERROR")
Expand Down Expand Up @@ -492,21 +494,43 @@ function(assert_execute_process)
endif()

if(DEFINED ARG_EXPECT_OUTPUT)
list(GET ARG_EXPECT_OUTPUT 0 OPERATOR)
if(OPERATOR MATCHES ^MATCHES|STREQUAL$)
list(REMOVE_AT ARG_EXPECT_OUTPUT 0)
else()
set(OPERATOR "MATCHES")
endif()
string(JOIN "" EXPECTED_OUTPUT ${ARG_EXPECT_OUTPUT})
if(NOT "${OUT}" MATCHES "${EXPECTED_OUTPUT}")
if(NOT "${OUT}" ${OPERATOR} "${EXPECTED_OUTPUT}")
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected the output" OUT "of command" COMMAND
"to match" EXPECTED_OUTPUT)
if(OPERATOR STREQUAL "MATCHES")
fail("expected the output" OUT "of command" COMMAND
"to match" EXPECTED_OUTPUT)
else()
fail("expected the output" OUT "of command" COMMAND
"to be equal to" EXPECTED_OUTPUT)
endif()
return()
endif()
endif()

if(DEFINED ARG_EXPECT_ERROR)
list(GET ARG_EXPECT_ERROR 0 OPERATOR)
if(OPERATOR MATCHES ^MATCHES|STREQUAL$)
list(REMOVE_AT ARG_EXPECT_ERROR 0)
else()
set(OPERATOR "MATCHES")
endif()
string(JOIN "" EXPECTED_ERROR ${ARG_EXPECT_ERROR})
if(NOT "${ERR}" MATCHES "${EXPECTED_ERROR}")
if(NOT "${ERR}" ${OPERATOR} "${EXPECTED_ERROR}")
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected the error" ERR "of command" COMMAND
"to match" EXPECTED_ERROR)
if(OPERATOR STREQUAL "MATCHES")
fail("expected the error" ERR "of command" COMMAND
"to match" EXPECTED_ERROR)
else()
fail("expected the error" ERR "of command" COMMAND
"to be equal to" EXPECTED_ERROR)
endif()
endif()
endif()
endfunction()
Expand Down
60 changes: 60 additions & 0 deletions test/assert_execute_process.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ section("process execution output assertions")
assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E echo "Hello world!"
EXPECT_OUTPUT "Hello" ".*!")

assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E echo "Hello world!"
EXPECT_OUTPUT MATCHES "Hello" ".*!")

assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E echo "Hello world!"
EXPECT_OUTPUT STREQUAL "Hello world!\n")
endsection()

section("it should fail to assert a process execution output")
Expand All @@ -50,6 +58,28 @@ section("process execution output assertions")
" ${CMAKE_COMMAND} -E echo Hello world!\n"
"to match:\n"
" Hello.*earth!")

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

assert_fatal_error(
CALL assert_execute_process
COMMAND "${CMAKE_COMMAND}" -E echo "Hello world!"
EXPECT_OUTPUT STREQUAL "Hello earth!\n"
EXPECT_MESSAGE "expected the output:\n"
".*\n"
"of command:\n"
" ${CMAKE_COMMAND} -E echo Hello world!\n"
"to be equal to:\n"
" Hello earth!\n")
endsection()
endsection()

Expand All @@ -60,6 +90,14 @@ section("process execution error assertions")
assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E make_directory some-file
EXPECT_ERROR "Error creating directory" ".*some-file")

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

assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E make_directory some-file
EXPECT_ERROR STREQUAL "Error creating directory \"some-file\".\n")
endsection()

section("it should fail to assert a process execution error")
Expand All @@ -73,5 +111,27 @@ section("process execution error assertions")
" ${CMAKE_COMMAND} -E make_directory some-file\n"
"to match:\n"
" Error creating directory.*some-other-file")

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

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

0 comments on commit 701632d

Please sign in to comment.