From 02a34a61fa1dcb77a0cb8a76c87130f18e46dbb2 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Thu, 17 Oct 2024 07:02:51 +0700 Subject: [PATCH] feat: add `EXPECT_FAIL` option to `assert_execute_process` function Signed-off-by: Alfi Maulana --- README.md | 3 ++- cmake/Assertion.cmake | 9 +++++---- test/assert_execute_process.cmake | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 81a4871..c76cb90 100644 --- a/README.md +++ b/README.md @@ -208,11 +208,12 @@ Asserts whether the given command correctly executes a process. ```cmake assert_execute_process( [COMMAND] [...] + [EXPECT_FAIL] [EXPECT_OUTPUT ...] [EXPECT_ERROR ...]) ``` -This function asserts whether the given `` and `` 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 `` and `` 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 ``. If more than one `` string is given, they are concatenated into a single output with no separator between the strings. diff --git a/cmake/Assertion.cmake b/cmake/Assertion.cmake index f7b807b..c2f5cbb 100644 --- a/cmake/Assertion.cmake +++ b/cmake/Assertion.cmake @@ -429,12 +429,13 @@ endfunction() # # assert_execute_process( # [COMMAND] [...] +# [EXPECT_FAIL] # [EXPECT_OUTPUT ...] # [EXPECT_ERROR ...]) # # This function asserts whether the given `` and `` -# 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 ``. If more than one `` @@ -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}) @@ -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") diff --git a/test/assert_execute_process.cmake b/test/assert_execute_process.cmake index 7b34f13..db2f28f 100644 --- a/test/assert_execute_process.cmake +++ b/test/assert_execute_process.cmake @@ -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() @@ -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")