Skip to content

Commit

Permalink
feat: replace sample function to git_clone 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 2, 2024
1 parent 6640d08 commit 947aa40
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if(MY_PROJECT_ENABLE_TESTS)
enable_testing()

find_package(Assertion 1.0.0 REQUIRED)
assertion_add_test(test/mkdir_recursive.cmake)
assertion_add_test(test/git_clone.cmake)
endif()

if(MY_PROJECT_ENABLE_INSTALL)
Expand Down
16 changes: 14 additions & 2 deletions cmake/MyProject.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function(mkdir_recursive DIR_PATH)
file(MAKE_DIRECTORY "${DIR_PATH}")
# Clone a Git repository to a specified directory.
#
# git_clone(<url> <dir>)
#
# This function clones a Git repository from the `<url>` and saves it to the
# `<dir>` directory. It outputs a fatal error message if the operation fails.
function(git_clone URL DIR)
execute_process(
COMMAND git clone "${URL}" "${DIR}"
ERROR_VARIABLE ERR
RESULT_VARIABLE RES)
if(NOT RES EQUAL 0)
message(FATAL_ERROR "Failed to clone '${URL}': ${ERR}")
endif()
endfunction()
20 changes: 20 additions & 0 deletions test/git_clone.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
find_package(MyProject REQUIRED PATHS ${CMAKE_CURRENT_LIST_DIR}/../cmake)

section("it should clone a Git repository")
file(REMOVE_RECURSE repo)

git_clone(https://github.com/threeal/cmake-starter repo)

assert(EXISTS repo)
assert_execute_process(git -C repo rev-parse --is-inside-work-tree)
endsection()

section("it should fail to clone an invalid Git repository")
file(REMOVE_RECURSE repo)

assert_fatal_error(
CALL git_clone https://github.com repo
MESSAGE "Failed to clone 'https://github.com'")

assert(NOT EXISTS repo)
endsection()
10 changes: 0 additions & 10 deletions test/mkdir_recursive.cmake

This file was deleted.

0 comments on commit 947aa40

Please sign in to comment.