Selaa lähdekoodia

Merge topic 'string-json-better-error'

35f002e293 string(JSON): Include invalid JSON in parse error

Acked-by: Kitware Robot <[email protected]>
Merge-request: !10944
Brad King 4 kuukautta sitten
vanhempi
sitoutus
f19fa50b48

+ 2 - 1
Source/cmStringCommand.cxx

@@ -1059,7 +1059,8 @@ Json::Value ReadJson(std::string const& jsonstr)
   std::string error;
   if (!jsonReader->parse(jsonstr.data(), jsonstr.data() + jsonstr.size(),
                          &json, &error)) {
-    throw json_error(cmStrCat("failed parsing json string: "_s, error));
+    throw json_error(
+      cmStrCat("failed parsing json string:\n"_s, jsonstr, '\n', error));
   }
   return json;
 }

+ 20 - 18
Tests/RunCMake/Instrumentation/hook.cmake

@@ -53,24 +53,26 @@ foreach(i RANGE ${length})
 endforeach()
 
 has_key_index(staticSystemInformation "${contents}" ${hasStaticInfo})
-has_key_index(OSName "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(OSPlatform "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(OSRelease "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(OSVersion "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(familyId "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(hostname "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(is64Bits "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(modelId "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(numberOfLogicalCPU "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(numberOfPhysicalCPU "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(processorAPICID "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(processorCacheSize "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(processorClockFrequency "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(processorName "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(totalPhysicalMemory "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(totalVirtualMemory "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(vendorID "${staticSystemInformation}" ${hasStaticInfo})
-has_key_index(vendorString "${staticSystemInformation}" ${hasStaticInfo})
+if (NOT hasStaticInfo STREQUAL UNEXPECTED)
+  has_key_index(OSName "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(OSPlatform "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(OSRelease "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(OSVersion "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(familyId "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(hostname "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(is64Bits "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(modelId "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(numberOfLogicalCPU "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(numberOfPhysicalCPU "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(processorAPICID "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(processorCacheSize "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(processorClockFrequency "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(processorName "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(totalPhysicalMemory "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(totalVirtualMemory "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(vendorID "${staticSystemInformation}" ${hasStaticInfo})
+  has_key_index(vendorString "${staticSystemInformation}" ${hasStaticInfo})
+endif()
 
 get_filename_component(dataDir ${index} DIRECTORY)
 get_filename_component(v1 ${dataDir} DIRECTORY)

+ 1 - 0
Tests/RunCMake/string/JSONBadJson-result.txt

@@ -0,0 +1 @@
+1

+ 26 - 0
Tests/RunCMake/string/JSONBadJson-stderr.txt

@@ -0,0 +1,26 @@
+CMake Error at JSONBadJson.cmake:1 \(string\):
+  string sub-command JSON failed parsing json string:
+
+  invalid
+
+  \* Line 1, Column 1
+
+    Syntax error: value, object or array expected.
+
+  .
+Call Stack \(most recent call first\):
+  CMakeLists\.txt:[0-9]+ \(include\)
+
+
+CMake Error at JSONBadJson.cmake:2 \(string\):
+  string sub-command JSON failed parsing json string:
+
+  value
+
+  \* Line 1, Column 1
+
+    Syntax error: value, object or array expected.
+
+  .
+Call Stack \(most recent call first\):
+  CMakeLists\.txt:[0-9]+ \(include\)

+ 3 - 0
Tests/RunCMake/string/JSONBadJson.cmake

@@ -0,0 +1,3 @@
+string(JSON badInput SET invalid key \"value\")
+string(JSON badValue SET {} key value)
+string(JSON good SET {} key \"value\")

+ 1 - 0
Tests/RunCMake/string/RunCMakeTest.cmake

@@ -6,6 +6,7 @@ run_cmake(JSONNoJson)
 run_cmake(JSONWrongMode)
 run_cmake(JSONOneArg)
 run_cmake(JSONNoArgs)
+run_cmake(JSONBadJson)
 
 run_cmake(Append)
 run_cmake(AppendNoArgs)