Skip to content

Commit

Permalink
test: add test for testing setup_go function
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Apr 21, 2024
1 parent baebaaf commit 2e90396
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.19)
cmake_minimum_required(VERSION 3.29)

project(
SetupGo
Expand Down
6 changes: 6 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function(add_cmake_test FILE)
NAME ${NAME}
COMMAND ${CMAKE_COMMAND}
-D CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}
-D CMAKE_BUILD_DIR=${CMAKE_CURRENT_SOURCE_DIR}/build
-D TEST_MATCHES=^${NAME}$
-P ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
)
Expand All @@ -15,3 +16,8 @@ add_cmake_test(
"Create directory recursively"
# Add more test cases here.
)

add_cmake_test(
cmake/SetupGoTest.cmake
"Set up the latest version of Go"
)
38 changes: 38 additions & 0 deletions test/cmake/SetupGoTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Matches everything if not defined
if(NOT TEST_MATCHES)
set(TEST_MATCHES ".*")
endif()

set(TEST_COUNT 0)

include(SetupGo)

if("Set up the latest version of Go" MATCHES ${TEST_MATCHES})
math(EXPR TEST_COUNT "${TEST_COUNT} + 1")

setup_go()

if(NOT DEFINED GO_EXECUTABLE)
message(FATAL_ERROR "The GO_EXECUTABLE variable should be defined")
endif()

if(NOT EXISTS ${GO_EXECUTABLE})
message(FATAL_ERROR "The Go executable at '${GO_EXECUTABLE}' should exist")
endif()

if(NOT IS_EXECUTABLE ${GO_EXECUTABLE})
message(FATAL_ERROR "The Go executable at '${GO_EXECUTABLE}' should be executable")
endif()

execute_process(
COMMAND ${GO_EXECUTABLE} version
RESULT_VARIABLE RES
)
if(NOT RES EQUAL 0)
message(FATAL_ERROR "It should not fail to execute the Go executable")
endif()
endif()

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

0 comments on commit 2e90396

Please # to comment.