"""Regression tests for false green results in the changed-code coverage gate. Copyright (C) 2004-2026 The Cacti Group. GPL-2.0-or-later. """ from contextlib import redirect_stdout import importlib.util import io import json from pathlib import Path import tempfile import unittest import sys from unittest.mock import patch sys.dont_write_bytecode = True SPEC = importlib.util.spec_from_file_location('coverage_gate', Path(__file__).with_name('report_changed_coverage.py')) GATE = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(GATE) class CoverageEvidenceTest(unittest.TestCase): def test_deletion_only_files_do_not_require_added_line_coverage(self): diff = '--- a/lib/old.php\n+++ b/lib/old.php\n@@ -3 +2,0 @@\n-return true;\n' with patch.object(GATE.subprocess, 'check_output', side_effect=['base', diff]): self.assertEqual(GATE.added_lines('HEAD^'), {}) def test_deleted_file_headers_do_not_become_added_source_lines(self): diff = ('diff --git a/old.css b/old.css\n--- a/old.css\n+++ /dev/null\n' '@@ -1 +0,0 @@\n-body { color: red; }\n' 'diff --git a/current.css b/current.css\n--- a/current.css\n' '+++ b/current.css\n@@ -0,0 +1 @@\n+body { color: blue; }\n' 'diff --git a/last.css b/last.css\n--- a/last.css\n' '+++ /dev/null\n@@ -1 +0,0 @@\n-body { color: red; }\n') with patch.object(GATE.subprocess, 'check_output', side_effect=['base', diff]): self.assertEqual(GATE.added_lines('HEAD^'), {'current.css': {1}}) def evaluate(self, evidence, added=(3,), minimum=None): with tempfile.TemporaryDirectory() as temporary: root = Path(temporary) (root / 'lib').mkdir() source = root / 'lib/example.php' source.write_text('