From 2e9039634dc8c3048d0fd111b44fed4a60eb44ab Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 21 Apr 2024 22:12:30 +0700 Subject: [PATCH] test: add test for testing `setup_go` function --- CMakeLists.txt | 2 +- test/CMakeLists.txt | 6 ++++++ test/cmake/SetupGoTest.cmake | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/cmake/SetupGoTest.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index ee6e149..3703936 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.19) +cmake_minimum_required(VERSION 3.29) project( SetupGo diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b506921..3855e30 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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} ) @@ -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" +) diff --git a/test/cmake/SetupGoTest.cmake b/test/cmake/SetupGoTest.cmake new file mode 100644 index 0000000..b541e3c --- /dev/null +++ b/test/cmake/SetupGoTest.cmake @@ -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()