Merge topic 'instrumentation-cdash-file-check'

77443e7258 instrumentation: Don't expect staged CDash files when cdashSubmit is disabled

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Tested-by: Aiden Woodruff <aiden.woodruff@kitware.com>
Merge-request: !12508
diff --git a/Source/CTest/cmCTestBuildHandler.cxx b/Source/CTest/cmCTestBuildHandler.cxx
index c7e7c3d..44bc963 100644
--- a/Source/CTest/cmCTestBuildHandler.cxx
+++ b/Source/CTest/cmCTestBuildHandler.cxx
@@ -608,6 +608,11 @@
 
 void cmCTestBuildHandler::GenerateInstrumentationXML(cmXMLWriter& xml)
 {
+  if (!this->CTest->GetInstrumentation().HasOption(
+        cmInstrumentationQuery::Option::CDashSubmit)) {
+    return;
+  }
+
   // Record instrumentation data on a per-target basis.
   cmsys::Directory targets_dir;
   std::string targets_snippet_dir = cmStrCat(
diff --git a/Source/CTest/cmCTestTestHandler.cxx b/Source/CTest/cmCTestTestHandler.cxx
index f19c525..9e7e7a5 100644
--- a/Source/CTest/cmCTestTestHandler.cxx
+++ b/Source/CTest/cmCTestTestHandler.cxx
@@ -1659,7 +1659,9 @@
     xml.EndElement(); // Value
     xml.EndElement(); // Measurement
 
-    if (!result.InstrumentationFile.empty()) {
+    if (!result.InstrumentationFile.empty() &&
+        this->CTest->GetInstrumentation().HasOption(
+          cmInstrumentationQuery::Option::CDashSubmit)) {
       std::string instrument_file_path =
         cmStrCat(this->CTest->GetInstrumentation().GetCDashDir(), "/test/",
                  result.InstrumentationFile);
diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx
index 02e6692..fbd9398 100644
--- a/Source/cmCTest.cxx
+++ b/Source/cmCTest.cxx
@@ -3869,6 +3869,11 @@
 void cmCTest::ConvertInstrumentationSnippetsToXML(cmXMLWriter& xml,
                                                   std::string const& subdir)
 {
+  if (!this->GetInstrumentation().HasOption(
+        cmInstrumentationQuery::Option::CDashSubmit)) {
+    return;
+  }
+
   std::string data_dir =
     cmStrCat(this->GetInstrumentation().GetCDashDir(), '/', subdir);
 
diff --git a/Tests/RunCMake/ctest_instrumentation/InstrumentationWithoutCDashSubmit-check.cmake b/Tests/RunCMake/ctest_instrumentation/InstrumentationWithoutCDashSubmit-check.cmake
new file mode 100644
index 0000000..224b91c
--- /dev/null
+++ b/Tests/RunCMake/ctest_instrumentation/InstrumentationWithoutCDashSubmit-check.cmake
@@ -0,0 +1,28 @@
+include("${RunCMake_SOURCE_DIR}/NoInstrumentationInCTestXML-check.cmake")
+
+# Instrumentation should still produce snippets, but CTest must not look
+# for their CDash staging copies when cdashSubmit has not been requested.
+set(instrumentation_dir "${RunCMake_TEST_BINARY_DIR}/.cmake/instrumentation/v1")
+if(ARGS_USE_STALE_CDASH)
+  foreach(subdir configure build/commands build/targets/old-target)
+    if(NOT EXISTS "${instrumentation_dir}/cdash/${subdir}/old.json")
+      set(RunCMake_TEST_FAILED "Stale CDash snippet was consumed: ${subdir}/old.json")
+      return()
+    endif()
+  endforeach()
+elseif(EXISTS "${instrumentation_dir}/cdash")
+  set(RunCMake_TEST_FAILED "Unexpected CDash staging directory")
+  return()
+endif()
+file(GLOB snippets "${instrumentation_dir}/data/test-*.json")
+list(LENGTH snippets count)
+if(NOT count EQUAL 1)
+  set(RunCMake_TEST_FAILED "Expected one local test snippet, found ${count}")
+  return()
+endif()
+file(READ "${snippets}" snippet)
+string(JSON role GET "${snippet}" role)
+string(JSON test_name GET "${snippet}" testName)
+if(NOT role STREQUAL "test" OR NOT test_name STREQUAL "main")
+  set(RunCMake_TEST_FAILED "Unexpected local test snippet: ${snippet}")
+endif()
diff --git a/Tests/RunCMake/ctest_instrumentation/InstrumentationWithoutCDashSubmitWithStaleData-check.cmake b/Tests/RunCMake/ctest_instrumentation/InstrumentationWithoutCDashSubmitWithStaleData-check.cmake
new file mode 100644
index 0000000..74840e1
--- /dev/null
+++ b/Tests/RunCMake/ctest_instrumentation/InstrumentationWithoutCDashSubmitWithStaleData-check.cmake
@@ -0,0 +1 @@
+include("${RunCMake_SOURCE_DIR}/InstrumentationWithoutCDashSubmit-check.cmake")
diff --git a/Tests/RunCMake/ctest_instrumentation/RunCMakeTest.cmake b/Tests/RunCMake/ctest_instrumentation/RunCMakeTest.cmake
index 586b9aa..968e9a6 100644
--- a/Tests/RunCMake/ctest_instrumentation/RunCMakeTest.cmake
+++ b/Tests/RunCMake/ctest_instrumentation/RunCMakeTest.cmake
@@ -1,7 +1,7 @@
 include(RunCTest)
 
 function(run_InstrumentationInCTestXML CASE_NAME)
-  cmake_parse_arguments(ARGS "USE_INSTRUMENTATION_ENV_VARS;USE_VERBOSE_INSTRUMENTATION;USE_INSTRUMENTATION_CMD" "" "" ${ARGN})
+  cmake_parse_arguments(ARGS "USE_INSTRUMENTATION_ENV_VARS;USE_VERBOSE_INSTRUMENTATION;USE_INSTRUMENTATION_CMD;USE_LOCAL_INSTRUMENTATION;USE_STALE_CDASH" "" "" ${ARGN})
   if(ARGS_USE_VERBOSE_INSTRUMENTATION)
     set(ENV{CTEST_USE_VERBOSE_INSTRUMENTATION} "1")
     set(RunCMake_USE_VERBOSE_INSTRUMENTATION 1)
@@ -22,6 +22,26 @@
     set(RunCMake_USE_VERBOSE_INSTRUMENTATION 0)
   endif()
 
+  if(ARGS_USE_LOCAL_INSTRUMENTATION)
+    set(CASE_CMAKELISTS_SUFFIX_CODE [[
+cmake_instrumentation(
+  API_VERSION 1
+  DATA_VERSION 1
+)
+]])
+  endif()
+
+  if(ARGS_USE_STALE_CDASH)
+    set(CASE_CTEST_PREFIX_CODE [=[
+# Simulate staged data left by an earlier run with cdashSubmit enabled.
+foreach(subdir configure build/commands build/targets/old-target)
+  file(WRITE "${CTEST_BINARY_DIRECTORY}/.cmake/instrumentation/v1/cdash/${subdir}/old.json"
+    [[{"command":"old","role":"custom","dynamicSystemInformation":{"afterHostMemoryUsed":123}}]]
+  )
+endforeach()
+]=])
+  endif()
+
   configure_file(${RunCMake_SOURCE_DIR}/main.c
                  ${RunCMake_BINARY_DIR}/${CASE_NAME}/main.c COPYONLY)
   run_ctest("${CASE_NAME}")
@@ -39,3 +59,9 @@
 run_InstrumentationInCTestXML(InstrumentationInCTestXMLWithCmd
   USE_INSTRUMENTATION_CMD
 )
+run_InstrumentationInCTestXML(InstrumentationWithoutCDashSubmit
+  USE_LOCAL_INSTRUMENTATION
+)
+run_InstrumentationInCTestXML(InstrumentationWithoutCDashSubmitWithStaleData
+  USE_LOCAL_INSTRUMENTATION USE_STALE_CDASH
+)
diff --git a/Tests/RunCMake/ctest_instrumentation/test.cmake.in b/Tests/RunCMake/ctest_instrumentation/test.cmake.in
index bb2b924..51a3de1 100644
--- a/Tests/RunCMake/ctest_instrumentation/test.cmake.in
+++ b/Tests/RunCMake/ctest_instrumentation/test.cmake.in
@@ -11,6 +11,7 @@
 set(CTEST_USE_LAUNCHERS                 TRUE)
 
 ctest_start(Experimental)
+@CASE_CTEST_PREFIX_CODE@
 ctest_configure()
 ctest_build()
 ctest_test()