Skip to content

Commit

Permalink
feat: allow test definitions to be specified without value
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 22, 2024
1 parent 923882f commit 3902d79
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ add_cmake_script_test(

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>`.

If the `CMAKE_SCRIPT_TEST_DEFINITIONS` variable is defined, the script will be processed with the predefined variables listed in that variable. Each entry should be in the format `<name>=<value>`, where `<name>` is the variable name and `<value>` is its value. If `DEFINITIONS` is specified, additional variables will also be defined.
If the `CMAKE_SCRIPT_TEST_DEFINITIONS` variable is defined, the script will be processed with the predefined variables listed in that variable. Each entry should be in the format `<name>=<value>`, where `<name>` is the variable name and `<value>` is its value. If `<value>` is not provided, it uses the value of a variable named `<name>` in the current CMake scope. If `DEFINITIONS` is specified, additional variables will also be defined.

#### Example

```cmake
add_cmake_script_test(test_foo.cmake NAME "Test Foo" DEFINITIONS FOO=foo BAR=bar)
set(BAR bar)
add_cmake_script_test(test_foo.cmake NAME "Test Foo" DEFINITIONS FOO=foo BAR)
```

The example above adds a new test named `Test Foo`, which processes the `test_foo.cmake` file in script mode with predefined `FOO` and `BAR` variables.
Expand Down
8 changes: 6 additions & 2 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ set(ASSERTION_VERSION 2.0.0)
# If the `CMAKE_SCRIPT_TEST_DEFINITIONS` variable is defined, the script will be
# processed with the predefined variables listed in that variable. Each entry
# should be in the format `<name>=<value>`, where `<name>` is the variable name
# and `<value>` is its value. If `DEFINITIONS` is specified, additional
# variables will also be defined.
# and `<value>` is its value. If `<value>` is not provided, it uses the value of
# a variable named `<name>` in the current CMake scope. If `DEFINITIONS` is
# specified, additional variables will also be defined.
function(add_cmake_script_test)
if(DEFINED CMAKE_SCRIPT_MODE_FILE)
message(SEND_ERROR "Unable to add a new test in script mode")
Expand All @@ -61,6 +62,9 @@ function(add_cmake_script_test)

set(TEST_COMMAND "${CMAKE_COMMAND}")
foreach(DEFINITION IN LISTS CMAKE_SCRIPT_TEST_DEFINITIONS ARG_DEFINITIONS)
if(NOT DEFINITION MATCHES =)
set(DEFINITION "${DEFINITION}=${${DEFINITION}}")
endif()
list(APPEND TEST_COMMAND -D "${DEFINITION}")
endforeach()
list(APPEND TEST_COMMAND -P "${ARG_FILE}")
Expand Down
3 changes: 2 additions & 1 deletion test/test_add_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ section("it should create a new test with predefined variables")

file(WRITE project/CMakeLists.txt ${CMAKELISTS_HEADER}
"set(CMAKE_SCRIPT_TEST_DEFINITIONS FOO=foo BAR=bar)\n"
"add_cmake_script_test(test.cmake DEFINITIONS BAR=barbar BAZ=baz)\n")
"set(BAR barbar)\n"
"add_cmake_script_test(test.cmake DEFINITIONS BAR BAZ=baz)\n")

assert_execute_process("${CMAKE_COMMAND}" --fresh -S project -B project/build)
assert_execute_process(
Expand Down

0 comments on commit 3902d79

Please sign in to comment.