Skip to content

Commit

Permalink
feat: improve error handling (#6)
Browse files Browse the repository at this point in the history
* test: add test that checking out invalid Git repo

* feat: add support to pass `ERROR_VARIABLE` to `git_checkout` function
  • Loading branch information
threeal authored Mar 9, 2024
1 parent 0712eb8 commit 380c0be
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
10 changes: 9 additions & 1 deletion cmake/GitCheckout.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ include_guard(GLOBAL)

# Clones and checks out a Git repository from a remote location.
#
# If the `ERROR_VARIABLE` is specified, it will set the error message to that variable.
# Otherwise, it will print the error message and halt the execution.
#
# Arguments:
# - URL: The URL of the remote Git repository.
function(git_checkout URL)
cmake_parse_arguments(ARG "" "ERROR_VARIABLE" "" ${ARGN})
execute_process(
COMMAND git clone ${URL}
RESULT_VARIABLE RES
)
if(NOT RES EQUAL 0)
message(FATAL_ERROR "Failed to clone ${URL} (${RES})")
if(ARG_ERROR_VARIABLE)
set(${ARG_ERROR_VARIABLE} "Failed to clone ${URL} (${RES})" PARENT_SCOPE)
else()
message(FATAL_ERROR "Failed to clone ${URL} (${RES})")
endif()
endif()
endfunction()
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ endfunction()
add_cmake_test(
GitCheckoutTest.cmake
"Check out a Git repository"
"Check out an invalid Git repository"
"Check out an invalid Git repository with error variable specified"
)
41 changes: 40 additions & 1 deletion test/GitCheckoutTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ endif()

set(TEST_COUNT 0)

# Add more test cases here.
if("Check out a Git repository" MATCHES ${TEST_MATCHES})
math(EXPR TEST_COUNT "${TEST_COUNT} + 1")

Expand All @@ -21,6 +20,46 @@ if("Check out a Git repository" MATCHES ${TEST_MATCHES})
endif()
endif()

if("Check out an invalid Git repository" MATCHES ${TEST_MATCHES})
math(EXPR TEST_COUNT "${TEST_COUNT} + 1")

set(MOCK_MESSAGE on)
macro(message MODE MESSAGE)
if(MOCK_MESSAGE)
if(${MODE} STREQUAL FATAL_ERROR)
set(FATAL_ERROR_MESSAGE "${MESSAGE}" PARENT_SCOPE)
endif()
else()
_message(${MODE} ${MESSAGE})
endif()
endmacro()

include(GitCheckout)
git_checkout(https://github.com/threeal/invalid-project)

set(MOCK_MESSAGE off)

set(EXPECTED_FATAL_ERROR_MESSAGE "Failed to clone https://github.com/threeal/invalid-project (128)")
if(NOT FATAL_ERROR_MESSAGE STREQUAL EXPECTED_FATAL_ERROR_MESSAGE)
message(FATAL_ERROR "It should fail to check out because of `${EXPECTED_FATAL_ERROR_MESSAGE}`, but got `${FATAL_ERROR_MESSAGE}` instead")
endif()
endif()

if("Check out an invalid Git repository with error variable specified" MATCHES ${TEST_MATCHES})
math(EXPR TEST_COUNT "${TEST_COUNT} + 1")

include(GitCheckout)
git_checkout(
https://github.com/threeal/invalid-project
ERROR_VARIABLE ERR
)

set(EXPECTED_FATAL_ERROR_MESSAGE "Failed to clone https://github.com/threeal/invalid-project (128)")
if(NOT ${ERR} STREQUAL EXPECTED_FATAL_ERROR_MESSAGE)
message(FATAL_ERROR "It should fail to check out because of `${EXPECTED_FATAL_ERROR_MESSAGE}`, but got `${ERR}` instead")
endif()
endif()

if(TEST_COUNT LESS_EQUAL 0)
message(FATAL_ERROR "Nothing to test with: ${TEST_MATCHES}")
endif()

0 comments on commit 380c0be

Please sign in to comment.