Skip to content

Commit

Permalink
feat: add support to format target existence assertion errors (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal authored Jul 6, 2024
1 parent a5bbca9 commit dcd8ceb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ function(assert)
elseif(ARGV0 STREQUAL "POLICY")
fail("expected policy" ARGV1 "to exist")
return()
elseif(ARGV0 STREQUAL "TARGET")
fail("expected target" ARGV1 "to exist")
return()
elseif(ARGV0 STREQUAL "DEFINED")
fail("expected variable" ARGV1 "to be defined")
return()
Expand All @@ -145,6 +148,9 @@ function(assert)
elseif(ARGV1 STREQUAL "POLICY")
fail("expected policy" ARGV2 "not to exist")
return()
elseif(ARGV1 STREQUAL "TARGET")
fail("expected target" ARGV2 "not to exist")
return()
elseif(ARGV1 STREQUAL "DEFINED")
fail("expected variable" ARGV2 "not to be defined")
return()
Expand Down
39 changes: 39 additions & 0 deletions test/assert.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,45 @@ section("policy existence condition assertions")
endsection()
endsection()

section("target existence condition assertions")
file(MAKE_DIRECTORY project)

section("it should assert target existence conditions")
file(
WRITE project/CMakeLists.txt
"cmake_minimum_required(VERSION 3.5)\n"
"project(SomeProject)\n"
"\n"
"add_custom_target(some_target)\n"
"\n"
"include(${ASSERTION_LIST_FILE})\n"
"\n"
"assert(TARGET some_target)\n"
"assert(NOT TARGET non_existing_target)\n")
assert_execute_process("${CMAKE_COMMAND}" project -B project/build)
endsection()

section("it should fail to assert target existence conditions")
file(
WRITE project/CMakeLists.txt
"cmake_minimum_required(VERSION 3.5)\n"
"project(SomeProject)\n"
"\n"
"add_custom_target(some_target)\n"
"\n"
"include(${ASSERTION_LIST_FILE})\n"
"\n"
"assert_fatal_error(\n"
" CALL assert TARGET non_existing_target\n"
" MESSAGE \"expected target:\\n non_existing_target\\nto exist\")\n"
"\n"
"assert_fatal_error(\n"
" CALL assert NOT TARGET some_target\n"
" MESSAGE \"expected target:\\n some_target\\nnot to exist\")\n")
assert_execute_process("${CMAKE_COMMAND}" project -B project/build)
endsection()
endsection()

section("variable existence condition assertions")
set(EXISTING_VARIABLE TRUE)
unset(NON_EXISTING_VARIABLE)
Expand Down

0 comments on commit dcd8ceb

Please sign in to comment.