Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent add_cmake_script_test from Being Called in Script Mode #272

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ set(ASSERTION_VERSION 2.0.0)
#
# add_cmake_script_test(<file> [NAME <name>] [DEFINITIONS <variables>...])
#
#
# This function adds a new test that processes the specified `<file>` in script
# mode. If `NAME` is provided, `<name>` will be used as the test name;
# otherwise, the test name will default to `<file>`.
Expand All @@ -38,15 +37,20 @@ set(ASSERTION_VERSION 2.0.0)
# and `<value>` is its value. If `DEFINITIONS` is specified, additional
# variables will also be defined.
function(add_cmake_script_test FILE)
cmake_parse_arguments(PARSE_ARGV 1 ARG "" NAME DEFINITIONS)
if(DEFINED CMAKE_SCRIPT_MODE_FILE)
message(SEND_ERROR "Unable to add a new test in script mode")
return()
endif()

cmake_parse_arguments(PARSE_ARGV 1 ARG "" NAME DEFINITIONS)
if(NOT DEFINED ARG_NAME)
set(ARG_NAME "${FILE}")
endif()

cmake_path(ABSOLUTE_PATH FILE)
if(NOT EXISTS ${FILE})
message(SEND_ERROR "Cannot find test file:\n ${FILE}")
return()
endif()

set(TEST_COMMAND "${CMAKE_COMMAND}")
Expand Down
12 changes: 11 additions & 1 deletion test/add_cmake_script_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,19 @@ section("it should create a new test with predefined variables")
ctest --test-dir project/build --no-tests=error)
endsection()

file(REMOVE project/test.cmake)
section("it should fail to create a new test due to script mode")
file(WRITE project/script.cmake
"include(${ASSERTION_LIST_FILE})\n"
"add_cmake_script_test(test.cmake)\n")

assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -P project/script.cmake
EXPECT_ERROR "Unable to add a new test in script mode")
endsection()

section("it should fail to create a new test due to a non-existent file")
file(REMOVE project/test.cmake)

assert_execute_process(
COMMAND "${CMAKE_COMMAND}" --fresh -S project -B project/build
EXPECT_ERROR "Cannot find test file:.*test.cmake")
Expand Down