|
| 1 | +#!/bin/bash |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +# Helper script to detect which test components should run based on changed files. |
| 6 | +# Source this script to set the CUOPT_TEST_COMPONENTS variable. |
| 7 | +# |
| 8 | +# Usage: source ci/detect_test_components.sh |
| 9 | +# |
| 10 | +# Sets CUOPT_TEST_COMPONENTS to a comma-separated list of components (routing,lp,mip) |
| 11 | +# or "all" if broad/shared changes are detected or if not in a pull-request build. |
| 12 | + |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +detect_test_components() { |
| 16 | + # If CUOPT_TEST_COMPONENTS is already set externally, respect it |
| 17 | + if [[ -n "${CUOPT_TEST_COMPONENTS:-}" ]]; then |
| 18 | + export CUOPT_TEST_COMPONENTS |
| 19 | + return |
| 20 | + fi |
| 21 | + |
| 22 | + # Only apply selective testing for pull-request builds |
| 23 | + if [[ "${RAPIDS_BUILD_TYPE:-}" != "pull-request" ]]; then |
| 24 | + export CUOPT_TEST_COMPONENTS="all" |
| 25 | + return |
| 26 | + fi |
| 27 | + |
| 28 | + # In RAPIDS CI pull-request builds, the branch is a merge commit so |
| 29 | + # HEAD~1..HEAD gives the full PR diff. If the parent isn't reachable |
| 30 | + # (e.g. shallow clone or non-merge workflow), fall back to running all. |
| 31 | + local changed_files |
| 32 | + if ! git rev-parse --verify HEAD~1 &>/dev/null; then |
| 33 | + export CUOPT_TEST_COMPONENTS="all" |
| 34 | + return |
| 35 | + fi |
| 36 | + if ! changed_files=$(git diff --name-only HEAD~1..HEAD 2>/dev/null); then |
| 37 | + # If git diff fails, run all tests to be safe |
| 38 | + export CUOPT_TEST_COMPONENTS="all" |
| 39 | + return |
| 40 | + fi |
| 41 | + |
| 42 | + if [[ -z "${changed_files}" ]]; then |
| 43 | + export CUOPT_TEST_COMPONENTS="all" |
| 44 | + return |
| 45 | + fi |
| 46 | + |
| 47 | + local components="" |
| 48 | + local run_all=false |
| 49 | + |
| 50 | + # Check for shared/infrastructure changes that should trigger all tests |
| 51 | + while IFS= read -r file; do |
| 52 | + case "${file}" in |
| 53 | + cpp/include/*|cpp/cmake/*|cpp/CMakeLists.txt|cpp/src/utilities/*) |
| 54 | + run_all=true |
| 55 | + break |
| 56 | + ;; |
| 57 | + # Changes to test infrastructure |
| 58 | + cpp/tests/CMakeLists.txt|cpp/tests/utilities/*) |
| 59 | + run_all=true |
| 60 | + break |
| 61 | + ;; |
| 62 | + # Changes to CI infrastructure |
| 63 | + ci/test_cpp.sh|ci/test_python.sh|ci/test_wheel_cuopt.sh|ci/run_ctests.sh|ci/run_cuopt_pytests.sh|ci/detect_test_components.sh) |
| 64 | + run_all=true |
| 65 | + break |
| 66 | + ;; |
| 67 | + # Changes to conda/build config |
| 68 | + conda/*|dependencies.yaml) |
| 69 | + run_all=true |
| 70 | + break |
| 71 | + ;; |
| 72 | + esac |
| 73 | + done <<< "${changed_files}" |
| 74 | + |
| 75 | + if ${run_all}; then |
| 76 | + export CUOPT_TEST_COMPONENTS="all" |
| 77 | + return |
| 78 | + fi |
| 79 | + |
| 80 | + # Detect individual components |
| 81 | + local has_routing=false |
| 82 | + local has_lp=false |
| 83 | + local has_mip=false |
| 84 | + |
| 85 | + while IFS= read -r file; do |
| 86 | + case "${file}" in |
| 87 | + # Routing component |
| 88 | + cpp/src/routing/*|cpp/src/distance/*|cpp/tests/routing/*|cpp/tests/distance_engine/*|cpp/tests/examples/routing/*) |
| 89 | + has_routing=true |
| 90 | + ;; |
| 91 | + python/cuopt/cuopt/routing/*|python/cuopt/cuopt/tests/routing/*) |
| 92 | + has_routing=true |
| 93 | + ;; |
| 94 | + regression/routing*) |
| 95 | + has_routing=true |
| 96 | + ;; |
| 97 | + # LP component |
| 98 | + cpp/src/dual_simplex/*|cpp/src/barrier/*|cpp/src/pdlp/*|cpp/src/math_optimization/*) |
| 99 | + has_lp=true |
| 100 | + ;; |
| 101 | + cpp/tests/linear_programming/*|cpp/tests/dual_simplex/*|cpp/tests/qp/*) |
| 102 | + has_lp=true |
| 103 | + ;; |
| 104 | + python/cuopt/cuopt/tests/linear_programming/*|python/cuopt/cuopt/tests/quadratic_programming/*) |
| 105 | + has_lp=true |
| 106 | + ;; |
| 107 | + # LP regression |
| 108 | + regression/lp*) |
| 109 | + has_lp=true |
| 110 | + ;; |
| 111 | + # MIP component |
| 112 | + cpp/src/branch_and_bound/*|cpp/src/cuts/*|cpp/src/mip_heuristics/*) |
| 113 | + has_mip=true |
| 114 | + ;; |
| 115 | + cpp/tests/mip/*) |
| 116 | + has_mip=true |
| 117 | + ;; |
| 118 | + regression/mip*) |
| 119 | + has_mip=true |
| 120 | + ;; |
| 121 | + # Python source changes that could affect any component |
| 122 | + python/cuopt/cuopt/*.py|python/cuopt/cuopt/distance_engine/*|python/cuopt/cuopt/utils/*) |
| 123 | + has_routing=true |
| 124 | + has_lp=true |
| 125 | + ;; |
| 126 | + python/libcuopt/*) |
| 127 | + has_routing=true |
| 128 | + has_lp=true |
| 129 | + has_mip=true |
| 130 | + ;; |
| 131 | + esac |
| 132 | + done <<< "${changed_files}" |
| 133 | + |
| 134 | + # Build the components string |
| 135 | + if ${has_routing}; then |
| 136 | + components="${components:+${components},}routing" |
| 137 | + fi |
| 138 | + if ${has_lp}; then |
| 139 | + components="${components:+${components},}lp" |
| 140 | + fi |
| 141 | + if ${has_mip}; then |
| 142 | + components="${components:+${components},}mip" |
| 143 | + fi |
| 144 | + |
| 145 | + # If no specific component was detected, default to all |
| 146 | + if [[ -z "${components}" ]]; then |
| 147 | + components="all" |
| 148 | + fi |
| 149 | + |
| 150 | + export CUOPT_TEST_COMPONENTS="${components}" |
| 151 | +} |
| 152 | + |
| 153 | +detect_test_components |
| 154 | +echo "CUOPT_TEST_COMPONENTS=${CUOPT_TEST_COMPONENTS}" |
0 commit comments