Author: rinrab
Date: Wed Jan 29 18:51:10 2025
New Revision: 1923451

URL: http://svn.apache.org/viewvc?rev=1923451&view=rev
Log:
CMake: Speed up test discovery when expanded by using a Python script that
lists all the tests in a single invocation. This optimizes the amount of
executables we run, because now we need to once run the script instead of
doing this for each test script. Now we can discover all the tests instantly,
without any additional time needed for this.

* build/list_tests.py: New script for listing all the tests in many scripts
  at the same time.
* CMakeLists.txt
  (tests): Use the new script to list the tests if expand is enables,
   otherwise just enumerate the files and add them without any manipulations
   as it was before.

Added:
    subversion/trunk/build/list_tests.py   (with props)
Modified:
    subversion/trunk/CMakeLists.txt

Modified: subversion/trunk/CMakeLists.txt
URL: 
http://svn.apache.org/viewvc/subversion/trunk/CMakeLists.txt?rev=1923451&r1=1923450&r2=1923451&view=diff
==============================================================================
--- subversion/trunk/CMakeLists.txt (original)
+++ subversion/trunk/CMakeLists.txt Wed Jan 29 18:51:10 2025
@@ -771,6 +771,7 @@ include("build/cmake/targets.cmake")
 if(SVN_ENABLE_TESTS)
   find_package(Python3 COMPONENTS Interpreter REQUIRED)
   set(run_tests_script "${CMAKE_CURRENT_SOURCE_DIR}/build/run_tests.py")
+  set(list_tests_script "${CMAKE_CURRENT_SOURCE_DIR}/build/list_tests.py")
 
   function(add_py_test name prog)
     if(SVN_TEST_CONFIGURE_FOR_PARALLEL)
@@ -803,41 +804,50 @@ if(SVN_ENABLE_TESTS)
      "subversion/tests/cmdline/*_tests.py"
   )
 
-  foreach(py_test_abspath ${PYTHON_TESTS})
-    # Keep `.py'.
-    get_filename_component(py_test_name ${py_test_abspath} NAME_WLE)
-    file(RELATIVE_PATH py_test_relpath ${CMAKE_CURRENT_SOURCE_DIR} 
${py_test_abspath})
-    set(binary_dir $<TARGET_FILE_DIR:svn>)
-
-    if(SVN_TEST_EXPAND)
-      message("Listing tests in ${py_test_name}")
-      execute_process(
-        COMMAND
-          "${Python3_EXECUTABLE}" "${run_tests_script}"
-          --log-to-stdout
-          --list
-          ${CMAKE_CURRENT_SOURCE_DIR}
-          ${binary_dir}
-          ${py_test_abspath}
-          OUTPUT_VARIABLE tests_list_output
-      )
-      string(REGEX MATCHALL "\n *([0-9]+)" tests_list ${tests_list_output})
+  set(binary_dir $<TARGET_FILE_DIR:svn>)
+
+  if(SVN_TEST_EXPAND)
+    execute_process(
+      COMMAND
+        "${Python3_EXECUTABLE}" "${list_tests_script}"
+        ${PYTHON_TESTS}
+        OUTPUT_VARIABLE tests_list_output
+        WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
+        RESULT_VARIABLE command_result
+    )
+
+    if (command_result)
+      message(FATAL_ERROR "list_tests.py failed.")
+    endif()
 
-      foreach(test_num ${tests_list})
-        string(REGEX MATCH "([0-9]+)" test_num ${test_num})
+    string(REGEX MATCHALL "[^\n\r]+" tests_list "${tests_list_output}")
+
+    foreach(test_case ${tests_list})
+      if(test_case MATCHES "(.+)#(.+)")
+        set(py_test_abspath "${CMAKE_MATCH_1}")
+        set(py_test_num "${CMAKE_MATCH_2}")
+
+        get_filename_component(py_test_name ${py_test_abspath} NAME_WLE)
+        file(RELATIVE_PATH py_test_relpath ${CMAKE_CURRENT_SOURCE_DIR} 
${py_test_abspath})
 
         add_py_test(
-          "cmdline.${py_test_name}.${test_num}"
-          "${py_test_relpath}#${test_num}"
+          "cmdline.${py_test_name}.${py_test_num}"
+          "${py_test_relpath}#${py_test_num}"
         )
