From f7121e69fa0b057e68291813cd8045e3885d0e08 Mon Sep 17 00:00:00 2001
From: Yichao Yu <yyc1992@gmail.com>
Date: Sun, 8 Sep 2013 22:40:11 -0400
Subject: [PATCH] Add test for end of configure hook.

---
 Tests/CMakeTests/CMakeLists.txt                    | 11 ++++
 Tests/CMakeTests/EndOfConfigHook/CMakeLists.txt    | 61 ++++++++++++++++++++++
 .../CMakeTests/EndOfConfigHook/sub/CMakeLists.txt  |  0
 3 files changed, 72 insertions(+)
 create mode 100644 Tests/CMakeTests/EndOfConfigHook/CMakeLists.txt
 create mode 100644 Tests/CMakeTests/EndOfConfigHook/sub/CMakeLists.txt

diff --git a/Tests/CMakeTests/CMakeLists.txt b/Tests/CMakeTests/CMakeLists.txt
index 344b772..1003dc2 100644
--- a/Tests/CMakeTests/CMakeLists.txt
+++ b/Tests/CMakeTests/CMakeLists.txt
@@ -69,3 +69,14 @@ if(GIT_EXECUTABLE)
     )
   AddCMakeTest(CheckSourceTree "${CheckSourceTree_PreArgs}")
 endif()
+set(build_generator_args
+  --build-generator ${CMAKE_TEST_GENERATOR}
+  --build-makeprogram ${CMAKE_TEST_MAKEPROGRAM}
+  )
+add_test(CMake.EndOfConfigHook ${CMAKE_CTEST_COMMAND}
+  --build-and-test
+  "${CMAKE_CURRENT_SOURCE_DIR}/EndOfConfigHook"
+  "${CMAKE_CURRENT_BINARY_DIR}/EndOfConfigHook"
+  ${build_generator_args}
+  --build-project EndOfConfigHook
+  )
diff --git a/Tests/CMakeTests/EndOfConfigHook/CMakeLists.txt b/Tests/CMakeTests/EndOfConfigHook/CMakeLists.txt
new file mode 100644
index 0000000..b155803
--- /dev/null
+++ b/Tests/CMakeTests/EndOfConfigHook/CMakeLists.txt
@@ -0,0 +1,61 @@
+cmake_minimum_required(VERSION 2.8.3)
+project(EndOfConfigHook)
+
+function(rm_if_exist fname)
+  if(EXISTS "${fname}")
+    file(REMOVE "${fname}")
+  endif()
+endfunction()
+rm_if_exist("${CMAKE_CURRENT_BINARY_DIR}/random.dst1")
+rm_if_exist("${CMAKE_CURRENT_BINARY_DIR}/random.dst2")
+
+function(var_exist var)
+  if("${${var}}" STREQUAL "")
+    message(FATAL_ERROR "${var} is Empty.")
+  endif()
+endfunction()
+var_exist(CMAKE_CURRENT_LIST_DIR)
+var_exist(CMAKE_PARENT_LIST_FILE)
+
+string(RANDOM LENGTH 1024 random_msg)
+
+function(write_dst1 var access value)
+  if(NOT "${${var}}" STREQUAL "${value}")
+    message(FATAL_ERROR "Value of ${var} is different from the thrid arguement passed to the watch function (${value}).")
+  endif()
+  if("${value}" STREQUAL "")
+    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/random.dst1" "${random_msg}")
+  endif()
+endfunction()
+
+function(write_dst2 var access value)
+  if(NOT "${${var}}" STREQUAL "${value}")
+    message(FATAL_ERROR "Value of ${var} is different from the thrid arguement passed to the watch function (${value}).")
+  endif()
+  if("${value}" STREQUAL "")
+    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/random.dst2" "${random_msg}")
+  endif()
+endfunction()
+
+file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/random.dst0" "${random_msg}")
+variable_watch(CMAKE_CURRENT_LIST_DIR write_dst1)
+variable_watch(CMAKE_PARENT_LIST_FILE write_dst2)
+
+add_subdirectory(sub)
+if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/random.dst2" OR
+    EXISTS "${CMAKE_CURRENT_BINARY_DIR}/random.dst1")
+  message(FATAL_ERROR "End of configure hook is incorrectly triggered.")
+endif()
+
+add_custom_target(test-hook-fired ALL
+  COMMAND "${CMAKE_COMMAND}" -E compare_files
+  "${CMAKE_CURRENT_BINARY_DIR}/random.dst0"
+  "${CMAKE_CURRENT_BINARY_DIR}/random.dst1"
+  COMMAND "${CMAKE_COMMAND}" -E echo "CMAKE_CURRENT_LIST_DIR hook triggered."
+  COMMAND "${CMAKE_COMMAND}" -E compare_files
+  "${CMAKE_CURRENT_BINARY_DIR}/random.dst0"
+  "${CMAKE_CURRENT_BINARY_DIR}/random.dst2"
+  COMMAND "${CMAKE_COMMAND}" -E echo "CMAKE_PARENT_LIST_FILE hook triggered."
+  DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/random.dst0"
+  "${CMAKE_CURRENT_BINARY_DIR}/random.dst1"
+  "${CMAKE_CURRENT_BINARY_DIR}/random.dst2")
diff --git a/Tests/CMakeTests/EndOfConfigHook/sub/CMakeLists.txt b/Tests/CMakeTests/EndOfConfigHook/sub/CMakeLists.txt
new file mode 100644
index 0000000..e69de29
-- 
1.8.4

