Files
cacti/tests/tools/run_request_coverage.sh
T
91a1f170f8 fix(ci): fix PHPStan 2.3.6 findings and regenerate baseline (#8755)
* fix(ci): regenerate PHPStan baseline for phpstan 2.3.6

#8754 bumped phpstan/phpstan 2.3.5 -> 2.3.6, which reports more
array|bool offset-access occurrences than the baseline counts allow and
a few new always-true/false comparisons, failing the locked integration
job on develop. Regenerate the baseline with 2.3.6.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(phpstan): guard db_fetch_row results and drop dead comparisons

Instead of only baselining the new phpstan 2.3.6 findings, fix the code:

- Guard db_fetch_row_prepared() results (array|bool) with is_array()
  before reading offsets in aggregate_graphs, auth_changepassword,
  auth_profile, cli/add_device, cli/repair_database, color, data_input,
  graph_templates, install/upgrades/1_2_20, lib/api_tree, lib/auth,
  lib/html_reports, lib/plugins, lib/reports, lib/template and
  package_import. A missing row now skips/aborts instead of reading
  offsets on false.
- Remove always-true/false conditions: floor() > 0 in get_span(), the
  impossible '-' comparison in dsstats, the redundant == null after
  empty() in user_admin/user_group_admin, and the always-true
  $output == true in spikekill.
- Cast hexdec() to int before bcmul() in process_poller_output_batch so
  INF/NAN cannot reach bcmath.

Regenerate the baseline against the fixed code (2241 -> 2154 entries).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* docs(changelog): add entry for PHPStan 2.3.6 fixes (#8756)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* test(coverage): probe the row guards and dead-condition fixes

Add RowGuardMatrix, a fixture that loads the exact changed statements and
functions from the guarded files, keeps their original line numbers, and
runs them with a missing row and with a populated row so the changed-line
coverage gates see the new guards. Statements that end in exit() run in
dedicated modes.

Register the fixture in run_pr_change_tests.sh (100 percent gate) and
run_request_coverage.sh (80 percent gate), and let run_php_probe pass
arguments to a probe. Refresh the sink inventory baseline for the one
intentional redirect added to reports_item_edit().

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* test(coverage): limit the guards to files that are not mirrored copies

SonarCloud's new-code duplication gate counted the guard lines added to
cli/repair_database.php and install/upgrades/1_2_20.php (mirrored upgrade
logic) and to user_admin.php and user_group_admin.php (mirrored policy
tables). Leave those four files as they are on develop so their existing
baseline entries stay, drop their probes, and regenerate the baseline.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: TheWitness <copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-16 16:59:40 -05:00

77 lines
5.4 KiB
Bash

#!/usr/bin/env bash
# Copyright (C) 2004-2026 The Cacti Group. GPL-2.0-or-later.
set -euo pipefail
: "${BASE_COMMIT:?Coverage needs the pull request base commit}"
: "${CACTI_PR_COVERAGE_OUTPUT:?Coverage needs an evidence directory}"
mkdir -p "$CACTI_PR_COVERAGE_OUTPUT"
command -v grep > /dev/null
merge_base="$(git merge-base "$BASE_COMMIT" HEAD)"
changed_paths="$(git diff --name-only "$merge_base" HEAD)"
run_probe() {
local script="$1" pattern="$2" port="$3"
if [[ -f "$script" ]] && grep -Eq "$pattern" <<< "$changed_paths"; then
HOST_PORT="$port" bash "$script"
fi
}
run_probe tests/e2e/aggregate-item-overrides.sh '^(aggregate_(graphs|templates)\.php|lib/api_aggregate\.php|install/upgrades/1_3_0\.php|tests/e2e/aggregate-item-overrides)' 18273
CACTI_AGGREGATE_TEST_BROWSER=1 run_probe tests/e2e/aggregate-automation.sh '^(automation_aggregates\.php|lib/(api_automation|automation|api_aggregate)\.php|install/upgrades/1_3_0\.php|tests/e2e/aggregate-automation)' 18274
run_probe tests/e2e/accessibility-coverage.sh '^((about|aggregate_graphs|auth_changepassword|auth_resetpassword|automation_templates|color|data_queries|data_templates|graph_realtime|graph_xport|graphs_new|host|host_templates|index|package|permission_denied|plugins|tree|user_admin|user_group_admin|utilities)\.php|include/top_(general_|graph_)?header\.php|install/install\.php|lib/html(_form|_graph)?\.php|tests/e2e/accessibility)' 18275
run_probe tests/e2e/queue/run.sh '^(lib/api_queue\.php|cli/queue_(admin|worker)\.php|include/global_settings\.php|install/upgrades/1_3_0\.php|tests/e2e/queue/)' 18276
run_php_probe() {
local script="$1" pattern="$2"
shift 2
local php_args=(-d memory_limit=2G)
if [[ -n "${CACTI_PR_XDEBUG_EXTENSION:-}" ]]; then
php_args+=(-d "zend_extension=$CACTI_PR_XDEBUG_EXTENSION")
fi
if [[ -f "$script" ]] && grep -Eq "$pattern" <<< "$changed_paths"; then
CACTI_PR_RAW_COVERAGE="$CACTI_PR_COVERAGE_OUTPUT" XDEBUG_MODE=coverage php "${php_args[@]}" "$script" "$@"
fi
}
run_php_probe tests/fixtures/Core/Auth/BoundaryContracts.php '^(auth_changepassword\.php|include/global\.php|lib/auth\.php|package_import\.php|tests/fixtures/Core/Auth/BoundaryContracts\.php)'
run_php_probe tests/fixtures/Core/ScriptSubstitutionMatrix.php '^(lib/functions\.php|tests/fixtures/Core/ScriptSubstitutionMatrix\.php)'
run_php_probe tests/fixtures/Core/ConstantTimeGuardMatrix.php '^(auth_2fa\.php|auth_resetpassword\.php|tests/fixtures/Core/ConstantTimeGuardMatrix\.php)'
run_php_probe tests/fixtures/Core/PackageManifestMatrix.php '^(package_import\.php|package_repos\.php|tests/fixtures/Core/PackageManifestMatrix\.php)'
run_php_probe tests/fixtures/Core/PackageSaveMatrix.php '^(package_repos\.php|tests/fixtures/Core/PackageSaveMatrix\.php)'
run_php_probe tests/fixtures/Core/GraphDrilldownMarkup.php '^(lib/html\.php|tests/fixtures/Core/GraphDrilldownMarkup\.php)'
run_php_probe tests/fixtures/Core/SpikekillMutationMatrix.php '^(lib/spikekill\.php|tests/fixtures/Core/SpikekillMutationMatrix\.php)'
run_php_probe tests/fixtures/Core/SpikekillRequestGate.php '^(spikekill\.php|tests/fixtures/Core/SpikekillRequestGate\.php)'
run_php_probe tests/fixtures/Core/Rrd/FallbackSourceIdentity.php '^(lib/rrd\.php|tests/fixtures/Core/Rrd/FallbackSourceIdentity\.php)'
run_php_probe tests/fixtures/SnmpAgentGlobalSettingsCoverage.php '^(lib/snmpagent\.php|tests/fixtures/SnmpAgentGlobalSettingsCoverage\.php)'
run_php_probe tests/fixtures/CmdBoostFlushRedirectCoverage.php '^(cmd\.php|tests/fixtures/CmdBoostFlushRedirectCoverage\.php)'
run_php_probe tests/fixtures/ScriptServerLogMaskingCoverage.php '^(cmd\.php|script_server\.php|lib/poller\.php|tests/fixtures/ScriptServerLogMaskingCoverage\.php)'
run_php_probe tests/fixtures/UserAdministrationAuditCoverage.php '^(user_admin\.php|lib/functions\.php|tests/fixtures/UserAdministrationAudit)'
run_php_probe tests/fixtures/Core/GraphViewCrossFilterCoverage.php '^(lib/html\.php|lib/html_graph\.php|tests/fixtures/Core/GraphViewCrossFilterCoverage\.php)'
run_php_probe tests/fixtures/Core/HtmlGraphMarkup.php '^(lib/html_graph\.php|tests/fixtures/Core/HtmlGraphMarkup\.php)'
run_php_probe tests/fixtures/Core/TreeFilterDeviceTemplateCoverage.php '^(lib/html_tree\.php|tests/fixtures/Core/TreeFilterDeviceTemplateCoverage\.php)'
run_php_probe tests/fixtures/Core/DataSourceRrdRepairCoverage.php '^(data_sources\.php|tests/fixtures/Core/DataSourceRrdRepairCoverage\.php)'
run_php_probe tests/fixtures/Core/PackageArchive.php '^(lib/package\.php|tests/fixtures/Core/PackageArchive\.php)'
row_guard_files='^(aggregate_graphs|auth_changepassword|auth_profile|color|data_input|data_source_profiles|graph_templates)\.php|^cli/add_device\.php|^lib/(api_tree|auth|dsstats|html_reports|plugins|poller|reports|template)\.php|^tests/fixtures/Core/RowGuardMatrix\.php'
run_php_probe tests/fixtures/Core/RowGuardMatrix.php "$row_guard_files"
for row_guard_mode in auth-changepassword add-device reports-edit; do
run_php_probe tests/fixtures/Core/RowGuardMatrix.php "$row_guard_files" "$row_guard_mode"
done
run_probe tests/e2e/installer-coverage.sh '^(lib/installer\.php|cli/(install_cacti|upgrade_database)\.php|install/(functions|preflight|report|background|install)\.php|tests/e2e/installer)' 18277
run_php_probe tests/fixtures/Installer/RetiredThemeCleanup.php '^(install/(functions\.php|upgrades/1_3_0\.php)|tests/fixtures/Installer/RetiredThemeCleanup\.php)'
run_php_probe tests/Unit/fixtures/graph_group_probe.php '^(lib/functions\.php|tests/Unit/fixtures/graph_group_probe\.php)'