-      endforeach()
-    else()
+      endif()
+    endforeach()
+  else()
+    foreach(py_test_abspath ${PYTHON_TESTS})
+      # Keep `.py'.
+      get_filename_component(py_test_name ${py_test_abspath} NAME_WLE)
+      file(RELATIVE_PATH py_test_relpath ${CMAKE_CURRENT_SOURCE_DIR} 
${py_test_abspath})
+
       add_py_test(
         "cmdline.${py_test_name}"
         "${py_test_relpath}"
       )
-    endif()
-  endforeach()
+    endforeach()
+  endif()
 endif()
 
 if (SVN_ENABLE_SVNXX)

Added: subversion/trunk/build/list_tests.py
URL: 
http://svn.apache.org/viewvc/subversion/trunk/build/list_tests.py?rev=1923451&view=auto
==============================================================================
--- subversion/trunk/build/list_tests.py (added)
+++ subversion/trunk/build/list_tests.py Wed Jan 29 18:51:10 2025
@@ -0,0 +1,90 @@
+#!/usr/bin/env python
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+# list_tests.py - lists all the tests in a Python test script
+#
+
+'''usage: python list_tests.py <prog ...>
+
+This script will print out all the tests in the `TESTPATH#TESTNUM`
+format for each of these tests.
+'''
+
+import os, sys
+import traceback
+
+if sys.version_info < (3, 5):
+  import imp
+else:
+  # The imp module is deprecated since Python 3.4; the replacement we use,
+  # module_from_spec(), is available since Python 3.5.
+  import importlib.util
+
+# Placeholder for the svntest module
+svntest = None
+
+tests = sys.argv[1:]
+
+def _load_py_test_module(progabs, modname):
+  'Run a python test, passing parameters as needed.'
+  try:
+    if sys.version_info < (3, 0):
+      prog_mod = imp.load_module(modname, open(progabs, 'r'), progabs,
+                                  ('.py', 'U', imp.PY_SOURCE))
+    elif sys.version_info < (3, 5):
+      prog_mod = imp.load_module(modname,
+                                  open(progabs, 'r', encoding="utf-8"),
+                                  progabs, ('.py', 'U', imp.PY_SOURCE))
+    else:
+        spec = importlib.util.spec_from_file_location(modname, progabs)
+        prog_mod = importlib.util.module_from_spec(spec)
+        sys.modules[modname] = prog_mod
+        spec.loader.exec_module(prog_mod)
+  except:
+    print("\nError loading test (details in following traceback): " + modname)
+    traceback.print_exc()
+    sys.exit(1)
+
+  return prog_mod
+
+basedir = os.path.join("subversion/tests/cmdline")
+
+# The svntest module is very pedantic about the current working directory
+old_cwd = os.getcwd()
+try:
+  sys.path.insert(0, os.path.abspath(basedir))
+
+  os.chdir(basedir)
+
+  __import__('svntest')
+  __import__('svntest.main')
+  __import__('svntest.testcase')
+  svntest = sys.modules['svntest']
+  svntest.main = sys.modules['svntest.main']
+  svntest.testcase = sys.modules['svntest.testcase']
+
+finally:
+  os.chdir(old_cwd)
+
+for progabs in tests:
+  modname = os.path.basename(progabs)[:-3]
+
+  testlist = _load_py_test_module(progabs, modname).test_list
+  for testnum in range(1, len(testlist)):
+    print(progabs + "#" + str(testnum))

Propchange: subversion/trunk/build/list_tests.py
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to