From 88586eea44a76667b143a7565b2d5ba753dc8899 Mon Sep 17 00:00:00 2001 From: SoftFever Date: Wed, 9 Sep 2026 00:10:02 +0800 Subject: [PATCH] enable python unit test --- .gitignore | 3 ++- scripts/run_unit_tests.sh | 18 ++++++++++++++++++ src/slic3r/plugin/PythonInterpreter.cpp | 15 ++++++++++----- tests/slic3rutils/CMakeLists.txt | 15 +++++++++++---- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index a2a1520fd5..21befeb0dd 100644 --- a/.gitignore +++ b/.gitignore @@ -53,4 +53,5 @@ __pycache__/ *.pyc *.opc /.test/ -docs/superpowers/ \ No newline at end of file +docs/superpowers/ +ctest_results.xml \ No newline at end of file diff --git a/scripts/run_unit_tests.sh b/scripts/run_unit_tests.sh index f6a01dd3e0..f19e92e88a 100755 --- a/scripts/run_unit_tests.sh +++ b/scripts/run_unit_tests.sh @@ -17,6 +17,24 @@ cd "${ROOT_DIR}" || exit 1 TEST_DIR="${1:-build/tests}" BUILD_CONFIG="${2:-}" +# The slic3rutils plugin-host tests build numpy arrays through the CPython copied next +# to the test binary (see tests/slic3rutils/CMakeLists.txt); that runtime ships pip but +# no numpy, so those tests SKIP without it. Install it into that interpreter's own +# site-packages -- no PYTHONPATH needed, and re-checked every run because a rebuild of +# the test target wipes and re-copies the runtime. Needs network on the first run only; +# a failure here is not fatal, the tests just keep skipping. +find_args=("${TEST_DIR}" \( -path '*/python/bin/python3' -o -path '*/python/python.exe' \)) +# Multi-config trees hold one copy per configuration; only bootstrap the one being run. +[ -n "${BUILD_CONFIG}" ] && find_args+=(-path "*/${BUILD_CONFIG}/*") +python_exe="$(find "${find_args[@]}" -print -quit 2>/dev/null)" +if [ -z "${python_exe}" ]; then + echo "No bundled Python under ${TEST_DIR}; numpy-backed binding tests will skip." +elif ! "${python_exe}" -c "import numpy" >/dev/null 2>&1; then + echo "Installing numpy into the embedded test interpreter (${python_exe})..." + "${python_exe}" -m pip install --quiet --disable-pip-version-check --retries 1 "numpy<3" \ + || echo "numpy install failed; numpy-backed binding tests will skip." +fi + # Run the whole suite, excluding tests tagged [NotWorking]. # --no-tests=error fails the job if the filter matches nothing (instead of passing green). args=(--test-dir "${TEST_DIR}" -LE "NotWorking" --no-tests=error --output-junit "$(pwd)/ctest_results.xml" --output-on-failure -j) diff --git a/src/slic3r/plugin/PythonInterpreter.cpp b/src/slic3r/plugin/PythonInterpreter.cpp index 3d180c2f2a..d82bf52a13 100644 --- a/src/slic3r/plugin/PythonInterpreter.cpp +++ b/src/slic3r/plugin/PythonInterpreter.cpp @@ -345,16 +345,21 @@ boost::filesystem::path find_bundled_python_home() fs::path bundle_python = fs::path(resources_dir()).parent_path() / "MacOS" / "python"; if (valid_python_home(bundle_python)) return bundle_python; -#elif defined(_WIN32) - fs::path exe_python = boost::dll::program_location().parent_path() / "python"; - if (valid_python_home(exe_python)) - return exe_python; -#else +#elif !defined(_WIN32) fs::path linux_python = fs::path(resources_dir()).parent_path() / "lib" / "python"; if (valid_python_home(linux_python)) return linux_python; #endif + // Next to the executable: the Windows install layout, and the runtime copied + // beside every platform's unit-test binary (tests/slic3rutils/CMakeLists.txt). + // The CI test runner only receives the build/tests tree, so the candidates + // below -- all of which point into the deps or install trees -- never resolve + // there. + fs::path exe_python = boost::dll::program_location().parent_path() / "python"; + if (valid_python_home(exe_python)) + return exe_python; + fs::path configured_python = ORCA_BUNDLED_PYTHON_ROOT; if (!configured_python.empty() && valid_python_home(configured_python)) return configured_python; diff --git a/tests/slic3rutils/CMakeLists.txt b/tests/slic3rutils/CMakeLists.txt index 2ab78f56de..d5fe4f3d2f 100644 --- a/tests/slic3rutils/CMakeLists.txt +++ b/tests/slic3rutils/CMakeLists.txt @@ -42,9 +42,16 @@ if (WIN32) COMMENT "Copying Python runtime for slic3rutils plugin host API tests" VERBATIM ) -elseif (APPLE) - target_link_options(${_TEST_NAME}_tests PRIVATE - "LINKER:-rpath,@executable_path/python/lib") +else () + # The CI unit-test runner only receives the build/tests tree, so both the + # interpreter and the libpython the test binary links have to travel next to + # the executable; find_bundled_python_home() picks the copy up from there. + if (APPLE) + target_link_options(${_TEST_NAME}_tests PRIVATE + "LINKER:-rpath,@executable_path/python/lib") + else () + set_property(TARGET ${_TEST_NAME}_tests APPEND PROPERTY BUILD_RPATH "$ORIGIN/python/lib") + endif () add_custom_command(TARGET ${_TEST_NAME}_tests POST_BUILD COMMAND ${CMAKE_COMMAND} -E rm -rf @@ -52,7 +59,7 @@ elseif (APPLE) COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_PREFIX_PATH}/libpython" "$/python" - COMMENT "Copying Python runtime for macOS plugin host API tests" + COMMENT "Copying Python runtime for the plugin host API tests" VERBATIM ) endif()