From 6f934896fbc682e1d9ab85d18bccbe40a5d7cbe5 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Fri, 7 Jun 2024 14:12:47 +0700 Subject: [PATCH] feat: show error in execute process assertion (#90) * test: modify execute process assertion tests to call command with error message * feat: show error if execute process failed to run during assertion --- cmake/Assertion.cmake | 3 ++- test/AssertExecuteProcess.cmake | 33 +++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/cmake/Assertion.cmake b/cmake/Assertion.cmake index 0f7342b..cbe04bc 100644 --- a/cmake/Assertion.cmake +++ b/cmake/Assertion.cmake @@ -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}") diff --git a/test/AssertExecuteProcess.cmake b/test/AssertExecuteProcess.cmake index 72563ec..5460bbc 100644 --- a/test/AssertExecuteProcess.cmake +++ b/test/AssertExecuteProcess.cmake @@ -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") @@ -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()