Files
TheWitnessandCopilot 9b217e0e3a INFO: set compat to 1.2.29 (#838)
* INFO: set compat to 1.2.32

* docs: update Copilot instructions Cacti baseline to 1.2.32 (matches INFO compat)

* INFO: back off compat to 1.2.29

* Add manifest.json + upgrade-time file pruning

* Prune phpunit.xml on upgrade and ignore markdown-lint configs

phpunit.xml ships in the repo for CI but is a dev-only test artifact, so it
is removed from manifest.json 'expected', pruned from installs on upgrade
(like tests/), and excluded from the manifest drift check.

The retired markdown-lint configs (.mdlrc, .md_style.rb) are deleted, and
.md* is now ignored like .git*: protected from pruning and excluded from
drift if it reappears.

* Move thold_functions.php and thold_webapi.php into includes/; flatten css

The main threshold library becomes includes/functions.php and the web API
helper becomes includes/webapi.php; every caller (setup.php, includes/*, the
CLI/poller/notify entry points and the test suite) loads them from includes/.
Each per-theme stylesheet css/<theme>/main.css becomes css/<theme>.css so users
can drop their own css/<theme>.css into the css base, and the page_head hook
looks there. Old paths are tombstoned so existing installs drop the stale
copies on upgrade.

* Regenerate cacti.pot for the includes/ file relocations

Moving the plugin's library files into includes/ changed the source-reference
lines in the translation template; regenerate it so the 'Verify translation
template' CI check passes.

* Harden the hook-include test and make RPN integer ops explicit

The HookFunctionsIncludeTest invokes real hooks with placeholder input purely
to execute their relocated include_once lines; it now swallows engine
notices/warnings/deprecations during those calls (and restores the error
handler and config afterward) so it cannot trip the suite's failOnDeprecation
or leak state into other tests. The RPN % and ^ operators now cast their
operands to int explicitly, matching their long-standing integer-truncation
behaviour while silencing the implicit float-to-int conversion deprecation.

* Harden prune against tampered-manifest path traversal

The upgrade-time prune now rejects any tombstone containing '.'/'..' segments
(which could escape the plugin directory or resolve to its root) and treats
ancestors of whitelist entries as protected, so a tombstone on a parent
directory can no longer delete a whitelisted file beneath it.

* docs: update and align the Project Structure block

Point the structure tree at the relocated files (libraries now under
includes/, data files under docs/, stylesheets under css/) and align the
trailing '# ...' comments to a single column so they no longer drift right.

* docs/tests: standardize test header and correct manifest-contract notes

- Use the full license header (from the plugin's own setup.php) in
  tests/Unit/PruneFilesTest.php instead of the abbreviated copyright banner.
- Document that the manifest drift check and the upgrade-time prune also
  handle phpunit.xml and .md* files, matching the implemented behavior.

* refactor: rename prune helpers to the plugin-slug function prefix

The repository naming contract reserves the plugin_<name>_ prefix for plugin
lifecycle / hook-registration functions; all other functions use the plain
<name>_ prefix. Rename the internal upgrade helpers accordingly:

  plugin_<name>_prune_files() -> <name>_prune_files()
  plugin_<name>_rmtree()      -> <name>_rmtree()

The call site, unit tests, and the copilot-instructions.md references are
updated to match. No behavioral change.

* chore: set the Cacti compatibility floor to 1.2.29

Align the compat metadata (and the contributor-guide references) with the
team decision to standardize the minimum supported Cacti version at 1.2.29;
the previously proposed 1.2.32 floor was not adopted.

---------

Co-authored-by: Copilot <copilot@users.noreply.github.com>
2026-10-01 01:40:00 -05:00

234 lines
7.1 KiB
PHP

<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
/*
* Report line coverage for the lines a branch changes.
*
* Whole-file coverage is not a useful gate here: most of the plugin only runs
* inside a live Cacti, so the repository figure would sit near zero no matter
* how well a change is tested. What a reviewer wants to know is whether the
* lines this branch adds are exercised, which is what this measures.
*
* Usage: php tests/bin/patch-coverage.php <clover.xml> <base-ref> [min-percent]
*
* Exits 1 if coverage is below the threshold, 2 on bad input.
*/
if ($argc < 3) {
fwrite(STDERR, "usage: patch-coverage.php <clover.xml> <base-ref> [min-percent]\n");
exit(2);
}
$clover_path = $argv[1];
$base_ref = $argv[2];
$minimum = isset($argv[3]) ? (float) $argv[3] : 100.0;
if (!is_readable($clover_path)) {
fwrite(STDERR, "cannot read coverage report: $clover_path\n");
exit(2);
}
/**
* Line numbers each measured file changed, keyed by repository-relative path.
*
* Only added and modified lines count. Deletions have nothing left to cover,
* and context lines were not part of this change.
*
* Paths stay repository-relative so the report can be produced in a container
* and evaluated on the host, where the absolute paths differ.
*
* @param string $base_ref Git ref to diff against.
*
* @return array<string, array<int, bool>>
*/
function changed_lines($base_ref) {
$command = 'git diff --no-ext-diff --unified=0 --no-color --diff-filter=AM ' . escapeshellarg($base_ref) . '...HEAD -- "*.php"';
$output = [];
$status = 0;
$last_line = exec($command, $output, $status);
if ($last_line === false || $status !== 0) {
fwrite(STDERR, "git diff failed\n");
exit(2);
}
$diff = implode("\n", $output);
$changed = [];
$file = null;
foreach (explode("\n", $diff) as $line) {
if (strncmp($line, '+++ b/', 6) === 0) {
$file = substr($line, 6);
if (strncmp($file, 'tests/', 6) === 0) {
$file = null;
continue;
}
$changed[$file] = [];
} elseif (strncmp($line, '@@', 2) === 0 && $file !== null) {
if (preg_match('/\+(\d+)(?:,(\d+))?/', $line, $match)) {
$start = (int) $match[1];
$count = isset($match[2]) ? (int) $match[2] : 1;
for ($i = 0; $i < $count; $i++) {
$changed[$file][$start + $i] = true;
}
}
}
}
return $changed;
}
$changed = changed_lines($base_ref);
$clover = simplexml_load_file($clover_path);
if ($clover === false) {
fwrite(STDERR, "cannot parse coverage report: $clover_path\n");
exit(2);
}
$covered = 0;
$total = 0;
$missing = [];
$measured = [];
foreach ($clover->xpath('//file') as $file) {
$path = (string) $file['name'];
$relative = null;
foreach (array_keys($changed) as $candidate) {
if ($path === $candidate || substr($path, -strlen('/' . $candidate)) === '/' . $candidate) {
$relative = $candidate;
break;
}
}
if ($relative === null) {
continue;
}
$measured[$relative] = true;
foreach ($file->line as $line) {
$number = (int) $line['num'];
// Only statement lines are measurable; method markers double-count.
if ((string) $line['type'] !== 'stmt' || !isset($changed[$relative][$number])) {
continue;
}
$total++;
if ((int) $line['count'] > 0) {
$covered++;
} else {
$missing[] = $relative . ':' . $number;
}
}
}
/*
* These entry points require a complete running Cacti and cannot safely be
* loaded into the isolated unit process. Keep the exception explicit: any
* newly changed production PHP file must either appear in Clover or be added
* here with reviewable justification.
*/
$unmeasured_allowlist = [
// Database migration/schema declarations require a live Cacti database.
'includes/database.php',
// Authenticated web entry point; its status mapping lives in the covered
// thold_notification_queue_status_cells() helper.
'notify_queue.php',
'thold_notify.php',
// Daemon entry point: does CLI bootstrap, pcntl signal handling, and
// argv parsing at the top level, so it cannot be require()'d into the
// isolated unit process. Its persistence/evaluation logic lives in the
// covered thold_daemon_persist_sample()/thold_check_threshold() helpers.
'thold_process.php',
// CLI entry points: include cli_check.php / chdir and execute at the
// top level, so they cannot be require()'d into the isolated unit process.
'cli_import.php',
'cli_thresholds.php',
// Poller entry point: bootstraps the poller environment and executes
// at the top level, so it cannot be require()'d into the isolated unit
// process.
'poller_thold.php',
// Authenticated web entry points: chdir + include(auth.php) and several
// lib/* includes at the top level, so they cannot be require()'d into
// the isolated unit process.
'thold.php',
'thold_graph.php',
'thold_templates.php',
'notify_lists.php',
// Daemon entry point, same category as thold_process.php above (pcntl
// signal handling at the top level).
'thold_daemon.php',
// Settings-page and header-tab-bar hook callbacks: only meaningful when
// invoked by Cacti's plugin hook dispatcher inside a live page render,
// with the real $settings/$config arrays and database populated.
'includes/settings.php',
'includes/tab.php',
// Web-page action handlers (thold_wizard() et al.) that read $_REQUEST
// and read/write the Cacti database directly; only meaningful inside a
// live authenticated request, not the isolated unit process.
'includes/webapi.php',
];
$unmeasured = array_values(array_diff(array_keys($changed), array_keys($measured)));
$unexpected_unmeasured = array_values(array_diff($unmeasured, $unmeasured_allowlist));
if ($unmeasured !== []) {
print "Changed production PHP files absent from Clover:\n " . implode("\n ", $unmeasured) . "\n";
}
if ($unexpected_unmeasured !== []) {
print "FAIL: changed production PHP files are not measured or allowlisted:\n "
. implode("\n ", $unexpected_unmeasured) . "\n";
exit(1);
}
if ($total === 0) {
print "Patch coverage: no measured lines changed.\n";
exit(0);
}
$percent = ($covered / $total) * 100;
printf("Patch coverage: %.2f%% (%d/%d lines)\n", $percent, $covered, $total);
if ($missing !== []) {
print "Uncovered changed lines:\n " . implode("\n ", $missing) . "\n";
}
if ($percent + 0.005 < $minimum) {
printf("FAIL: below the %.2f%% minimum.\n", $minimum);
exit(1);
}
exit(0);