Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Lib/test/test_tools/test_check_warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import importlib
import unittest
from pathlib import Path
from test.support import captured_stdout, os_helper

from test.test_tools import imports_under_tool, skip_if_missing


skip_if_missing("build")
with imports_under_tool("build"):
check_warnings = importlib.import_module("check_warnings")


class CheckWarningsTests(unittest.TestCase):
def test_partial_improvement(self):
for expected, actual, ignore_all, status in (
(2, 1, False, 1), (2, 0, False, 1),
(2, 2, False, 0), (2, 3, False, 0),
(0, 0, False, 0), (2, 1, True, 0),
):
with self.subTest(expected=expected, actual=actual,
ignore_all=ignore_all):
rules = {check_warnings.IgnoreRule(
"source.c", expected, ignore_all)}
warnings = {"source.c": [{}] * actual} if actual else {}
with captured_stdout():
result = check_warnings.get_unexpected_improvements(
rules, warnings)
self.assertEqual(result, status)

def test_cli_partial_improvement(self):
with os_helper.temp_dir() as directory:
output = Path(directory, "compiler.txt")
output.write_text("source.c:1:2: warning: example [-Wunused]\n",
encoding="utf-8")
ignore = Path(directory, "ignore.txt")
ignore.write_text("source.c 2\n", encoding="utf-8")
args = ["-c", str(output), "-i", str(ignore), "-t", "gcc", "-p", ""]
with captured_stdout():
self.assertEqual(check_warnings.main(args), 0)
self.assertEqual(check_warnings.main(
args + ["--fail-on-improvement"]), 1)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix ``Tools/build/check_warnings.py`` to detect warning-count reductions
when some warnings remain, and to accept zero expected warnings when none
are present.
14 changes: 5 additions & 9 deletions Tools/build/check_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,19 @@ def get_unexpected_improvements(
files_with_warnings: dict[str, list[CompileWarning]],
) -> int:
"""
Returns failure status if the number of warnings for a file is greater
Returns failure status if the number of warnings for a file is smaller
than the expected number of warnings for that file based on the ignore
rules
"""
unexpected_improvements = []
for rule in ignore_rules:
if (
not rule.ignore_all
and rule.file_path not in files_with_warnings.keys()
):
if rule.file_path not in files_with_warnings.keys():
unexpected_improvements.append((rule.file_path, rule.count, 0))
elif len(files_with_warnings[rule.file_path]) < rule.count:
if not rule.ignore_all:
count = len(files_with_warnings.get(rule.file_path, ()))
if count < rule.count:
unexpected_improvements.append((
rule.file_path,
rule.count,
len(files_with_warnings[rule.file_path]),
count,
))

if unexpected_improvements:
Expand Down
Loading