Browse Source

cmCacheManager: Test and document newline truncation behavior

Kyle Edwards 8 years ago
parent
commit
c42b377c29

+ 7 - 0
Help/release/dev/cache-newline.rst

@@ -0,0 +1,7 @@
+cache-newline
+-------------
+
+* Variables containing newlines in their values now get truncated before the
+  newline when they are written to the cache file. In addition, a warning
+  comment is written to the cache file, and a warning message is displayed to
+  the user on the console.

+ 1 - 0
Tests/RunCMake/CMakeLists.txt

@@ -339,6 +339,7 @@ add_RunCMake_test(CPackInstallProperties)
 add_RunCMake_test(ExternalProject)
 add_RunCMake_test(FetchContent)
 add_RunCMake_test(CTestCommandLine)
+add_RunCMake_test(CacheNewline)
 # Only run this test on unix platforms that support
 # symbolic links
 if(UNIX)

+ 3 - 0
Tests/RunCMake/CacheNewline/CMakeLists.txt

@@ -0,0 +1,3 @@
+cmake_minimum_required(VERSION 3.5)
+project(${RunCMake_TEST} NONE)
+include(${RunCMake_TEST}.cmake)

+ 16 - 0
Tests/RunCMake/CacheNewline/CacheNewline-check.cmake

@@ -0,0 +1,16 @@
+set(CACHE_EXPECTED_FILE "${RunCMake_TEST_SOURCE_DIR}/cache-regex.txt")
+set(CACHE_ACTUAL_FILE "${RunCMake_BINARY_DIR}/CacheNewline-build/CMakeCache.txt")
+
+file(READ ${CACHE_EXPECTED_FILE} CACHE_EXPECTED)
+string(REGEX REPLACE "\r\n" "\n" CACHE_EXPECTED "${CACHE_EXPECTED}")
+string(REGEX REPLACE "\n+$" "" CACHE_EXPECTED "${CACHE_EXPECTED}")
+file(READ ${CACHE_ACTUAL_FILE} CACHE_ACTUAL)
+string(REGEX REPLACE "\r\n" "\n" CACHE_ACTUAL "${CACHE_ACTUAL}")
+string(REGEX REPLACE "\n+$" "" CACHE_ACTUAL "${CACHE_ACTUAL}")
+
+if(NOT "${CACHE_ACTUAL}" MATCHES "${CACHE_EXPECTED}")
+  set(RunCMake_TEST_FAILED "${CACHE_ACTUAL_FILE} does not match ${CACHE_EXPECTED_FILE}:
+
+CMakeCache.txt contents = [\n${CACHE_ACTUAL}\n]
+Expected = [\n${CACHE_EXPECTED}\n]")
+endif()

+ 2 - 0
Tests/RunCMake/CacheNewline/CacheNewline-stderr.txt

@@ -0,0 +1,2 @@
+CMake Warning:
+  Value of NEWLINE_VARIABLE contained a newline; truncating

+ 5 - 0
Tests/RunCMake/CacheNewline/CacheNewline.cmake

@@ -0,0 +1,5 @@
+cmake_minimum_required(VERSION 3.5)
+
+project(CacheNewlineTest NONE)
+
+set(NEWLINE_VARIABLE "a\nb" CACHE STRING "Offending entry")

+ 3 - 0
Tests/RunCMake/CacheNewline/RunCMakeTest.cmake

@@ -0,0 +1,3 @@
+include(RunCMake)
+
+run_cmake(CacheNewline)

+ 6 - 0
Tests/RunCMake/CacheNewline/cache-regex.txt

@@ -0,0 +1,6 @@
+//Offending entry
+NEWLINE_VARIABLE:STRING=a
+# WARNING: Value of NEWLINE_VARIABLE contained a newline and was
+#  truncated\. Original value:
+# a
+# \\nb